1
|
|
|
<?php namespace Mascame\Artificer\Extension; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Str; |
4
|
|
|
use Mascame\Extender\Booter\BooterInterface; |
5
|
|
|
use Symfony\Component\CssSelector\XPath\Extension\AbstractExtension; |
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
7
|
|
|
|
8
|
|
|
class Booter extends \Mascame\Extender\Booter\Booter implements BooterInterface { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var \Mascame\Artificer\Plugin\Manager|\Mascame\Artificer\Widget\Manager |
12
|
|
|
*/ |
13
|
|
|
protected $manager; |
14
|
|
|
|
15
|
|
|
public function boot($instance, $name) |
16
|
|
|
{ |
17
|
|
|
$this->beforeBooting($instance, $name); |
18
|
|
|
|
19
|
|
|
parent::boot($instance, $name); |
20
|
|
|
|
21
|
|
|
$this->afterBooting($instance, $name); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $instance |
26
|
|
|
* @param $name |
27
|
|
|
*/ |
28
|
|
|
protected function beforeBooting($instance, $name) { |
29
|
|
|
if (! $instance->namespace) $instance->namespace = $name; |
30
|
|
|
if (! $instance->name) $instance->name = $name; |
31
|
|
|
|
32
|
|
|
if (! $instance->slug) { |
33
|
|
|
// For slug readability |
34
|
|
|
$name = str_replace('\\', '-', $name); |
35
|
|
|
$instance->slug = Str::slug($name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->manager->setSlug($instance->slug, $instance->namespace); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $instance \Mascame\Artificer\Extension\AbstractExtension |
43
|
|
|
* @param $name |
44
|
|
|
*/ |
45
|
|
|
protected function afterBooting($instance, $name) { |
46
|
|
|
if (! $this->manager->isInstalled($instance->namespace)) return; |
47
|
|
|
|
48
|
|
|
$this->addAssets($instance); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $instance |
53
|
|
|
*/ |
54
|
|
|
protected function addAssets($instance) { |
55
|
|
|
$package = 'packages/' . $instance->package . '/'; |
56
|
|
|
|
57
|
|
|
$assets = $instance->assets(); |
58
|
|
|
|
59
|
|
|
$assets = array_map(function($asset) use ($package) { |
60
|
|
|
return $package . $asset; |
61
|
|
|
}, $assets); |
62
|
|
|
|
63
|
|
|
\Assets::config([ |
64
|
|
|
// Reset those dirs to avoid wrong paths |
65
|
|
|
'css_dir' => '', |
66
|
|
|
'js_dir' => '', |
67
|
|
|
])->add($assets); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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: