Completed
Push — dev ( 6f78cf...ec46b6 )
by Marc
02:13
created

Booter::addAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mascame\Artificer\Extension\AbstractExtension.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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