This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathComponentInstaller.php
838 lines (758 loc) · 27.4 KB
/
ComponentInstaller.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
<?php
/**
* @see https://github.com/zendframework/zend-component-installer for the canonical source repository
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-component-installer/blob/master/LICENSE.md New BSD License
*/
namespace Zend\ComponentInstaller;
use ArrayObject;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use DirectoryIterator;
use Zend\ComponentInstaller\Injector\AbstractInjector;
use Zend\ComponentInstaller\Injector\ConfigInjectorChain;
use Zend\ComponentInstaller\Injector\InjectorInterface;
use function array_filter;
use function array_flip;
use function array_keys;
use function array_map;
use function array_unshift;
use function explode;
use function file_exists;
use function file_get_contents;
use function implode;
use function in_array;
use function is_array;
use function is_dir;
use function is_numeric;
use function is_string;
use function preg_match;
use function preg_replace;
use function rtrim;
use function sprintf;
use function str_replace;
use function stripslashes;
use function strtolower;
use function substr;
/**
* If a package represents a component module, update the application configuration.
*
* Packages opt-in to this workflow by defining one or more of the keys:
*
* - extra.zf.component
* - extra.zf.module
* - extra.zf.config-provider
*
* with the value being the string namespace the component and/or module
* defines, or, in the case of config-provider, the fully qualified class name
* of the provider:
*
* <code class="lang-javascript">
* {
* "extra": {
* "zf": {
* "component": "Zend\\Form",
* "module": "ZF\\Apigility\\ContentNegotiation",
* "config-provider": "Zend\\Expressive\\PlatesRenderer\\ConfigProvider"
* }
* }
* }
* </code>
*
* With regards to components and modules, for this to work correctly, the
* package MUST define a `Module` in the namespace listed in either the
* extra.zf.component or extra.zf.module definition.
*
* Components are added to the TOP of the modules list, to ensure that userland
* code and/or modules can override the settings. Modules are added to the
* BOTTOM of the modules list. Config providers are added to the TOP of
* configuration providers.
*
* In either case, you can edit the appropriate configuration file when
* complete to create a specific order.
*/
class ComponentInstaller implements
EventSubscriberInterface,
PluginInterface
{
/**
* Cached injectors to re-use for packages installed later in the current process.
*
* @var Injector\InjectorInterface[]
*/
private $cachedInjectors = [];
/**
* @var Composer
*/
private $composer;
/**
* @var IOInterface
*/
private $io;
/**
* Map of known package types to composer config keys.
*
* @var string[]
*/
private $packageTypes = [
Injector\InjectorInterface::TYPE_CONFIG_PROVIDER => 'config-provider',
Injector\InjectorInterface::TYPE_COMPONENT => 'component',
Injector\InjectorInterface::TYPE_MODULE => 'module',
];
/**
* Project root in which to install.
*
* @var string
*/
private $projectRoot;
/**
* Constructor
*
* Optionally accept the project root into which to install.
*
* @param string $projectRoot
*/
public function __construct($projectRoot = '')
{
if (is_string($projectRoot) && ! empty($projectRoot) && is_dir($projectRoot)) {
$this->projectRoot = $projectRoot;
}
}
/**
* Activate plugin.
*
* Sets internal pointers to Composer and IOInterface instances, and resets
* cached injector map.
*
* @param Composer $composer
* @param IOInterface $io
* @return void
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
$this->cachedInjectors = [];
}
/**
* Return list of event handlers in this class.
*
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
'post-package-install' => 'onPostPackageInstall',
'post-package-uninstall' => 'onPostPackageUninstall',
];
}
/**
* post-package-install event hook.
*
* This routine exits early if any of the following conditions apply:
*
* - Executed in non-development mode
* - No config/application.config.php is available
* - The composer.json does not define one of either extra.zf.component
* or extra.zf.module
* - The value used for either extra.zf.component or extra.zf.module are
* empty or not strings.
*
* Otherwise, it will attempt to update the application configuration
* using the value(s) discovered in extra.zf.component and/or extra.zf.module,
* writing their values into the `modules` list.
*
* @param PackageEvent $event
* @return void
*/
public function onPostPackageInstall(PackageEvent $event)
{
if (! $event->isDevMode()) {
// Do nothing in production mode.
return;
}
$package = $event->getOperation()->getPackage();
$name = $package->getName();
$extra = $this->getExtraMetadata($package->getExtra());
if (empty($extra)) {
// Package does not define anything of interest; do nothing.
return;
}
$packageTypes = $this->discoverPackageTypes($extra);
$options = (new ConfigDiscovery())
->getAvailableConfigOptions($packageTypes, $this->projectRoot);
if ($options->isEmpty()) {
// No configuration options found; do nothing.
return;
}
$dependencies = $this->loadModuleClassesDependencies($package);
$applicationModules = $this->findApplicationModules();
$this->marshalInstallableModules($extra, $options)
->each(function ($module) use ($name) {
})
// Create injectors
->reduce(function ($injectors, $module) use ($options, $packageTypes, $name) {
// Get extra from root package
$rootExtra = $this->getExtraMetadata($this->composer->getPackage()->getExtra());
$whitelist = $rootExtra['component-whitelist'] ?? [];
$packageType = $packageTypes[$module];
$injectors[$module] = $this->promptForConfigOption($module, $options, $packageType, $name, $whitelist);
return $injectors;
}, new Collection([]))
// Inject modules into configuration
->each(function ($injector, $module) use ($name, $packageTypes, $applicationModules, $dependencies) {
if (isset($dependencies[$module])) {
$injector->setModuleDependencies($dependencies[$module]);
}
$injector->setApplicationModules($applicationModules);
$this->injectModuleIntoConfig($name, $module, $injector, $packageTypes[$module]);
});
}
/**
* Find all Module classes in the package and their dependencies
* via method `getModuleDependencies` of Module class.
*
* These dependencies are used later
* @see \Zend\ComponentInstaller\Injector\AbstractInjector::injectAfterDependencies
* to add component in a correct order on the module list - after dependencies.
*
* It works with PSR-0, PSR-4, 'classmap' and 'files' composer autoloading.
*
* @param PackageInterface $package
* @return array
*/
private function loadModuleClassesDependencies(PackageInterface $package)
{
$dependencies = new ArrayObject([]);
$installer = $this->composer->getInstallationManager();
$packagePath = $installer->getInstallPath($package);
$this->mapAutoloaders($package->getAutoload(), $dependencies, $packagePath);
return $dependencies->getArrayCopy();
}
/**
* Find all modules of the application.
*
* @return array
*/
private function findApplicationModules()
{
$modulePath = is_string($this->projectRoot) && ! empty($this->projectRoot)
? sprintf('%s/module', $this->projectRoot)
: 'module';
$modules = [];
if (is_dir($modulePath)) {
$directoryIterator = new DirectoryIterator($modulePath);
foreach ($directoryIterator as $file) {
if ($file->isDot() || ! $file->isDir()) {
continue;
}
$modules[] = $file->getBasename();
}
}
return $modules;
}
/**
* post-package-uninstall event hook
*
* This routine exits early if any of the following conditions apply:
*
* - Executed in non-development mode
* - No config/application.config.php is available
* - The composer.json does not define one of either extra.zf.component
* or extra.zf.module
* - The value used for either extra.zf.component or extra.zf.module are
* empty or not strings.
*
* Otherwise, it will attempt to update the application configuration
* using the value(s) discovered in extra.zf.component and/or extra.zf.module,
* removing their values from the `modules` list.
*
* @param PackageEvent $event
* @return void
*/
public function onPostPackageUninstall(PackageEvent $event)
{
if (! $event->isDevMode()) {
// Do nothing in production mode.
return;
}
$options = (new ConfigDiscovery())
->getAvailableConfigOptions(
new Collection(array_keys($this->packageTypes)),
$this->projectRoot
);
if ($options->isEmpty()) {
// No configuration options found; do nothing.
return;
}
$package = $event->getOperation()->getPackage();
$name = $package->getName();
$extra = $this->getExtraMetadata($package->getExtra());
$this->removePackageFromConfig($name, $extra, $options);
}
/**
* Retrieve the zf-specific metadata from the "extra" section
*
* @param array $extra
* @return array
*/
private function getExtraMetadata(array $extra)
{
return isset($extra['zf']) && is_array($extra['zf'])
? $extra['zf']
: []
;
}
/**
* Discover what package types are relevant based on what the package
* exposes in the extra configuration.
*
* @param string[] $extra
* @return Collection Collection of Injector\InjectorInterface::TYPE_* constants.
*/
private function discoverPackageTypes(array $extra)
{
$packageTypes = array_flip($this->packageTypes);
$knownTypes = array_keys($packageTypes);
return Collection::create($extra)
->filter(function ($packages, $type) use ($knownTypes) {
return in_array($type, $knownTypes, true);
})
->reduce(function ($discoveredTypes, $packages, $type) use ($packageTypes) {
$packages = is_array($packages) ? $packages : [$packages];
foreach ($packages as $package) {
$discoveredTypes[$package] = $packageTypes[$type];
}
return $discoveredTypes;
}, new Collection([]));
}
/**
* Marshal a collection of defined package types.
*
* @param array $extra extra.zf value
* @return Collection
*/
private function marshalPackageTypes(array $extra)
{
// Create a collection of types registered in the package.
return Collection::create($this->packageTypes)
->filter(function ($configKey, $type) use ($extra) {
return $this->metadataForKeyIsValid($configKey, $extra);
});
}
/**
* Marshal a collection of package modules.
*
* @param array $extra extra.zf value
* @param Collection $packageTypes
* @param Collection $options ConfigOption instances
* @return Collection
*/
private function marshalPackageModules(array $extra, Collection $packageTypes, Collection $options)
{
// We only want to list modules that the application can configure.
$supportedTypes = $options
->reduce(function ($allowed, $option) {
return $allowed->merge($option->getInjector()->getTypesAllowed());
}, new Collection([]))
->unique()
->toArray();
return $packageTypes
->reduce(function ($modules, $configKey, $type) use ($extra, $supportedTypes) {
if (! in_array($type, $supportedTypes, true)) {
return $modules;
}
return $modules->merge((array) $extra[$configKey]);
}, new Collection([]))
// Make sure the list is unique
->unique();
}
/**
* Prepare a list of modules to install/register with configuration.
*
* @param string[] $extra
* @param Collection $options
* @return string[] List of packages to install
*/
private function marshalInstallableModules(array $extra, Collection $options)
{
return $this->marshalPackageModules($extra, $this->marshalPackageTypes($extra), $options)
// Filter out modules that do not have a registered injector
->reject(function ($module) use ($options) {
return $options->reduce(function ($registered, $option) use ($module) {
return $registered || $option->getInjector()->isRegistered($module);
}, false);
});
}
/**
* Prompt for the user to select a configuration location to update.
*
* @param string $name
* @param Collection $options
* @param int $packageType
* @param string $packageName
* @param array $whitelist
* @return Injector\InjectorInterface
*/
private function promptForConfigOption(
string $name,
Collection $options,
int $packageType,
string $packageName,
array $whitelist
) {
if ($cachedInjector = $this->getCachedInjector($packageType)) {
return $cachedInjector;
}
// If package is whitelisted, don't ask...
if (in_array($packageName, $whitelist, true)) {
return $options[1]->getInjector();
}
// Default to first discovered option; index 0 is always "Do not inject"
$default = $options->count() > 1 ? 1 : 0;
$ask = $options->reduce(function ($ask, $option, $index) {
$ask[] = sprintf(
" [<comment>%d</comment>] %s\n",
$index,
$option->getPromptText()
);
return $ask;
}, []);
array_unshift($ask, sprintf(
"\n <question>Please select which config file you wish to inject '%s' into:</question>\n",
$name
));
$ask[] = sprintf(' Make your selection (default is <comment>%d</comment>):', $default);
while (true) {
$answer = $this->io->ask(implode($ask), $default);
if (is_numeric($answer) && isset($options[(int) $answer])) {
$injector = $options[(int) $answer]->getInjector();
$this->promptToRememberOption($injector, $packageType);
return $injector;
}
$this->io->write('<error>Invalid selection</error>');
}
}
/**
* Prompt the user to determine if the selection should be remembered for later packages.
*
* @todo Will need to store selection in filesystem and remove when all packages are complete
* @param Injector\InjectorInterface $injector
* @param int $packageType
* return void
*/
private function promptToRememberOption(Injector\InjectorInterface $injector, $packageType)
{
$ask = ["\n <question>Remember this option for other packages of the same type? (Y/n)</question>"];
while (true) {
$answer = strtolower($this->io->ask(implode($ask), 'y'));
switch ($answer) {
case 'y':
$this->cacheInjector($injector, $packageType);
return;
case 'n':
// intentionally fall-through
default:
return;
}
}
}
/**
* Inject a module into available configuration.
*
* @param string $package Package name
* @param string $module Module to install in configuration
* @param Injector\InjectorInterface $injector Injector to use.
* @param int $packageType
* @return void
*/
private function injectModuleIntoConfig($package, $module, Injector\InjectorInterface $injector, $packageType)
{
$this->io->write(sprintf('<info> Installing %s from package %s</info>', $module, $package));
try {
if (! $injector->inject($module, $packageType)) {
$this->io->write('<info> Package is already registered; skipping</info>');
}
} catch (Exception\RuntimeException $ex) {
$this->io->write(sprintf(
'<error> %s</error>',
$ex->getMessage()
));
}
}
/**
* Remove a package from configuration.
*
* @param string $package Package name
* @param array $metadata Metadata pulled from extra.zf
* @param Collection $configOptions Discovered configuration options from
* which to remove package.
* @return void
*/
private function removePackageFromConfig($package, array $metadata, Collection $configOptions)
{
// Create a collection of types registered in the package.
$packageTypes = $this->marshalPackageTypes($metadata);
// Create a collection of configured injectors for the package types
// registered.
$injectors = $configOptions
->map(function ($configOption) {
return $configOption->getInjector();
})
->filter(function ($injector) use ($packageTypes) {
return $packageTypes->reduce(function ($registered, $key, $type) use ($injector) {
return $registered || $injector->registersType($type);
}, false);
});
// Create a collection of unique modules based on the package types present,
// and remove each from configuration.
$this->marshalPackageModules($metadata, $packageTypes, $configOptions)
->each(function ($module) use ($package, $injectors) {
$this->removeModuleFromConfig($module, $package, $injectors);
});
}
/**
* Remove an individual module defined in a package from configuration.
*
* @param string $module Module to remove
* @param string $package Package in which module is defined
* @param Collection $injectors Injectors to use for removal
* @return void
*/
private function removeModuleFromConfig($module, $package, Collection $injectors)
{
$injectors->each(function (InjectorInterface $injector) use ($module, $package) {
$this->io->write(sprintf('<info> Removing %s from package %s</info>', $module, $package));
if ($injector->remove($module)) {
$this->io->write(sprintf(
'<info> Removed package from %s</info>',
$this->getInjectorConfigFileName($injector)
));
}
});
}
/**
* @param InjectorInterface $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getInjectorConfigFileName(InjectorInterface $injector)
{
if ($injector instanceof ConfigInjectorChain) {
return $this->getInjectorChainConfigFileName($injector);
} elseif ($injector instanceof AbstractInjector) {
return $this->getAbstractInjectorConfigFileName($injector);
}
return '';
}
/**
* @param ConfigInjectorChain $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getInjectorChainConfigFileName(ConfigInjectorChain $injector)
{
return implode(', ', array_map(function ($item) {
return $this->getInjectorConfigFileName($item);
}, $injector->getCollection()->toArray()));
}
/**
* @param AbstractInjector $injector
* @return string
* @todo remove after InjectorInterface has getConfigName defined
*/
private function getAbstractInjectorConfigFileName(AbstractInjector $injector)
{
return $injector->getConfigFile();
}
/**
* Is a given module name valid?
*
* @param string $module
* @return bool
*/
private function moduleIsValid($module)
{
return is_string($module) && ! empty($module);
}
/**
* Is a given metadata value (extra.zf.*) valid?
*
* @param string $key Key to examine in metadata
* @param array $metadata
* @return bool
*/
private function metadataForKeyIsValid($key, array $metadata)
{
if (! isset($metadata[$key])) {
return false;
}
if (is_string($metadata[$key])) {
return $this->moduleIsValid($metadata[$key]);
}
if (! is_array($metadata[$key])) {
return false;
}
return Collection::create($metadata[$key])
->reduce(function ($valid, $value) {
if (false === $valid) {
return $valid;
}
return $this->moduleIsValid($value);
}, null);
}
/**
* Attempt to retrieve a cached injector for the current package type.
*
* @param int $packageType
* @return null|Injector\InjectorInterface
*/
private function getCachedInjector($packageType)
{
if (isset($this->cachedInjectors[$packageType])) {
return $this->cachedInjectors[$packageType];
}
return null;
}
/**
* Cache an injector for later use.
*
* @param Injector\InjectorInterface $injector
* @param int $packageType
* @return void
*/
private function cacheInjector(Injector\InjectorInterface $injector, $packageType)
{
$this->cachedInjectors[$packageType] = $injector;
}
/**
* Iterate through each autoloader type to find dependencies.
*
* @param array $autoload List of autoloader types and associated autoloader definitions.
* @param ArrayObject $dependencies Module dependencies defined by the module.
* @param string $packagePath Path to the package on the filesystem.
* @return void
*/
private function mapAutoloaders(array $autoload, ArrayObject $dependencies, $packagePath)
{
foreach ($autoload as $type => $map) {
$this->mapType($map, $type, $dependencies, $packagePath);
}
}
/**
* Iterate through a single autolaoder type to find dependencies.
*
* @param array $map Map of namespace => path(s) pairs.
* @param string $type Type of autoloader being iterated.
* @param ArrayObject $dependencies Module dependencies defined by the module.
* @param string $packagePath Path to the package on the filesystem.
* @return void
*/
private function mapType(array $map, $type, ArrayObject $dependencies, $packagePath)
{
foreach ($map as $namespace => $paths) {
$paths = (array) $paths;
$this->mapNamespacePaths($paths, $namespace, $type, $dependencies, $packagePath);
}
}
/**
* Iterate through the paths defined for a given namespace.
*
* @param array $paths Paths defined for the given namespace.
* @param string $namespace PHP namespace to which the paths map.
* @param string $type Type of autoloader being iterated.
* @param ArrayObject $dependencies Module dependencies defined by the module.
* @param string $packagePath Path to the package on the filesystem.
* @return void
*/
private function mapNamespacePaths(array $paths, $namespace, $type, ArrayObject $dependencies, $packagePath)
{
foreach ($paths as $path) {
$this->mapPath($path, $namespace, $type, $dependencies, $packagePath);
}
}
/**
* Find module dependencies for a given namespace for a given path.
*
* @param string $path Path to inspect.
* @param string $namespace PHP namespace to which the paths map.
* @param string $type Type of autoloader being iterated.
* @param ArrayObject $dependencies Module dependencies defined by the module.
* @param string $packagePath Path to the package on the filesystem.
* @return void
*/
private function mapPath($path, $namespace, $type, ArrayObject $dependencies, $packagePath)
{
switch ($type) {
case 'classmap':
$fullPath = sprintf('%s/%s', $packagePath, $path);
if (substr($path, -10) === 'Module.php') {
$modulePath = $fullPath;
break;
}
$modulePath = sprintf('%s/Module.php', rtrim($fullPath, '/'));
break;
case 'files':
if (substr($path, -10) !== 'Module.php') {
return;
}
$modulePath = sprintf('%s/%s', $packagePath, $path);
break;
case 'psr-0':
$modulePath = sprintf(
'%s/%s%s%s',
$packagePath,
$path,
str_replace('\\', '/', $namespace),
'Module.php'
);
break;
case 'psr-4':
$modulePath = sprintf(
'%s/%s%s',
$packagePath,
$path,
'Module.php'
);
break;
default:
return;
}
if (! file_exists($modulePath)) {
return;
}
$result = $this->getModuleDependencies($modulePath);
if (empty($result)) {
return;
}
// Mimic array + array operation in ArrayObject
$dependencies->exchangeArray($dependencies->getArrayCopy() + $result);
}
/**
* @param string $file
* @return array
*/
private function getModuleDependencies($file)
{
$content = file_get_contents($file);
if (preg_match('/namespace\s+([^\s]+)\s*;/', $content, $m)) {
$moduleName = $m[1];
// @codingStandardsIgnoreStart
$regExp = '/public\s+function\s+getModuleDependencies\s*\(\s*\)\s*{[^}]*return\s*(?:array\(|\[)([^})\]]*)(\)|\])/';
// @codingStandardsIgnoreEnd
if (preg_match($regExp, $content, $m)) {
$dependencies = array_filter(
explode(',', stripslashes(rtrim(preg_replace('/[\s"\']/', '', $m[1]), ',')))
);
if ($dependencies) {
return [$moduleName => $dependencies];
}
}
}
return [];
}
}