Completed
Push — master ( 52471e...4da56f )
by Ryan
06:22
created

AddonManager::getAddonNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Addon;
2
3
use Anomaly\Streams\Platform\Addon\Event\AddonsHaveRegistered;
4
use Anomaly\Streams\Platform\Addon\Extension\ExtensionModel;
5
use Anomaly\Streams\Platform\Addon\Module\ModuleModel;
6
use Illuminate\Contracts\Events\Dispatcher;
7
8
/**
9
 * Class AddonManager
10
 *
11
 * @link    http://pyrocms.com/
12
 * @author  PyroCMS, Inc. <[email protected]>
13
 * @author  Ryan Thompson <[email protected]>
14
 */
15
class AddonManager
16
{
17
18
    /**
19
     * The addon paths.
20
     *
21
     * @var AddonPaths
22
     */
23
    protected $paths;
24
25
    /**
26
     * The addon collection.
27
     *
28
     * @var AddonCollection
29
     */
30
    protected $addons;
31
32
    /**
33
     * The addon loader.
34
     *
35
     * @var AddonLoader
36
     */
37
    protected $loader;
38
39
    /**
40
     * The addon integrator.
41
     *
42
     * @var AddonIntegrator
43
     */
44
    protected $integrator;
45
46
    /**
47
     * The modules model.
48
     *
49
     * @var ModuleModel
50
     */
51
    protected $modules;
52
53
    /**
54
     * The event dispatcher.
55
     *
56
     * @var Dispatcher
57
     */
58
    protected $dispatcher;
59
60
    /**
61
     * The extensions model.
62
     *
63
     * @var ExtensionModel
64
     */
65
    protected $extensions;
66
67
    /**
68
     * Create a new AddonManager instance.
69
     *
70
     * @param AddonPaths $paths
71
     * @param AddonLoader $loader
72
     * @param ModuleModel $modules
73
     * @param Dispatcher $dispatcher
74
     * @param ExtensionModel $extensions
75
     * @param AddonIntegrator $integrator
76
     * @param AddonCollection $addons
77
     */
78
    public function __construct(
79
        AddonPaths $paths,
80
        AddonLoader $loader,
81
        ModuleModel $modules,
82
        Dispatcher $dispatcher,
83
        ExtensionModel $extensions,
84
        AddonIntegrator $integrator,
85
        AddonCollection $addons
86
    ) {
87
        $this->paths      = $paths;
88
        $this->addons     = $addons;
89
        $this->loader     = $loader;
90
        $this->modules    = $modules;
91
        $this->integrator = $integrator;
92
        $this->dispatcher = $dispatcher;
93
        $this->extensions = $extensions;
94
    }
95
96
    /**
97
     * Register all addons.
98
     */
99
    public function register()
100
    {
101
        $enabled   = $this->getEnabledAddonNamespaces();
102
        $installed = $this->getInstalledAddonNamespaces();
103
104
        $paths = $this->paths->all();
105
106
        /*
107
         * First load all the addons
108
         * so they're available.
109
         */
110
        foreach ($paths as $path) {
111
            $this->loader->load($path);
112
        }
113
114
        $this->loader->register();
115
116
        /*
117
         * Then register all of the addons now
118
         * that they're all PSR autoloaded.
119
         */
120
        foreach ($paths as $path) {
121
122
            $namespace = $this->getAddonNamespace($path);
123
124
            $this->integrator->register(
125
                $path,
126
                $namespace,
127
                in_array($namespace, $enabled),
128
                in_array($namespace, $installed)
129
            );
130
        }
131
132
        // Sort all addons.
133
        $this->addons = $this->addons->sort();
134
135
        /*
136
         * Disperse addons to their
137
         * respective collections and
138
         * finish the integration service.
139
         */
140
        $this->addons->disperse();
141
        $this->addons->registered();
142
        $this->integrator->finish();
143
144
        $this->dispatcher->fire(new AddonsHaveRegistered($this->addons));
145
    }
146
147
    /**
148
     * Get namespaces for enabled addons.
149
     *
150
     * @return array
151
     */
152 View Code Duplication
    protected function getEnabledAddonNamespaces()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (env('INSTALLED')) {
155
            $modules    = $this->modules->getEnabledNamespaces()->all();
156
            $extensions = $this->extensions->getEnabledNamespaces()->all();
157
        } else {
158
            $modules    = [];
159
            $extensions = [];
160
        }
161
162
        return array_merge($modules, $extensions);
163
    }
164
165
    /**
166
     * Get namespaces for installed addons.
167
     *
168
     * @return array
169
     */
170 View Code Duplication
    protected function getInstalledAddonNamespaces()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        if (env('INSTALLED')) {
173
            $modules    = $this->modules->getInstalledNamespaces()->all();
174
            $extensions = $this->extensions->getInstalledNamespaces()->all();
175
        } else {
176
            $modules    = [];
177
            $extensions = [];
178
        }
179
180
        return array_merge($modules, $extensions);
181
    }
182
183
    /**
184
     * Get the addon namespace.
185
     *
186
     * @param $path
187
     * @return string
188
     */
189
    protected function getAddonNamespace($path)
190
    {
191
        $vendor = strtolower(basename(dirname($path)));
192
        $slug   = strtolower(substr(basename($path), 0, strpos(basename($path), '-')));
193
        $type   = strtolower(substr(basename($path), strpos(basename($path), '-') + 1));
194
195
        return "{$vendor}.{$type}.{$slug}";
196
    }
197
}
198