Development

/branches/1.2/lib/config/sfProjectConfiguration.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/config/sfProjectConfiguration.class.php

Revision 27191, 15.7 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3, 1.4] fixed missing variable declaration in sfProjectCOnfiguration (closes #7949)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfProjectConfiguration represents a configuration for a symfony project.
13  *
14  * @package    symfony
15  * @subpackage config
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfProjectConfiguration
20 {
21   protected
22     $rootDir               = null,
23     $symfonyLibDir         = null,
24     $dispatcher            = null,
25     $plugins               = array('sfPropelPlugin'),
26     $pluginPaths           = array(),
27     $overriddenPluginPaths = array(),
28     $pluginConfigurations  = array(),
29     $pluginsLoaded         = false;
30
31   static protected
32     $active = null;
33
34   /**
35    * Constructor.
36    *
37    * @param string            $rootDir    The project root directory
38    * @param sfEventDispatcher $dispatcher The event dispatcher
39    */
40   public function __construct($rootDir = null, sfEventDispatcher $dispatcher = null)
41   {
42     if (is_null(sfProjectConfiguration::$active) || $this instanceof sfApplicationConfiguration)
43     {
44       sfProjectConfiguration::$active = $this;
45     }
46
47     $this->rootDir = is_null($rootDir) ? self::guessRootDir() : realpath($rootDir);
48     $this->symfonyLibDir = realpath(dirname(__FILE__).'/..');
49     $this->dispatcher = is_null($dispatcher) ? new sfEventDispatcher() : $dispatcher;
50
51     ini_set('magic_quotes_runtime', 'off');
52
53     sfConfig::set('sf_symfony_lib_dir', $this->symfonyLibDir);
54
55     $this->setRootDir($this->rootDir);
56
57     $this->setup();
58
59     $this->loadPlugins();
60   }
61
62   /**
63    * Setups the current configuration.
64    *
65    * Override this method if you want to customize your project configuration.
66    */
67   public function setup()
68   {
69   }
70
71   /**
72    * Loads the project's plugin configurations.
73    */
74   public function loadPlugins()
75   {
76     foreach ($this->getPluginPaths() as $path)
77     {
78       if (false === $plugin = array_search($path, $this->overriddenPluginPaths))
79       {
80         $plugin = basename($path);
81       }
82       $class = $plugin.'Configuration';
83
84       if (is_readable($file = sprintf('%s/config/%s.class.php', $path, $class)))
85       {
86         require_once $file;
87         $configuration = new $class($this, $path, $plugin);
88       }
89       else
90       {
91         $configuration = new sfPluginConfigurationGeneric($this, $path, $plugin);
92       }
93
94       $this->pluginConfigurations[$plugin] = $configuration;
95     }
96
97     $this->pluginsLoaded = true;
98   }
99
100   /**
101    * Sets the project root directory.
102    *
103    * @param string $rootDir The project root directory
104    */
105   public function setRootDir($rootDir)
106   {
107     $this->rootDir = $rootDir;
108
109     sfConfig::add(array(
110       'sf_root_dir' => $rootDir,
111
112       // global directory structure
113       'sf_apps_dir'    => $rootDir.DIRECTORY_SEPARATOR.'apps',
114       'sf_lib_dir'     => $rootDir.DIRECTORY_SEPARATOR.'lib',
115       'sf_log_dir'     => $rootDir.DIRECTORY_SEPARATOR.'log',
116       'sf_data_dir'    => $rootDir.DIRECTORY_SEPARATOR.'data',
117       'sf_config_dir'  => $rootDir.DIRECTORY_SEPARATOR.'config',
118       'sf_test_dir'    => $rootDir.DIRECTORY_SEPARATOR.'test',
119       'sf_doc_dir'     => $rootDir.DIRECTORY_SEPARATOR.'doc',
120       'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
121     ));
122
123     $this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
124     $this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
125   }
126
127   /**
128    * Returns the project root directory.
129    *
130    * @return string The project root directory
131    */
132   public function getRootDir()
133   {
134     return $this->rootDir;
135   }
136
137   /**
138    * Sets the cache root directory.
139    *
140    * @param string $cacheDir The absolute path to the cache dir.
141    */
142   public function setCacheDir($cacheDir)
143   {
144     sfConfig::set('sf_cache_dir', $cacheDir);
145   }
146
147   /**
148    * Sets the log directory.
149    *
150    * @param string $logDir The absolute path to the log dir.
151    */
152   public function setLogDir($logDir)
153   {
154     sfConfig::set('sf_log_dir', $logDir);
155   }
156
157   /**
158    * Sets the web root directory.
159    *
160    * @param string $webDir The absolute path to the web dir.
161    */
162   public function setWebDir($webDir)
163   {
164     sfConfig::add(array(
165       'sf_web_dir'    => $webDir,
166       'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.'uploads',
167     ));
168   }
169
170   /**
171    * Gets directories where model classes are stored. The order of returned paths is lowest precedence
172    * to highest precedence.
173    *
174    * @return array An array of directories
175    */
176   public function getModelDirs()
177   {
178     return array_merge(
179       $this->getPluginSubPaths('/lib/model'),     // plugins
180       array(sfConfig::get('sf_lib_dir').'/model') // project
181     );
182   }
183
184   /**
185    * Gets directories where template files are stored for a generator class and a specific theme.
186    *
187    * @param string $class The generator class name
188    * @param string $theme The theme name
189    *
190    * @return array An array of directories
191    */
192   public function getGeneratorTemplateDirs($class, $theme)
193   {
194     return array_merge(
195       array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'), // project
196       $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/template'),      // plugins
197       array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/template'),    // project (default theme)
198       $this->getPluginSubPaths('/data/generator/'.$class.'/default/template')          // plugins (default theme)
199     );
200   }
201
202   /**
203    * Gets directories where the skeleton is stored for a generator class and a specific theme.
204    *
205    * @param string $class The generator class name
206    * @param string $theme The theme name
207    *
208    * @return array An array of directories
209    */
210   public function getGeneratorSkeletonDirs($class, $theme)
211   {
212     return array_merge(
213       array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'), // project
214       $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/skeleton'),      // plugins
215       array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/skeleton'),    // project (default theme)
216       $this->getPluginSubPaths('/data/generator/'.$class.'/default/skeleton')          // plugins (default theme)
217     );
218   }
219
220   /**
221    * Gets the template to use for a generator class.
222    *
223    * @param string $class The generator class name
224    * @param string $theme The theme name
225    * @param string $path  The template path
226    *
227    * @return string A template path
228    *
229    * @throws sfException
230    */
231   public function getGeneratorTemplate($class, $theme, $path)
232   {
233     $dirs = $this->getGeneratorTemplateDirs($class, $theme);
234     foreach ($dirs as $dir)
235     {
236       if (is_readable($dir.'/'.$path))
237       {
238         return $dir.'/'.$path;
239       }
240     }
241
242     throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
243   }
244
245   /**
246    * Gets the configuration file paths for a given relative configuration path.
247    *
248    * @param string $configPath The configuration path
249    *
250    * @return array An array of paths
251    */
252   public function getConfigPaths($configPath)
253   {
254     $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
255
256     $files = array(
257       sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath,              // symfony
258     );
259
260     foreach ($this->getPluginPaths() as $path)
261     {
262       if (is_file($file = $path.'/'.$globalConfigPath))
263       {
264         $files[] = $file;                                                            // plugins
265       }
266     }
267
268     $files = array_merge($files, array(
269       sfConfig::get('sf_root_dir').'/'.$globalConfigPath,                            // project
270       sfConfig::get('sf_root_dir').'/'.$configPath,                                  // project
271     ));
272
273     foreach ($this->getPluginPaths() as $path)
274     {
275       if (is_file($file = $path.'/'.$configPath))
276       {
277         $files[] = $file;                                                            // plugins
278       }
279     }
280
281     $configs = array();
282     foreach (array_unique($files) as $file)
283     {
284       if (is_readable($file))
285       {
286         $configs[] = $file;
287       }
288     }
289
290     return $configs;
291   }
292
293   /**
294    * Sets the enabled plugins.
295    *
296    * @param array $plugins An array of plugin names
297    *
298    * @throws LogicException If plugins have already been loaded
299    */
300   public function setPlugins(array $plugins)
301   {
302     if ($this->pluginsLoaded)
303     {
304       throw new LogicException('Plugins have already been loaded.');
305     }
306
307     $this->plugins = $plugins;
308
309     $this->pluginPaths = array();
310   }
311
312   /**
313    * Enables a plugin or a list of plugins.
314    *
315    * @param array|string $plugins A plugin name or a plugin list
316    */
317   public function enablePlugins($plugins)
318   {
319     $this->setPlugins(array_merge($this->plugins, is_array($plugins) ? $plugins : array($plugins)));
320   }
321
322   /**
323    * Disables a plugin.
324    *
325    * @param array|string $plugins A plugin name or a plugin list
326    *
327    * @throws LogicException If plugins have already been loaded
328    */
329   public function disablePlugins($plugins)
330   {
331     if ($this->pluginsLoaded)
332     {
333       throw new LogicException('Plugins have already been loaded.');
334     }
335
336     if (!is_array($plugins))
337     {
338       $plugins = array($plugins);
339     }
340
341     foreach ($plugins as $plugin)
342     {
343       if (false !== $pos = array_search($plugin, $this->plugins))
344       {
345         unset($this->plugins[$pos]);
346       }
347       else
348       {
349         throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
350       }
351     }
352
353     $this->pluginPaths = array();
354   }
355
356   /**
357    * Enabled all installed plugins except the one given as argument.
358    *
359    * @param array|string $plugins A plugin name or a plugin list
360    *
361    * @throws LogicException If plugins have already been loaded
362    */
363   public function enableAllPluginsExcept($plugins = array())
364   {
365     if ($this->pluginsLoaded)
366     {
367       throw new LogicException('Plugins have already been loaded.');
368     }
369
370     $this->plugins = array();
371     foreach ($this->getAllPluginPaths() as $plugin => $path)
372     {
373       $this->plugins[] = $plugin;
374     }
375
376     $this->disablePlugins($plugins);
377   }
378
379   /**
380    * Gets the list of enabled plugins.
381    *
382    * @return array An array of enabled plugins
383    */
384   public function getPlugins()
385   {
386     return $this->plugins;
387   }
388
389   /**
390    * Gets the paths plugin sub-directories, minding overloaded plugins.
391    *
392    * @param string $subPath The subdirectory to look for
393    *
394    * @return array The plugin paths.
395    */
396   public function getPluginSubPaths($subPath = '')
397   {
398     if (array_key_exists($subPath, $this->pluginPaths))
399     {
400       return $this->pluginPaths[$subPath];
401     }
402
403     $this->pluginPaths[$subPath] = array();
404     $pluginPaths = $this->getPluginPaths();
405     foreach ($pluginPaths as $pluginPath)
406     {
407       if (is_dir($pluginPath.$subPath))
408       {
409         $this->pluginPaths[$subPath][] = $pluginPath.$subPath;
410       }
411     }
412
413     return $this->pluginPaths[$subPath];
414   }
415
416   /**
417    * Gets the paths to plugins root directories, minding overloaded plugins.
418    *
419    * @return array The plugin root paths.
420    */
421   public function getPluginPaths()
422   {
423     if (array_key_exists('', $this->pluginPaths))
424     {
425       return $this->pluginPaths[''];
426     }
427
428     $pluginPaths = $this->getAllPluginPaths();
429
430     $this->pluginPaths[''] = array();
431     foreach ($this->getPlugins() as $plugin)
432     {
433       if (isset($pluginPaths[$plugin]))
434       {
435         $this->pluginPaths[''][] = $pluginPaths[$plugin];
436       }
437       else
438       {
439         throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
440       }
441     }
442
443     return $this->pluginPaths[''];
444   }
445
446   /**
447    * Returns an array of paths for all available plugins.
448    *
449    * @return array
450    */
451   public function getAllPluginPaths()
452   {
453     $pluginPaths = array();
454
455     $finder = sfFinder::type('dir')->maxdepth(0)->follow_link()->name('*Plugin');
456     $dirs = array(
457       sfConfig::get('sf_symfony_lib_dir').'/plugins',
458       sfConfig::get('sf_plugins_dir'),
459     );
460
461     foreach ($finder->in($dirs) as $path)
462     {
463       $pluginPaths[basename($path)] = $path;
464     }
465
466     foreach ($this->overriddenPluginPaths as $plugin => $path)
467     {
468       $pluginPaths[$plugin] = $path;
469     }
470
471     return $pluginPaths;
472   }
473
474   /**
475    * Manually sets the location of a particular plugin.
476    *
477    * This method can be used to ease functional testing of plugins. It is not
478    * intended to support sharing plugins between projects, as many plugins
479    * save project specific code (to /lib/form/base, for example).
480    *
481    * @param string $plugin
482    * @param string $path
483    */
484   public function setPluginPath($plugin, $path)
485   {
486     $this->overriddenPluginPaths[$plugin] = realpath($path);
487   }
488
489   /**
490    * Returns the configuration for the requested plugin.
491    *
492    * @param string $name
493    *
494    * @return sfPluginConfiguration
495    */
496   public function getPluginConfiguration($name)
497   {
498     if (!isset($this->pluginConfigurations[$name]))
499     {
500       throw new InvalidArgumentException(sprintf('There is no configuration object for the "%s" object.', $name));
501     }
502
503     return $this->pluginConfigurations[$name];
504   }
505
506   /**
507    * Returns the event dispatcher.
508    *
509    * @return sfEventDispatcher A sfEventDispatcher instance
510    */
511   public function getEventDispatcher()
512   {
513     return $this->dispatcher;
514   }
515
516   /**
517    * Returns the symfony lib directory.
518    *
519    * @return string The symfony lib directory
520    */
521   public function getSymfonyLibDir()
522   {
523     return $this->symfonyLibDir;
524   }
525
526   /**
527    * Returns the active configuration.
528    *
529    * @return sfProjectConfiguration The current sfProjectConfiguration instance
530    */
531   static public function getActive()
532   {
533     if (is_null(sfProjectConfiguration::$active))
534     {
535       throw new RuntimeException('There is no active configuration.');
536     }
537
538     return sfProjectConfiguration::$active;
539   }
540
541   /**
542    * Guesses the project root directory.
543    *
544    * @return string The project root directory
545    */
546   static public function guessRootDir()
547   {
548     $r = new ReflectionClass('ProjectConfiguration');
549
550     return realpath(dirname($r->getFileName()).'/..');
551   }
552
553   /**
554    * Returns a sfApplicationConfiguration configuration for a given application.
555    *
556    * @param string            $application An application name
557    * @param string            $environment The environment name
558    * @param Boolean           $debug       true to enable debug mode
559    * @param string            $rootDir     The project root directory
560    * @param sfEventDispatcher $dispatcher  An event dispatcher
561    *
562    * @return sfApplicationConfiguration A sfApplicationConfiguration instance
563    */
564   static public function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
565   {
566     $class = $application.'Configuration';
567
568     if (is_null($rootDir))
569     {
570       $rootDir = self::guessRootDir();
571     }
572
573     if (!file_exists($file = $rootDir.'/apps/'.$application.'/config/'.$class.'.class.php'))
574     {
575       throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
576     }
577
578     require_once $file;
579
580     return new $class($environment, $debug, $rootDir, $dispatcher);
581   }
582
583   /**
584    * Calls methods defined via sfEventDispatcher.
585    *
586    * @param string $method The method name
587    * @param array  $arguments The method arguments
588    *
589    * @return mixed The returned value of the called method
590    */
591   public function __call($method, $arguments)
592   {
593     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', array('method' => $method, 'arguments' => $arguments)));
594     if (!$event->isProcessed())
595     {
596       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
597     }
598
599     return $event->getReturnValue();
600   }
601 }
602
Note: See TracBrowser for help on using the browser.