Issues (29)

lib/Phile/Plugin/AbstractPlugin.php (1 issue)

1
<?php
2
/**
3
 * @author PhileCMS
4
 * @link https://philecms.github.io
5
 * @license http://opensource.org/licenses/MIT
6
 * @package Phile\Plugin
7
 */
8
9
namespace Phile\Plugin;
10
11
use Phile\Core\Config;
12
use Phile\Core\Event;
13
use Phile\Core\Utility;
14
use Phile\Gateway\EventObserverInterface;
15
16
/**
17
 * the AbstractPlugin class for implementing a plugin for PhileCMS
18
 */
19
abstract class AbstractPlugin implements EventObserverInterface
20
{
21
    /**
22
     * @var array plugin attributes
23
     */
24
    private $plugin = [];
25
26
    /**
27
     * @var array subscribed Phile events ['eventName' => 'classMethodToCall']
28
     */
29
    protected $events = [];
30
31
    /**
32
     * @var array the plugin settings
33
     */
34
    protected $settings = [];
35
36
    /**
37
     * Initializes the plugin.
38
     *
39
     * try to keep all initialization in one method to have a clean class
40
     * for the plugin-user
41
     *
42
     * @param string $pluginKey
43
     * @param string $pluginDir Root plugin directory this plugin is placed in.
44
     * @param Event $eventBus Phile application event-bus.
45
     * @param Config $config Phile application configuration.
46
     * @return void
47
     */
48 39
    final public function initializePlugin(
49
        string $pluginKey,
50
        string $pluginDir,
51
        Event $eventBus,
52
        Config $config
53
    ): void {
54
        /**
55
         * init $plugin property
56
         */
57 39
        $this->plugin['key'] = $pluginKey;
58 39
        list($vendor, $name) = explode('\\', $this->plugin['key']);
59 39
        $DS = DIRECTORY_SEPARATOR;
60 39
        $this->plugin['dir'] = $pluginDir . $vendor . $DS . $name . $DS;
61
62
        /**
63
         * init events
64
         */
65 39
        foreach ($this->events as $event => $method) {
66 39
            $eventBus->register($event, $this);
67
        }
68
69
        /**
70
         * init plugin settings
71
         */
72 39
        $defaults = Utility::load($this->getPluginPath('config.php'));
73 39
        if (empty($defaults) || !is_array($defaults)) {
74 34
            $defaults = [];
75
        }
76
77 39
        $globals = $config->toArray();
78 39
        if (!isset($globals['plugins'][$pluginKey])) {
79
            $globals['plugins'][$pluginKey] = [];
80
        }
81
82
        // settings precedence: global > default > class
83 39
        $this->settings = array_replace_recursive(
84 39
            $this->settings,
85
            $defaults,
86 39
            $globals['plugins'][$pluginKey]
87
        );
88
89
        // backwards compatibility to Phile 1.4
90 39
        $this->injectSettings($this->settings);
0 ignored issues
show
Deprecated Code introduced by
The function Phile\Plugin\AbstractPlugin::injectSettings() has been deprecated: since 1.5.1 will be removed ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
        /** @scrutinizer ignore-deprecated */ $this->injectSettings($this->settings);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
92 39
        $globals['plugins'][$pluginKey]['settings'] = $this->settings;
93 39
        $config->set($globals);
94
    }
95
96
    /**
97
     * inject settings
98
     *
99
     * backwards compatibility to Phile 1.4
100
     *
101
     * @param array $settings
102
     * @return void
103
     * @deprecated since 1.5.1 will be removed
104
     */
105
    public function injectSettings(array $settings = null): void
106
    {
107
    }
108
109
    /**
110
     * implements EventObserverInterface
111
     *
112
     * @param string $eventKey
113
     * @param null|array $eventData
114
     * @return void
115
     */
116 36
    public function on($eventKey, $eventData = null): void
117
    {
118 36
        if (!isset($this->events[$eventKey])) {
119
            return;
120
        }
121 36
        $method = $this->events[$eventKey];
122 36
        if (!is_callable([$this, $method])) {
123 1
            $class = get_class($this);
124 1
            throw new \RuntimeException(
125 1
                "Event $eventKey can't invoke $class::$method(). Not callable.",
126
                1428564865
127
            );
128
        }
129 35
        $this->{$this->events[$eventKey]}($eventData);
130
    }
131
132
    /**
133
     * get file path to plugin root (trailing slash) or to a sub-item
134
     *
135
     * @param  string $subPath
136
     * @return string
137
     */
138 39
    protected function getPluginPath(string $subPath = ''): string
139
    {
140 39
        return $this->plugin['dir'] . ltrim($subPath, DIRECTORY_SEPARATOR);
141
    }
142
}
143