|
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; |
|
|
|
|
|
|
35
|
|
|
$this->staticPluginPaths = $staticPluginPaths; |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: