-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathAddonIntegrator.php
230 lines (194 loc) · 6.35 KB
/
AddonIntegrator.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php namespace Anomaly\Streams\Platform\Addon;
use Anomaly\Streams\Platform\Addon\Command\GetNamespaceWithComposer;
use Anomaly\Streams\Platform\Addon\Event\AddonWasRegistered;
use Anomaly\Streams\Platform\Addon\Extension\Extension;
use Anomaly\Streams\Platform\Addon\Module\Module;
use Anomaly\Streams\Platform\Application\Application;
use Anomaly\Streams\Platform\Support\Configurator;
use Anomaly\Streams\Platform\View\Event\RegisteringTwigPlugins;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\View\Factory;
use Twig_ExtensionInterface;
/**
* Class AddonIntegrator
*
* @link http://pyrocms.com/
* @author PyroCMS, Inc. <[email protected]>
* @author Ryan Thompson <[email protected]>
*/
class AddonIntegrator
{
/**
* The view factory.
*
* @var Factory
*/
protected $views;
/**
* The addon provider.
*
* @var AddonProvider
*/
protected $provider;
/**
* The IoC container.
*
* @var Container
*/
protected $container;
/**
* The addon collection.
*
* @var AddonCollection
*/
protected $collection;
/**
* The application instance.
*
* @var Application
*/
protected $application;
/**
* The configurator utility.
*
* @var Configurator
*/
protected $configurator;
/**
* Create a new AddonIntegrator instance.
*
* @param Factory $views
* @param Container $container
* @param AddonProvider $provider
* @param Application $application
* @param Configurator $configurator
* @param AddonCollection $collection
* @internal param Asset $asset
* @internal param Image $image
*/
public function __construct(
Factory $views,
Container $container,
AddonProvider $provider,
Application $application,
Configurator $configurator,
AddonCollection $collection
)
{
$this->views = $views;
$this->provider = $provider;
$this->container = $container;
$this->collection = $collection;
$this->application = $application;
$this->configurator = $configurator;
}
/**
* Register an addon.
*
* @param $path
* @param $namespace
* @param boolean $enabled
* @param boolean $installed
* @return Addon|Extension|Module|Twig_ExtensionInterface
*/
public function register($path, $namespace, $enabled, $installed)
{
if (!is_dir($path)) {
return null;
}
list($vendor, $type, $slug) = explode('.', $namespace);
$class = studly_case($vendor) . '\\' . studly_case($slug) . studly_case($type) . '\\' . studly_case(
$slug
) . studly_case($type);
if (!class_exists($class)) {
$namespace = dispatch_sync(new GetNamespaceWithComposer($path));
list($vendor, $type, $slug) = explode('.', $namespace);
$class = studly_case($vendor) . '\\' . studly_case($slug) . studly_case($type) . '\\' . studly_case(
$slug
) . studly_case($type);
if (!class_exists($class)) {
return null;
}
}
$addon = app($class)
->setPath($path)
->setType($type)
->setSlug($slug)
->setVendor($vendor);
// If the addon supports states - set the state now.
if ($addon->getType() === 'module' || $addon->getType() === 'extension') {
$addon->setInstalled($installed);
$addon->setEnabled($enabled);
}
// Bind to the service container.
$this->container->alias($addon->getNamespace(), $alias = get_class($addon));
$this->container->instance($alias, $addon);
// Load package configuration.
if (!file_exists(base_path('bootstrap/cache/config.php'))) {
$this->configurator->addNamespace($addon->getNamespace(), $addon->getPath('resources/config'));
// Load published overrides.
$this->configurator->addNamespaceOverrides(
$addon->getNamespace(),
base_path(
'resources/addons/'
. $addon->getVendor() . '/'
. $addon->getSlug() . '-'
. $addon->getType()
. '/config'
)
);
}
// Load application overrides.
$this->configurator->addNamespaceOverrides(
$addon->getNamespace(),
$this->application->getResourcesPath(
'addons/'
. $addon->getVendor() . '/'
. $addon->getSlug() . '-'
. $addon->getType()
. '/config'
)
);
// Continue loading things.
$this->provider->register($addon);
// Add the view / translation namespaces.
$this->views->addNamespace(
$addon->getNamespace(),
[
$this->application->getResourcesPath(
"addons/{$addon->getVendor()}/{$addon->getSlug()}-{$addon->getType()}/views/"
),
base_path("resources/addons/{$addon->getVendor()}/{$addon->getSlug()}-{$addon->getType()}/views/"),
$addon->getPath('resources/views'),
]
);
trans()->addNamespace($addon->getNamespace(), $addon->getPath('resources/lang'));
/*
* If the addon is a plugin then
* load it into Twig when appropriate.
*/
if ($addon->getType() === 'plugin') {
app(Dispatcher::class)->listen(
'Anomaly\Streams\Platform\View\Event\RegisteringTwigPlugins',
function (RegisteringTwigPlugins $event) use ($addon) {
$twig = $event->getTwig();
if ($twig->hasExtension(get_class($addon))) {
return;
}
$twig->addExtension($addon);
}
);
}
$this->collection->put($addon->getNamespace(), $addon);
event(new AddonWasRegistered($addon));
return $addon;
}
/**
* Finish up addon integration.
*/
public function finish()
{
$this->provider->boot();
}
}