1
|
|
|
<?php namespace Mascame\Artificer\Extension; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Str; |
4
|
|
|
use Mascame\Extender\Booter\BooterInterface; |
5
|
|
|
use Mascame\Extender\Event\EventInterface; |
6
|
|
|
use Mascame\Extender\Installer\InstallerInterface; |
7
|
|
|
use Symfony\Component\CssSelector\XPath\Extension\AbstractExtension; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
9
|
|
|
|
10
|
|
|
class Manager extends \Mascame\Extender\Manager { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \Mascame\Artificer\Plugin\Manager|\Mascame\Artificer\Widget\Manager |
14
|
|
|
*/ |
15
|
|
|
protected $manager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Composer packages |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $packages = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param InstallerInterface $installer |
26
|
|
|
* @param BooterInterface|null $booter |
27
|
|
|
* @param EventInterface|null $eventDispatcher |
28
|
|
|
* @throws \Exception |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
InstallerInterface $installer, |
32
|
|
|
BooterInterface $booter = null, |
33
|
|
|
EventInterface $eventDispatcher = null |
34
|
|
|
) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($installer, $booter, $eventDispatcher); |
37
|
|
|
|
38
|
|
|
$this->packages = $this->getPackagesVersions(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @param \Closure $plugin |
44
|
|
|
*/ |
45
|
|
|
public function add($name, \Closure $plugin) |
46
|
|
|
{ |
47
|
|
|
if (! $this->isValidNamespace($name)) { |
48
|
|
|
throw new \Exception('Extension namespace is mandatory and must be compliant to "vendor/package". Provided: ' . $name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
parent::add($name, $plugin); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* vendor/package |
56
|
|
|
* |
57
|
|
|
* @param $namespace |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function isValidNamespace($namespace) { |
61
|
|
|
$regex = "/^[\\w-]+\\/[\\w-]+$/"; |
62
|
|
|
|
63
|
|
|
preg_match($regex, $namespace, $matches); |
64
|
|
|
|
65
|
|
|
return (count($matches) > 0); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function getPackagesVersions() { |
72
|
|
|
$installedPackagesFile = config('admin.vendorPath') . '/composer/installed.json'; |
73
|
|
|
$packagesWithName = []; |
74
|
|
|
|
75
|
|
|
if (\File::exists($installedPackagesFile)) { |
76
|
|
|
$packages = json_decode(\File::get($installedPackagesFile), true); |
77
|
|
|
|
78
|
|
|
$packagesWithName = []; |
79
|
|
|
|
80
|
|
|
foreach ($packages as $package) { |
81
|
|
|
$packagesWithName[$package['name']] = $package; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $packagesWithName; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $namespace |
90
|
|
|
* @return mixed |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function getVersion($namespace) { |
94
|
|
|
if (isset($this->packages[$namespace])) { |
95
|
|
|
return $this->packages[$namespace]['version']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new \Exception('Package with namespace "' . $namespace . '" not found (Should be an existent composer package).'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: