summaryrefslogtreecommitdiff
path: root/app/Core/Plugin/Loader.php
blob: 38f41d39fb18906bc4f893fdb286f99f44968392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

namespace Kanboard\Core\Plugin;

use Composer\Autoload\ClassLoader;
use DirectoryIterator;
use Exception;
use LogicException;
use Kanboard\Core\Tool;

/**
 * Plugin Loader
 *
 * @package Kanboard\Core\Plugin
 * @author  Frederic Guillot
 */
class Loader extends \Kanboard\Core\Base
{
    /**
     * Plugin instances
     *
     * @access protected
     * @var array
     */
    protected $plugins = array();
    protected $incompatiblePlugins = array();

    /**
     * Get list of loaded plugins
     *
     * @access public
     * @return Base[]
     */
    public function getPlugins()
    {
        return $this->plugins;
    }

    /**
     * Get list of not compatible plugins
     *
     * @access public
     * @return Base[]
     */
    public function getIncompatiblePlugins()
    {
        return $this->incompatiblePlugins;
    }

    /**
     * Scan plugin folder and load plugins
     *
     * @access public
     */
    public function scan()
    {
        if (file_exists(PLUGINS_DIR)) {
            $loader = new ClassLoader();
            $loader->addPsr4('Kanboard\Plugin\\', PLUGINS_DIR);
            $loader->register();

            $dir = new DirectoryIterator(PLUGINS_DIR);

            foreach ($dir as $fileInfo) {
                if ($fileInfo->isDir() && substr($fileInfo->getFilename(), 0, 1) !== '.') {
                    $pluginName = $fileInfo->getFilename();
                    $this->initializePlugin($pluginName);
                }
            }
        }
    }

    /**
     * Load plugin schema
     *
     * @access public
     * @param  string $pluginName
     */
    public function loadSchema($pluginName)
    {
        if (SchemaHandler::hasSchema($pluginName)) {
            $schemaHandler = new SchemaHandler($this->container);
            $schemaHandler->loadSchema($pluginName);
        }
    }

    /**
     * Load plugin
     *
     * @access public
     * @throws LogicException
     * @param  string $pluginName
     * @return Base
     */
    public function loadPlugin($pluginName)
    {
        $className = '\Kanboard\Plugin\\'.$pluginName.'\\Plugin';

        if (! class_exists($className)) {
            throw new LogicException('Unable to load this plugin class: '.$className);
        }

        return new $className($this->container);
    }

    /**
     * Initialize plugin
     *
     * @access public
     * @param  string $pluginName
     */
    public function initializePlugin($pluginName)
    {
        try {
            $plugin = $this->loadPlugin($pluginName);

            if (Version::isCompatible($plugin->getCompatibleVersion(), APP_VERSION)) {
                $this->loadSchema($pluginName);

                if (method_exists($plugin, 'onStartup')) {
                    $this->dispatcher->addListener('app.bootstrap', array($plugin, 'onStartup'));
                }

                Tool::buildDIC($this->container, $plugin->getClasses());
                Tool::buildDICHelpers($this->container, $plugin->getHelpers());

                $plugin->initialize();
                $this->plugins[$pluginName] = $plugin;
            } else {
                $this->incompatiblePlugins[$pluginName] = $plugin;
                $this->logger->error($pluginName.' is not compatible with this version');
            }
        } catch (Exception $e) {
            $this->logger->critical($pluginName.': '.$e->getMessage());
        }
    }
}