StaticStub::compileInitialization()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
ccs 0
cts 35
cp 0
rs 8.8571
cc 2
eloc 28
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * For licensing information, please see the LICENSE file accompanied with this file.
4
 *
5
 * @author Gerard van Helden <[email protected]>
6
 * @copyright 2012 Gerard van Helden <http://melp.nl>
7
 */
8
9
namespace Zicht\Tool\Packager\Node;
10
11
use Symfony\Component\Config\FileLocator;
12
use Zicht\Tool\Configuration;
13
use Zicht\Tool\Container\ContainerCompiler;
14
use Zicht\Tool\Script\Buffer;
15
16
/**
17
 * PHAR Stub implementation for a static build
18
 */
19
class StaticStub extends Stub
20
{
21
    /**
22
     * Construct the stub with the specified details
23
     *
24
     * @param \Phar $phar
25
     * @param string $appName
26
     * @param string $appVersion
27
     * @param array $staticConfig
28
     * @param array $staticPluginPaths
29
     */
30
    public function __construct(\Phar $phar, $appName, $appVersion, $staticConfig, $staticPluginPaths)
31
    {
32
        parent::__construct($phar, $appName, $appVersion);
33
34
        $this->staticConfig = $staticConfig;
0 ignored issues
show
Bug introduced by
The property staticConfig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        $this->staticPluginPaths = $staticPluginPaths;
0 ignored issues
show
Bug introduced by
The property staticPluginPaths does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
    }
37
38
39
    /**
40
     * Writes the initialization code for a static build of Z.
41
     *
42
     * @param \Zicht\Tool\Script\Buffer $buffer
43
     * @return mixed|void
44
     */
45
    protected function compileInitialization(Buffer $buffer)
46
    {
47
        $configurationLoader = new Configuration\ConfigurationLoader(
48
            $this->staticConfig,
49
            new FileLocator(array(getcwd())),
50
            new Configuration\FileLoader(
51
                new Configuration\PathDefaultFileLocator(
52
                    'ZPLUGINPATH',
53
                    $this->staticPluginPaths
54
                )
55
            )
56
        );
57
58
        $compiler = new ContainerCompiler($configurationLoader->processConfiguration(), array());
0 ignored issues
show
Bug introduced by
The call to ContainerCompiler::__construct() misses a required argument $file.

This check looks for function calls that miss required arguments.

Loading history...
59
        $this->phar['container.php'] = $compiler->compileContainerCode();
60
        $buffer->writeln('$container = require_once \'phar://z.phar/container.php\';');
61
62
        foreach ($configurationLoader->getPlugins() as $name => $plugin) {
63
            $className = get_class($plugin);
64
            $embeddedFilename = 'plugins/' . $name . '.php';
65
66
            $class = new \ReflectionClass($className);
67
68
            $this->phar[$embeddedFilename] = file_get_contents($class->getFileName());
69
            $buffer->write('require_once ')->asPhp('phar://z.phar/' . $embeddedFilename)->raw(';')->eol();
70
            $buffer->write('$p = new ')->write($className)->raw('();')->eol();
71
            $buffer->writeln('$p->setContainer($container);');
72
        }
73
        $buffer
74
            ->writeln('Zicht\Tool\Application::$HEADER = \'\';')
75
            ->write('$app = new Zicht\Tool\Application(')
76
            ->asPhp($this->appName)
77
            ->raw(', Zicht\Version\Version::fromString(')
78
            ->asPhp($this->appVersion)
79
            ->raw(') ?: new Zicht\Version\Version());')
80
            ->eol()
81
        ;
82
        $buffer->writeln('$app->setContainer($container);');
83
    }
84
}
85