<commit_before><?php namespace Mascame\Artificer\Plugin;

use Mascame\Arrayer\Arrayer;
use Mascame\Artificer\Options\AdminOption;
use Mascame\Artificer\Options\Option;

class PluginManager {

	public $pluginNamespace;
	protected $instances = array();
    protected static $added_plugins = array();
    protected static $fields = array();
    protected static $routes = array();
    protected static $plugins = array();
    public static $installed_plugins_routes = array();
	public static $plugins_config_file = '/config/packages/mascame/artificer/plugins.php';

    public function __construct($pluginNamespace = null) {
        if ($pluginNamespace) {
            $this->pluginNamespace = $pluginNamespace;

            $this->addPlugin($pluginNamespace);
        }
    }
    
    /**
     * @return mixed
     */
	public function getAll()
	{
		if (self::$plugins) return self::$plugins;

		$plugins = AdminOption::get('plugins');

        if (!isset($plugins['installed'])) {
            $plugins['installed'] = array();
        }

        if (!isset($plugins['uninstalled'])) {
            $plugins['uninstalled'] = array();
        }

        $new_added = array_diff(self::$added_plugins, array_merge($plugins['installed'], $plugins['uninstalled']));

        if (!empty($new_added)) {
            foreach ($new_added as $pluginNamespace) {
                $plugins['uninstalled'][] = $pluginNamespace;
            }

            $arrayer = new Arrayer($plugins);
            \File::put(self::$plugins_config_file, $arrayer->getContent());
        }

		return self::$plugins = $plugins;
	}

    /**
     * @return mixed
     */
	public function boot()
	{
        self::$plugins_config_file = app_path() . self::$plugins_config_file;
        $plugins = $this->getAll();
        $instances = array();

        if (isset($plugins['installed'])) {
            foreach ($plugins['installed'] as $key => $namespace) {
                $instances[$namespace] = \App::make($namespace);
                $instances[$namespace]->boot();

                self::$plugins['installed'][$key] = $instances[$namespace];

                if (isset(self::$fields[$namespace]) && !empty(self::$fields[$namespace])) {
                    $fields = AdminOption::get('classmap');

                    foreach (self::$fields[$namespace] as $field => $class) {
                        $fields[$field] = $class;
                    }

                    AdminOption::set('classmap', $fields);
                }

                self::$installed_plugins_routes[] = self::$routes[$namespace];
            }
        }

        if (isset($plugins['uninstalled'])) {
            foreach ($plugins['uninstalled'] as $key => $namespace) {
                $instances[$namespace] = \App::make($namespace);
                self::$plugins['uninstalled'][$key] = $instances[$namespace];
            }
        }
//dd(self::$plugins);
		return self::$plugins;
	}

    /**
     * @param $plugin
     * @return null
     */
	public function make($pluginNamespace) {
		return ($this->isInstalled($pluginNamespace)) ? $this->getPluginInstance($pluginNamespace) : null;
	}

    protected function getPluginInstance($pluginNamespace) {
        return $this->instances[$pluginNamespace];
    }

    /**
     * @param $pluginNamespace
     * @return mixed<commit_msg>fixed bug with removed plugins<commit_after><?php namespace Mascame\Artificer\Plugin;

use Mascame\Arrayer\Arrayer;
use Mascame\Artificer\Options\AdminOption;
use Mascame\Artificer\Options\Option;

class PluginManager {

	public $pluginNamespace;
	protected $instances = array();
    protected static $added_plugins = array();
    protected static $fields = array();
    protected static $routes = array();
    protected static $plugins = array();
    public static $installed_plugins_routes = array();
	public static $plugins_config_file = '/config/packages/mascame/artificer/plugins.php';

    public function __construct($pluginNamespace = null) {
        if ($pluginNamespace) {
            $this->pluginNamespace = $pluginNamespace;

            $this->addPlugin($pluginNamespace);
        }
    }
    
    /**
     * @return mixed
     */
	public function getAll()
	{
		if (self::$plugins) return self::$plugins;

		$plugins = AdminOption::get('plugins');

        if (!isset($plugins['installed'])) $plugins['installed'] = array();
        if (!isset($plugins['uninstalled'])) $plugins['uninstalled'] = array();

        $config_plugins = array_merge($plugins['installed'], $plugins['uninstalled']);
        $new_added = array_diff(self::$added_plugins, $config_plugins);

        if (!empty($new_added) || count($config_plugins) != count(self::$added_plugins)) {
            $this->generatePluginsFile($plugins);
        }

		return self::$plugins = $plugins;
	}

    /**
     * @param $plugins
     */
    protected function generatePluginsFile($plugins) {
        $old_plugins = $plugins;
        $plugins = array();

        foreach (self::$added_plugins as $pluginNamespace) {
            if (in_array($pluginNamespace, $old_plugins['installed'])) {
                $plugins['installed'][] = $pluginNamespace;
            } else {
                $plugins['uninstalled'][] = $pluginNamespace;
            }
        }

        $arrayer = new Arrayer($plugins);
        \File::put(self::$plugins_config_file, $arrayer->getContent());
    }

    /**
     * @return mixed
     */
	public function boot()
	{
        self::$plugins_config_file = app_path() . self::$plugins_config_file;
        $plugins = $this->getAll();
        $instances = array();

        if (isset($plugins['installed'])) {
            foreach ($plugins['installed'] as $key => $namespace) {
                $instances[$namespace] = \App::make($namespace);
                $instances[$namespace]->boot();

                self::$plugins['installed'][$key] = $instances[$namespace];

                $this->addFields($namespace);

                self::$installed_plugins_routes[] = (isset(self::$routes[$namespace])) ? self::$routes[$namespace] : null;
            }
        }

        if (isset($plugins['uninstalled'])) {
            foreach ($plugins['uninstalled'] as $key => $namespace) {
                $instances[$namespace] = \App::make($namespace);
                self::$plugins['uninstalled'][$key] = $instances[$namespace];
            }
        }

		return self::$plugins;
	}

    /**
     * @param $namespace
     */
    protected function addFields($namespace) {
        if (isset(self::$fields[$namespace]) && !empty(self::$fields[$namespace])) {
            $fields = AdminOption::get('classmap');

            foreach (self::$fields[$namespace] as $field => $class) {
                $fields[$field] = $class;
            }

            AdminOption::set('classmap', $fields);
        }
    }

    /**
     * @param $plugin
     * @return null
     */
	public function make($pluginNamespace) {
		return ($this->isInstalled($pluginNamespace)) ? $this->getPluginInstance($pluginNamespace) : null;
	}

    protected function getPluginInstance($pluginNamespace) {
        return $this->instances[$pluginNamespace];
    }

    /**
     * @param $pluginNamespace
     * @return mixed
{'fraction_non_alphanumeric': 0.15668842894846507, 'fraction_numerical': 0.0, 'mean_word_length': 2.6090225563909772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3305523', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1938, 'n_words': 354}