-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfProjectConfiguration.class.php
608 lines (522 loc) · 17.6 KB
/
sfProjectConfiguration.class.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfProjectConfiguration represents a configuration for a symfony project.
*
* @author Fabien Potencier <[email protected]>
*/
class sfProjectConfiguration
{
/** @var string */
protected $rootDir;
/** @var string */
protected $symfonyLibDir;
/** @var sfEventDispatcher */
protected $dispatcher;
/** @var array */
protected $plugins = [];
/** @var array */
protected $pluginPaths = [];
/** @var array */
protected $overriddenPluginPaths = [];
/** @var sfPluginConfiguration[] */
protected $pluginConfigurations = [];
/** @var bool */
protected $pluginsLoaded = false;
/** @var sfApplicationConfiguration */
protected static $active;
/**
* Constructor.
*
* @param string $rootDir The project root directory
* @param sfEventDispatcher $dispatcher The event dispatcher
*/
public function __construct($rootDir = null, ?sfEventDispatcher $dispatcher = null)
{
if (null === self::$active || $this instanceof sfApplicationConfiguration) {
self::$active = $this;
}
$this->rootDir = null === $rootDir ? static::guessRootDir() : realpath($rootDir);
$this->symfonyLibDir = realpath(__DIR__.'/..');
$this->dispatcher = $dispatcher ?? new sfEventDispatcher();
ini_set('magic_quotes_runtime', 'off');
sfConfig::set('sf_symfony_lib_dir', $this->symfonyLibDir);
$this->setRootDir($this->rootDir);
// provide forms the dispatcher
sfFormSymfony::setEventDispatcher($this->dispatcher);
$this->setup();
$this->loadPlugins();
$this->setupPlugins();
}
/**
* Calls methods defined via sfEventDispatcher.
*
* @param string $method The method name
* @param array $arguments The method arguments
*
* @return mixed The returned value of the called method
*
* @throws sfException
*/
public function __call($method, $arguments)
{
$event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', ['method' => $method, 'arguments' => $arguments]));
if (!$event->isProcessed()) {
throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
}
return $event->getReturnValue();
}
/**
* Setups the current configuration.
*
* Override this method if you want to customize your project configuration.
*/
public function setup()
{
}
/**
* Loads the project's plugin configurations.
*/
public function loadPlugins()
{
foreach ($this->getPluginPaths() as $path) {
if (false === $plugin = array_search($path, $this->overriddenPluginPaths)) {
$plugin = basename($path);
}
$class = $plugin.'Configuration';
if (is_readable($file = sprintf('%s/config/%s.class.php', $path, $class))) {
require_once $file;
$configuration = new $class($this, $path, $plugin);
} else {
$configuration = new sfPluginConfigurationGeneric($this, $path, $plugin);
}
$this->pluginConfigurations[$plugin] = $configuration;
}
$this->pluginsLoaded = true;
}
/**
* Sets up plugin configurations.
*
* Override this method if you want to customize plugin configurations.
*/
public function setupPlugins()
{
}
/**
* Sets the project root directory.
*
* @param string $rootDir The project root directory
*/
public function setRootDir($rootDir)
{
$this->rootDir = $rootDir;
sfConfig::add([
'sf_root_dir' => $rootDir,
// global directory structure
'sf_apps_dir' => $rootDir.DIRECTORY_SEPARATOR.'apps',
'sf_lib_dir' => $rootDir.DIRECTORY_SEPARATOR.'lib',
'sf_log_dir' => $rootDir.DIRECTORY_SEPARATOR.'log',
'sf_data_dir' => $rootDir.DIRECTORY_SEPARATOR.'data',
'sf_config_dir' => $rootDir.DIRECTORY_SEPARATOR.'config',
'sf_test_dir' => $rootDir.DIRECTORY_SEPARATOR.'test',
'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
]);
$this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
$this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
}
/**
* Returns the project root directory.
*
* @return string The project root directory
*/
public function getRootDir()
{
return $this->rootDir;
}
/**
* Sets the cache root directory.
*
* @param string $cacheDir the absolute path to the cache dir
*/
public function setCacheDir($cacheDir)
{
sfConfig::set('sf_cache_dir', $cacheDir);
}
/**
* Sets the log directory.
*
* @param string $logDir the absolute path to the log dir
*/
public function setLogDir($logDir)
{
sfConfig::set('sf_log_dir', $logDir);
}
/**
* Sets the web root directory.
*
* @param string $webDir the absolute path to the web dir
*/
public function setWebDir($webDir)
{
sfConfig::add([
'sf_web_dir' => $webDir,
'sf_upload_dir_name' => $uploadDirName = 'uploads',
'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.$uploadDirName,
]);
}
/**
* Gets directories where model classes are stored. The order of returned paths is lowest precedence
* to highest precedence.
*
* @return array An array of directories
*/
public function getModelDirs()
{
return array_merge(
$this->getPluginSubPaths('/lib/model'), // plugins
[sfConfig::get('sf_lib_dir').'/model'] // project
);
}
/**
* Gets directories where template files are stored for a generator class and a specific theme.
*
* @param string $class The generator class name
* @param string $theme The theme name
*
* @return array An array of directories
*/
public function getGeneratorTemplateDirs($class, $theme)
{
return array_merge(
[sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'], // project
$this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/template'), // plugins
[sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/template'], // project (default theme)
$this->getPluginSubPaths('/data/generator/'.$class.'/default/template') // plugins (default theme)
);
}
/**
* Gets directories where the skeleton is stored for a generator class and a specific theme.
*
* @param string $class The generator class name
* @param string $theme The theme name
*
* @return array An array of directories
*/
public function getGeneratorSkeletonDirs($class, $theme)
{
return array_merge(
[sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'], // project
$this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/skeleton'), // plugins
[sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/skeleton'], // project (default theme)
$this->getPluginSubPaths('/data/generator/'.$class.'/default/skeleton') // plugins (default theme)
);
}
/**
* Gets the template to use for a generator class.
*
* @param string $class The generator class name
* @param string $theme The theme name
* @param string $path The template path
*
* @return string A template path
*
* @throws sfException
*/
public function getGeneratorTemplate($class, $theme, $path)
{
$dirs = $this->getGeneratorTemplateDirs($class, $theme);
foreach ($dirs as $dir) {
if (is_readable($dir.'/'.$path)) {
return $dir.'/'.$path;
}
}
throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
}
/**
* Gets the configuration file paths for a given relative configuration path.
*
* @param string $configPath The configuration path
*
* @return array An array of paths
*/
public function getConfigPaths($configPath)
{
$globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
$files = [
$this->getSymfonyLibDir().'/config/'.$globalConfigPath, // symfony
];
foreach ($this->getPluginPaths() as $path) {
if (is_file($file = $path.'/'.$globalConfigPath)) {
$files[] = $file; // plugins
}
}
$files = array_merge($files, [
$this->getRootDir().'/'.$globalConfigPath, // project
$this->getRootDir().'/'.$configPath, // project
]);
foreach ($this->getPluginPaths() as $path) {
if (is_file($file = $path.'/'.$configPath)) {
$files[] = $file; // plugins
}
}
$configs = [];
foreach (array_unique($files) as $file) {
if (is_readable($file)) {
$configs[] = $file;
}
}
return $configs;
}
/**
* Sets the enabled plugins.
*
* @param array $plugins An array of plugin names
*
* @throws LogicException If plugins have already been loaded
*/
public function setPlugins(array $plugins)
{
if ($this->pluginsLoaded) {
throw new LogicException('Plugins have already been loaded.');
}
$this->plugins = $plugins;
$this->pluginPaths = [];
}
/**
* Enables a plugin or a list of plugins.
*
* @param array|string $plugins A plugin name or a plugin list
*/
public function enablePlugins($plugins)
{
if (!is_array($plugins)) {
if (func_num_args() > 1) {
$plugins = func_get_args();
} else {
$plugins = [$plugins];
}
}
$this->setPlugins(array_merge($this->plugins, $plugins));
}
/**
* Disables a plugin.
*
* @param array|string $plugins A plugin name or a plugin list
*
* @throws LogicException If plugins have already been loaded
*/
public function disablePlugins($plugins)
{
if ($this->pluginsLoaded) {
throw new LogicException('Plugins have already been loaded.');
}
if (!is_array($plugins)) {
$plugins = [$plugins];
}
foreach ($plugins as $plugin) {
if (false !== $pos = array_search($plugin, $this->plugins)) {
unset($this->plugins[$pos]);
} else {
throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
}
}
$this->pluginPaths = [];
}
/**
* Enabled all installed plugins except the one given as argument.
*
* @param array|string $plugins A plugin name or a plugin list
*
* @throws LogicException If plugins have already been loaded
*/
public function enableAllPluginsExcept($plugins = [])
{
if ($this->pluginsLoaded) {
throw new LogicException('Plugins have already been loaded.');
}
$this->plugins = array_keys($this->getAllPluginPaths());
sort($this->plugins);
$this->disablePlugins($plugins);
}
/**
* Gets the list of enabled plugins.
*
* @return array An array of enabled plugins
*/
public function getPlugins()
{
return $this->plugins;
}
/**
* Gets the paths plugin sub-directories, minding overloaded plugins.
*
* @param string $subPath The subdirectory to look for
*
* @return array the plugin paths
*/
public function getPluginSubPaths($subPath = '')
{
if (array_key_exists($subPath, $this->pluginPaths)) {
return $this->pluginPaths[$subPath];
}
$this->pluginPaths[$subPath] = [];
$pluginPaths = $this->getPluginPaths();
foreach ($pluginPaths as $pluginPath) {
if (is_dir($pluginPath.$subPath)) {
$this->pluginPaths[$subPath][] = $pluginPath.$subPath;
}
}
return $this->pluginPaths[$subPath];
}
/**
* Gets the paths to plugins root directories, minding overloaded plugins.
*
* @return array the plugin root paths
*
* @throws InvalidArgumentException If an enabled plugin does not exist
*/
public function getPluginPaths()
{
if (!isset($this->pluginPaths[''])) {
$pluginPaths = $this->getAllPluginPaths();
$this->pluginPaths[''] = [];
foreach ($this->getPlugins() as $plugin) {
if (isset($pluginPaths[$plugin])) {
$this->pluginPaths[''][] = $pluginPaths[$plugin];
} else {
throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
}
}
}
return $this->pluginPaths[''];
}
/**
* Returns an array of paths for all available plugins.
*
* @return array
*/
public function getAllPluginPaths()
{
$pluginPaths = [];
// search for *Plugin directories representing plugins
// follow links and do not recurse. No need to exclude VC because they do not end with *Plugin
$finder = sfFinder::type('dir')->maxdepth(0)->ignore_version_control(false)->follow_link()->name('*Plugin');
$dirs = [
$this->getSymfonyLibDir().'/plugins',
sfConfig::get('sf_plugins_dir'),
];
foreach ($finder->in($dirs) as $path) {
$pluginPaths[basename($path)] = $path;
}
foreach ($this->overriddenPluginPaths as $plugin => $path) {
$pluginPaths[$plugin] = $path;
}
return $pluginPaths;
}
/**
* Manually sets the location of a particular plugin.
*
* This method can be used to ease functional testing of plugins. It is not
* intended to support sharing plugins between projects, as many plugins
* save project specific code (to /lib/form/base, for example).
*
* @param string $plugin
* @param string $path
*/
public function setPluginPath($plugin, $path)
{
$this->overriddenPluginPaths[$plugin] = realpath($path);
}
/**
* Returns the configuration for the requested plugin.
*
* @param string $name
*
* @return sfPluginConfiguration
*/
public function getPluginConfiguration($name)
{
if (!isset($this->pluginConfigurations[$name])) {
throw new InvalidArgumentException(sprintf('There is no configuration object for the "%s" object.', $name));
}
return $this->pluginConfigurations[$name];
}
/**
* Returns the event dispatcher.
*
* @return sfEventDispatcher A sfEventDispatcher instance
*/
public function getEventDispatcher()
{
return $this->dispatcher;
}
/**
* Returns the symfony lib directory.
*
* @return string The symfony lib directory
*/
public function getSymfonyLibDir()
{
return $this->symfonyLibDir;
}
/**
* Returns the active configuration.
*
* @return sfApplicationConfiguration The current sfProjectConfiguration instance
*/
public static function getActive()
{
if (!static::hasActive()) {
throw new RuntimeException('There is no active configuration.');
}
return self::$active;
}
/**
* Returns true if these is an active configuration.
*
* @return bool
*/
public static function hasActive()
{
return null !== self::$active;
}
/**
* Guesses the project root directory.
*
* @return string The project root directory
*/
public static function guessRootDir()
{
$r = new ReflectionClass('ProjectConfiguration');
return realpath(dirname($r->getFileName()).'/..');
}
/**
* Returns a sfApplicationConfiguration configuration for a given application.
*
* @param string $application An application name
* @param string $environment The environment name
* @param bool $debug true to enable debug mode
* @param string $rootDir The project root directory
* @param sfEventDispatcher $dispatcher An event dispatcher
*
* @return sfApplicationConfiguration A sfApplicationConfiguration instance
*/
public static function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, ?sfEventDispatcher $dispatcher = null)
{
$class = $application.'Configuration';
if (null === $rootDir) {
$rootDir = static::guessRootDir();
}
if (!is_file($file = $rootDir.'/apps/'.$application.'/config/'.$class.'.class.php')) {
throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
}
require_once $file;
return new $class($environment, $debug, $rootDir, $dispatcher);
}
}