Completed
Push — master ( 9c1f83...0c1135 )
by Paulo Rodrigues
10:00
created

Compiler/Packages/BasePackagesCompilerPass.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Rj\FrontendBundle\DependencyInjection\Compiler\Packages;
4
5
use Rj\FrontendBundle\DependencyInjection\Compiler\BaseCompilerPass;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
abstract class BasePackagesCompilerPass extends BaseCompilerPass
10
{
11
    abstract protected function getTaggedPackages($container);
12
    abstract protected function getPackagesService($container);
13
14 39
    public function process(ContainerBuilder $container)
15
    {
16 39
        $packages = array();
17 39
        $registeredPackages = $this->getRegisteredPackages($container);
18
19 37
        foreach ($this->getTaggedPackages($container) as $id => $tags) {
20 20
            if (empty($tags) || !isset($tags[0]['alias'])) {
21 2
                throw new \LogicException(
22 2
                    "The tag for the service with id '$id' must define an 'alias' attribute"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $id instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
23
                );
24
            }
25
26 18
            $packageName = $tags[0]['alias'];
27
28 18
            if (isset($registeredPackages[$packageName])) {
29 2
                throw new \LogicException(
30 2
                    "A package named '$packageName' has already been registered"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
31
                );
32
            }
33
34 16
            if (isset($packages[$packageName])) {
35 2
                throw new \LogicException(
36 2
                    "Multiple packages were found with alias '$packageName'. Package alias' must be unique"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $packageName instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
37
                );
38
            }
39
40 16
            $packages[$packageName] = $id;
41
        }
42
43 31
        $this->addPackages($packages, $container);
44
45 31
        if ($container->hasDefinition($this->namespaceService('package.fallback'))) {
46 25
            $this->setDefaultPackage($container);
47
        }
48 31
    }
49
50 31
    protected function addPackages($packages, $container)
51
    {
52 31
        $packagesService = $this->getPackagesService($container);
53
54 31
        foreach ($packages as $name => $id) {
55 14
            $packagesService->addMethodCall(
56 14
                'addPackage',
57 14
                array($name, new Reference($id))
58
            );
59
        }
60 31
    }
61
62 25
    protected function setDefaultPackage($container)
63
    {
64 25
        $packagesService = $this->getPackagesService($container);
65 25
        $defaultPackage = $this->getRegisteredDefaultPackage($container);
66 25
        $fallbackPackageId = $this->namespaceService('package.fallback');
67
68 25
        $container->getDefinition($fallbackPackageId)->addMethodCall('setFallback', array($defaultPackage));
69
70 25
        $packagesService->replaceArgument(0, new Reference($fallbackPackageId));
71 25
    }
72
73
    /**
74
     * Retrieve packages that have already been registered.
75
     *
76
     * @return array with the packages' name as keys
77
     */
78 39
    protected function getRegisteredPackages($container)
79
    {
80 39
        $arguments = $this->getPackagesService($container)->getArguments();
81
82 37
        if (!isset($arguments[1]) || count($arguments[1]) < 2) {
83 33
            return array();
84
        }
85
86 4
        $argPackages = $arguments[1];
87
88 4
        $packages = array();
89 4
        $argCount = count($argPackages);
90 4
        for ($i = 0; $i < $argCount; $i++) {
91 4
            $packages[$argPackages[$i]] = $argPackages[++$i];
92
        }
93
94 4
        return $packages;
95
    }
96
97 25
    protected function getRegisteredDefaultPackage($container)
98
    {
99 25
        $arguments = $this->getPackagesService($container)->getArguments();
100
101 25
        if (!isset($arguments[0])) {
102
            return;
103
        }
104
105 25
        return $arguments[0];
106
    }
107
}
108