This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
forked from naderman/PyrusBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyrus.php
239 lines (205 loc) · 7.07 KB
/
Pyrus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/*
* This file is part of phpBB.
*
* (c) phpBB Ltd.
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace phpBB\PyrusBundle;
//use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\Container;
/**
* Provides simplified access to Pyrus in symfony environment
*/
class Pyrus
{
protected $container;
protected $frontend;
protected $config;
/**
* Sets Pyrus up for later use
*
* @param Container $container the dependency injection container
*/
public function __construct(Container $container, \PEAR2\Pyrus\ScriptFrontend\Commands $frontend = null)
{
$this->container = $container;
if (!file_exists($this->getPyrusDir())) {
mkdir($this->getPyrusDir(), 0777, true);
}
if (!$frontend) {
if (file_exists(__DIR__ . '/../../../../../PEAR2_Pyrus')) {
spl_autoload_register(array($this, 'pyrus_src_autoload'));
}
spl_autoload_register(array($this, 'pyrus_autoload'));
$frontend = new \PEAR2\Pyrus\ScriptFrontend\Commands;
}
$this->frontend = $frontend;
$this->readConfig(\PEAR2\Pyrus\Config::singleton($this->getPyrusDir(), $this->getPearConfig()));
$this->setupPyrus();
}
public function readConfig()
{
$this->config = \PEAR2\Pyrus\Config::singleton($this->getPyrusDir(), $this->getPearConfig());
$this->config->pluginregistry->scan();
}
public function setupPyrus()
{
if (!file_exists($this->getPearConfig())) {
$this->config->saveConfig($this->getPearConfig());
$this->readConfig();
// $this->installCustomRole();
}
// reset these paths every time, to allow moving the symfony directory
// around
// TODO: replace with configurable parameters
$root = $this->container->getParameter('kernel.root_dir');
$settings = array(
//'ext_dir' => '@php_dir@/pyrus/ext',
//'doc_dir' => '@php_dir@/pyrus/docs',
'bin_dir' => $root . '/bin',
//'data_dir' => '@php_dir@/pyrus/data',
//'cfg_dir' => '@php_dir@/pyrus/cfg',
'www_dir' => $root . '/web',
//'test_dir' => '@php_dir@/pyrus/tests',
//'src_dir' => '@php_dir@/pyrus/src',
'auto_discover' => 1,
//'cache_dir' => '@php_dir@/pyrus/cache',
//'temp_dir' => '@php_dir@/pyrus/temp',
//'download_dir' => '@php_dir@/pyrus/downloads',
//'plugins_dir' => '@default_config_dir@',
//'bundle_dir' => $this->getBundleDir(),
);
$options = array('plugin' => false);
foreach ($settings as $name => $desiredValue) {
$currentValue = $this->getConfig($name);
if ($currentValue != $desiredValue) {
$this->setConfig($name, $desiredValue);
}
}
$this->readConfig();
}
/**
* Retrieves a value from the Pyrus configuration.
*
* @param string $name The variable name
* @return mixed The configuration value
*/
public function getConfig($name)
{
$this->config = \PEAR2\Pyrus\Config::current();
return $this->config->__get($name);
}
/**
* Overwrites a configuration value.
*
* @param string $name The variable name
* @param mixed $value New value
*/
public function setConfig($name, $value)
{
$this->config->__set($name, $value);
$this->config->saveConfig($this->getPearConfig());
}
public function installCustomRole()
{
$pf = new \PEAR2\Pyrus\PackageFile\v2;
$pf->name = 'PyrusBundlePlugin';
$pf->channel = 'pear2.php.net';
$pf->summary = 'Local';
$pf->description = 'Description';
$pf->notes = 'My Notes';
$pf->maintainer['naderman']->role('lead')->email('[email protected]')->active('yes')->name('Nils Adermann');
$pf->files['Bundle.xml'] = array(
'attribs' => array('role' => 'customrole'),
);
$pf->files['PyrusBundlePlugin/Role/Bundle.php'] = array(
'attribs' => array('role' => 'php'),
);
$package_xml = __DIR__ . '/plugin/package.xml';
$pf->setPackagefile($package_xml);
$package = new \PEAR2\Pyrus\Package(false);
$xmlcontainer = new \PEAR2\Pyrus\PackageFile($pf);
$xml = new \PEAR2\Pyrus\Package\Xml($package_xml, $package, $xmlcontainer);
$package->setInternalPackage($xml);
\PEAR2\Pyrus\Main::$options['install-plugins'] = true;
\PEAR2\Pyrus\Installer::begin();
\PEAR2\Pyrus\Installer::prepare($package);
\PEAR2\Pyrus\Installer::commit();
$this->readConfig();
}
/**
* Returns the directory used for Pyrus packages
*
* @return string The vendor directory
*/
public function getPyrusDir()
{
return $this->container->getParameter('kernel.root_dir') . "/../vendor/pear2";
}
/**
* Retrieve path to pearconfig.xml user configuration
*
* @return string Path to pearconfig.xml
*/
public function getPearConfig()
{
return $this->getPyrusDir() . '/pearconfig.xml';
}
/**
* Returns the directory to be used for Pyrus installed Bundles.
*
* @return string The bundle directory
*/
public function getBundleDir()
{
throw new \Exception("TODO!");
$bundleDirs = getBundleDirs();
if (!isset($bundleDirs['Bundle'])) {
throw new \RunTimeException("No BundleDir for namespace Bundle defined in Application Kernel.");
}
return $bundleDirs['Bundle'];
}
/**
* Executes a Pyrus command through its CLI frontend in the bundle directory.
*
* @param array $argv The list of options and arguments, not including the
* executing script.
*/
public function run(array $argv)
{
array_unshift($argv, $this->getPyrusDir());
$this->frontend->run($argv);
}
/**
* The Pyrus autoloader taking classes from the phar file within this Bundle.
*
* @param string $class The class name
*/
public function pyrus_autoload($class)
{
$class = str_replace(array('_','\\'), '/', $class);
$path = 'phar://' . __DIR__ . '/pyrus.phar/PEAR2_Pyrus-2.0.0a2/php/' . $class . '.php';
if (file_exists($path)) {
include $path;
} else {
$path = $this->getPyrusDir() . '/php/' . $class . '.php';
include $path;
}
}
/**
* Pyrus autoloader for source package of Pyrus, useful for debugging.
*
* @param string $class The class name
*/
public function pyrus_src_autoload($class)
{
$class = str_replace(array('_','\\'), '/', $class);
$path = __DIR__ . '/../../../../../PEAR2_Pyrus/src/' . str_replace('PEAR2/', '', $class) . '.php';
if (file_exists($path)) {
include $path;
}
}
}