<?php
namespace services\plugin;

use \models\Plugin;
/**
 * GetPlugins service class
 * @author Jérôme Brilland
 * @version 2.0
 * @copyright (c) 2021, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */
class GetPlugins {

    /**
     * @access public
     */
    public function perform( $listener ) {
        $plugins = Plugin::findInstalledPlugins();
        foreach ( scandir( APP_DIR . 'plugins' ) as $pluginName ) {
            $filename = APP_DIR . 'plugins' . DS . $pluginName . DS . 'plugin.json';
            if ( file_exists( $filename ) && !array_key_exists( $pluginName, $plugins ) ) {
                $plugin = Plugin::fromJSON( \JSONHandler::decode( file_get_contents( $filename ), true ) );
                if ( $plugin ) {
                    $plugins[$pluginName] = $plugin;
                }
            }
        }
        $listener->getPluginsSucceeded( $plugins );
    }
}
