Development

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

You must first sign up to be able to contribute.

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

Revision 15805, 10.2 kB (checked in by fabien, 5 years ago)

[1.1, 1.2, 1.3] fixed register_globals cannot be set at runtime (closes #5922)

  • 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
25   static protected
26     $active = null;
27
28   /**
29    * Constructor.
30    *
31    * @param string              $rootDir    The project root directory
32    * @param sfEventDispatcher   $dispatcher The event dispatcher
33    */
34   public function __construct($rootDir = null, sfEventDispatcher $dispatcher = null)
35   {
36     if (is_null(sfProjectConfiguration::$active) || $this instanceof sfApplicationConfiguration)
37     {
38       sfProjectConfiguration::$active = $this;
39     }
40
41     $this->rootDir = is_null($rootDir) ? self::guessRootDir() : realpath($rootDir);
42
43     $this->symfonyLibDir = realpath(dirname(__FILE__).'/..');
44
45     // initializes autoloading for symfony core classes
46     require_once $this->symfonyLibDir.'/autoload/sfCoreAutoload.class.php';
47     sfCoreAutoload::register();
48
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
60   /**
61    * Setups the current configuration.
62    *
63    * Override this method if you want to customize your project configuration.
64    */
65   public function setup()
66   {
67   }
68
69   /**
70    * Sets the project root directory.
71    *
72    * @param string $rootDir The project root directory
73    */
74   public function setRootDir($rootDir)
75   {
76     $this->rootDir = $rootDir;
77
78     sfConfig::add(array(
79       'sf_root_dir' => $rootDir,
80
81       // global directory structure
82       'sf_apps_dir'    => $rootDir.DIRECTORY_SEPARATOR.'apps',
83       'sf_lib_dir'     => $rootDir.DIRECTORY_SEPARATOR.'lib',
84       'sf_log_dir'     => $rootDir.DIRECTORY_SEPARATOR.'log',
85       'sf_data_dir'    => $rootDir.DIRECTORY_SEPARATOR.'data',
86       'sf_config_dir'  => $rootDir.DIRECTORY_SEPARATOR.'config',
87       'sf_test_dir'    => $rootDir.DIRECTORY_SEPARATOR.'test',
88       'sf_doc_dir'     => $rootDir.DIRECTORY_SEPARATOR.'doc',
89       'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
90     ));
91
92     $this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
93     $this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
94   }
95
96   /**
97    * Returns the project root directory.
98    *
99    * @return string The project root directory
100    */
101   public function getRootDir()
102   {
103     return $this->rootDir;
104   }
105
106   /**
107    * Sets the cache root directory.
108    *
109    * @param string $cacheDir The absolute path to the cache dir.
110    */
111   public function setCacheDir($cacheDir)
112   {
113     sfConfig::set('sf_cache_dir', $cacheDir);
114   }
115
116   /**
117    * Sets the log directory.
118    *
119    * @param string $logDir The absolute path to the log dir.
120    */
121   public function setLogDir($logDir)
122   {
123     sfConfig::set('sf_log_dir', $logDir);
124   }
125
126   /**
127    * Sets the web root directory.
128    *
129    * @param string $webDir The absolute path to the web dir.
130    */
131   public function setWebDir($webDir)
132   {
133     sfConfig::add(array(
134       'sf_web_dir'    => $webDir,
135       'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.'uploads',
136     ));
137   }
138
139   /**
140    * Gets directories where model classes are stored. The order of returned paths is lowest precedence
141    * to highest precedence.
142    *
143    * @return array An array of directories
144    */
145   public function getModelDirs()
146   {
147     $dirs = array();
148
149     if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model'))                                   // plugins
150     {
151       $dirs = array_merge($dirs, $pluginDirs);
152     }
153
154     $dirs[] = sfConfig::get('sf_lib_dir').'/model';                                                           // project
155
156     return $dirs;
157   }
158
159   /**
160    * Gets directories where template files are stored for a generator class and a specific theme.
161    *
162    * @param string $class  The generator class name
163    * @param string $theme  The theme name
164    *
165    * @return array An array of directories
166    */
167   public function getGeneratorTemplateDirs($class, $theme)
168   {
169     $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template');                  // project
170
171     if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/template'))
172     {
173       $dirs = array_merge($dirs, $pluginDirs);                                                                // plugin
174     }
175
176     if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/template'))
177     {
178       $dirs = array_merge($dirs, $bundledPluginDirs);                                                         // bundled plugin
179     }
180
181     return $dirs;
182   }
183
184   /**
185    * Gets directories where the skeleton is 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 getGeneratorSkeletonDirs($class, $theme)
193   {
194     $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton');                  // project
195
196     if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
197     {
198       $dirs = array_merge($dirs, $pluginDirs);                                                                // plugin
199     }
200
201     if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
202     {
203       $dirs = array_merge($dirs, $bundledPluginDirs);                                                         // bundled plugin
204     }
205
206     return $dirs;
207   }
208
209   /**
210    * Gets the template to use for a generator class.
211    *
212    * @param string $class   The generator class name
213    * @param string $theme   The theme name
214    * @param string $path    The template path
215    *
216    * @return string A template path
217    *
218    * @throws sfException
219    */
220   public function getGeneratorTemplate($class, $theme, $path)
221   {
222     $dirs = $this->getGeneratorTemplateDirs($class, $theme);
223     foreach ($dirs as $dir)
224     {
225       if (is_readable($dir.'/'.$path))
226       {
227         return $dir.'/'.$path;
228       }
229     }
230
231     throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
232   }
233
234   /**
235    * Gets the paths to plugins root directories, minding overloaded plugins.
236    *
237    * @return array The plugin root paths.
238    */
239   public function getPluginPaths()
240   {
241     $pluginPaths = array();
242
243     $finder = sfFinder::type('dir')->maxdepth(0)->follow_link()->relative();
244
245     $bundledPlugins = $finder->discard('.*')->prune('.*')->in(sfConfig::get('sf_symfony_lib_dir').'/plugins');
246     $projectPlugins = $finder->discard('.*')->prune('.*')->in(sfConfig::get('sf_plugins_dir'));
247
248     // bundled plugins
249     foreach ($bundledPlugins as $plugin)
250     {
251       // plugins can override bundle plugins
252       if (false !== $pos = array_search($plugin, $projectPlugins))
253       {
254         $pluginPaths[] = sfConfig::get('sf_plugins_dir').'/'.$plugin;
255         unset($projectPlugins[$pos]);
256       }
257       else
258       {
259         $pluginPaths[] = sfConfig::get('sf_symfony_lib_dir').'/plugins/'.$plugin;
260       }
261     }
262
263     // project plugins
264     foreach ($projectPlugins as $plugin)
265     {
266       $pluginPaths[] = sfConfig::get('sf_plugins_dir').'/'.$plugin;
267     }
268
269     return $pluginPaths;
270   }
271
272   /**
273    * Returns the event dispatcher.
274    *
275    * @return sfEventDispatcher A sfEventDispatcher instance
276    */
277   public function getEventDispatcher()
278   {
279     return $this->dispatcher;
280   }
281
282   /**
283    * Returns the symfony lib directory.
284    *
285    * @return string The symfony lib directory
286    */
287   public function getSymfonyLibDir()
288   {
289     return $this->symfonyLibDir;
290   }
291
292   /**
293    * Returns the active configuration.
294    *
295    * @return sfProjectConfiguration The current sfProjectConfiguration instance
296    */
297   static public function getActive()
298   {
299     if (is_null(sfProjectConfiguration::$active))
300     {
301       throw new RuntimeException('There is no active configuration.');
302     }
303
304     return sfProjectConfiguration::$active;
305   }
306
307   static public function guessRootDir()
308   {
309     $r = new ReflectionClass('ProjectConfiguration');
310
311     return realpath(dirname($r->getFileName()).'/..');
312   }
313
314   /**
315    * Returns a sfApplicationConfiguration configuration for a given application.
316    *
317    * @param string            $application    An application name
318    * @param string            $environment    The environment name
319    * @param Boolean           $debug          true to enable debug mode
320    * @param string            $rootDir        The project root directory
321    * @param sfEventDispatcher $dispatcher     An event dispatcher
322    *
323    * @return sfApplicationConfiguration A sfApplicationConfiguration instance
324    */
325   static public function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
326   {
327     $class = $application.'Configuration';
328
329     if (is_null($rootDir))
330     {
331       $rootDir = self::guessRootDir();
332     }
333
334     if (!file_exists($file = $rootDir.'/apps/'.$application.'/config/'.$class.'.class.php'))
335     {
336       throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
337     }
338
339     require_once $file;
340
341     return new $class($environment, $debug, $rootDir, $dispatcher);
342   }
343
344   /**
345    * Calls methods defined via sfEventDispatcher.
346    *
347    * @param string $method The method name
348    * @param array  $arguments The method arguments
349    *
350    * @return mixed The returned value of the called method
351    */
352   public function __call($method, $arguments)
353   {
354     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', array('method' => $method, 'arguments' => $arguments)));
355     if (!$event->isProcessed())
356     {
357       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
358     }
359
360     return $event->getReturnValue();
361   }
362 }
363
Note: See TracBrowser for help on using the browser.