Development

/plugins/ysfDimensionsPlugin/branches/1.1/lib/config/ysfProjectConfiguration.class.php

You must first sign up to be able to contribute.

root/plugins/ysfDimensionsPlugin/branches/1.1/lib/config/ysfProjectConfiguration.class.php

Revision 10349, 10.2 kB (checked in by dwhittle, 5 years ago)

ysfDimensionsPlugin: cleaned up phpdoc + updated license year

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to "Date Author Revision"
Line 
1 <?php
2
3 /**
4  *
5  * Copyright (c) 2008 Yahoo! Inc.  All rights reserved.
6  * The copyrights embodied in the content in this file are licensed
7  * under the MIT open source license.
8  *
9  * For the full copyright and license information, please view the LICENSE.yahoo
10  * file that was distributed with this source code.
11  */
12
13 require_once(dirname(__FILE__).'/ysfConfigDimension.class.php');
14
15 /**
16  * ysfProjectConfiguration represents a configuration for a symfony project.
17  *
18  * This class adds support for configuration dimensions and caches all results.
19  *
20  * @package    ysymfony
21  * @subpackage config
22  * @author     Dustin Whittle <dustin.whittle@symfony-project.com>
23  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
24  * @version    SVN: $Id: sfApplicationConfiguration.class.php 7618 2008-02-27 00:02:41Z dwhittle $
25  */
26 class ysfProjectConfiguration extends sfProjectConfiguration
27 {
28   protected $dimension = null;
29
30   /**
31    * Sets the project dimension.
32    *
33    * @param array The configuration dimension as an array
34    */
35   public function setDimension($dimension)
36   {
37     if ($dimension === null || $dimension === false)
38     {
39       $this->dimension = null;
40
41       // if dimension changes, change cache dir
42       $this->setCacheDir($this->getRootDir().'/cache');
43     }
44     else
45     {
46       try
47       {
48         if(!$this->hasDimension())
49         {
50           $this->dimension = new ysfConfigDimension($this->getEventDispatcher(), (!isset($this->debug) || (isset($this->debug) && $this->debug === true)) ? new sfNoCache() : new sfAPCCache(array('prefix' => 'symfony.dimensions.config.default:'.$this->application.':'.$this->environment, 'automatic_cleaning_factor' => 0, 'lifetime' => 86400)));
51         }
52
53         // set dimension
54         $this->dimension->set($dimension);
55
56         sfConfig::set('sf_dimension', $this->dimension->getName());
57
58         // if dimension changes, change cache dir
59         $this->setCacheDir($this->getRootDir().'/cache/'.$this->dimension->getName());
60       }
61       catch (sfException $e)
62       {
63         if(method_exists($e, 'asResponse'))
64         {
65           // handle exception early on and exit if something bad happens
66           $e->asResponse()->send(); exit;
67         }
68         else
69         {
70           echo $e->getMessage(); exit;
71         }
72       }
73     }
74   }
75
76   /**
77    * Returns the project dimension.
78    *
79    * @return ysfConfigDimension The configuration dimension
80    */
81   public function getDimension()
82   {
83     return $this->dimension;
84   }
85
86   /**
87    * Has a dimension been set?
88    *
89    * @return boolean Returns true or false depending on whether a dimension has been set or not.
90    */
91   public function hasDimension()
92   {
93     return !is_null($this->dimension);
94   }
95
96   /**
97    * Gets directories where model classes are stored.
98    *
99    * @return array An array of directories
100    */
101   public function getModelDirs()
102   {
103     // if there is a configuration dimension
104     if ($this->hasDimension())
105     {
106       // if there is a cache return it
107       if($this->dimension->getCache()->has('sf_model_dirs'))
108       {
109         return $this->dimension->getCache()->get('sf_model_dirs');
110       }
111       else
112       {
113         $dimensions = array_reverse($this->dimension->getCascade());
114
115         $dirs = array(sfConfig::get('sf_lib_dir').'/model');                       // project
116
117         // extend base dirs and add dimension cascade, checking dir exists
118         foreach ($dimensions as $dimension)
119         {
120           if(is_readable($dirs[0].'/'.$dimension))
121           {
122             array_unshift($dirs, $dirs[0].'/'.$dimension);
123           }
124         }
125
126         if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model'))
127         {
128           foreach ($pluginDirs as $dir)
129           {
130             // extend base dirs and add dimension cascade, checking dir exists
131             foreach ($dimensions as $dimension)
132             {
133               if(is_readable($dir.'/'.$dimension))
134               {
135                 array_unshift($pluginDirs, $dir.'/'.$dimension);
136               }
137             }
138           }
139           $dirs = array_merge($dirs, $pluginDirs);                               // plugins
140         }
141
142         // save cache
143         $this->dimension->getCache()->set('sf_model_dirs', $dirs);
144       }
145     }
146     else
147     {
148       $dirs = parent::getModelDirs();
149     }
150
151     return $dirs;
152   }
153
154   /**
155    * Gets directories where template files are stored for a generator class and a specific theme.
156    *
157    * @param string The generator class name
158    * @param string The theme name
159    *
160    * @return array An array of directories
161    */
162   public function getGeneratorTemplateDirs($class, $theme)
163   {
164     // if there is a configuration dimension
165     if ($this->hasDimension())
166     {
167       $cacheKey = sprintf('sf_generator_template_dirs_%s_%s', $class, $theme);
168
169       // if there is a cache return it
170       if($this->dimension->getCache()->has($cacheKey))
171       {
172        $dirs = $this->dimension->getCache()->get($cacheKey);
173       }
174       else
175       {
176         $dimensions = $this->dimension->getCascade();
177
178         // otherwise create and store
179         $dirs = array();
180
181         if (is_readable(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'))
182         {
183           $dir = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template';                       // project
184
185           // extend base dirs and add dimension cascade + checking dir exists
186           foreach ($dimensions as $dimension)
187           {
188             if(is_readable($dir.'/'.$dimension))
189             {
190               array_push($dirs, $dir.'/'.$dimension);
191             }
192           }
193
194           array_push($dirs, $dir);
195         }
196
197         if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/template'))
198         {
199           foreach ($pluginDirs as $dir)
200           {
201             foreach ($dimensions as $dimension)
202             {
203               if(is_readable($dir.'/'.$dimension))
204               {
205                 array_push($dirs, $dir.'/'.$dimension);
206               }
207             }
208             array_push($dirs, $dir);                                                                              // plugin
209           }
210         }
211
212         if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/template'))
213         {
214           $dirs = array_merge($dirs, $bundledPluginDirs);                                                         // bundled plugin
215         }
216
217         // save cache
218         $this->dimension->getCache()->set($cacheKey, $dirs);
219       }
220     }
221     else
222     {
223       $dirs = parent::getGeneratorTemplateDirs($class, $theme);
224     }
225
226     return $dirs;
227   }
228
229   /**
230    * Gets directories where the skeleton is stored for a generator class and a specific theme.
231    *
232    * @param string The generator class name
233    * @param string The theme name
234    *
235    * @return array An array of directories
236    */
237   public function getGeneratorSkeletonDirs($class, $theme)
238   {
239     // if there is a configuration dimension
240     if ($this->hasDimension())
241     {
242       $cacheKey = sprintf('sf_generator_skeleton_dirs_%s_%s', $class, $theme);
243
244       // if there is a cache return it
245       if($this->dimension->getCache()->has($cacheKey))
246       {
247        $dirs = $this->dimension->getCache()->get($cacheKey);
248       }
249       else
250       {
251         $dimensions = $this->dimension->getCascade();
252
253         // otherwise create and store
254         $dirs = array();                  // project
255
256         if(is_readable(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'))
257         {
258           $dir = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton';
259
260           // extend base dirs and add dimension cascade + checking dir exists
261           foreach ($dimensions as $dimension)
262           {
263             if(is_readable($dir.'/'.$dimension))
264             {
265               array_push($dirs, $dir.'/'.$dimension);
266             }
267           }
268           array_push($dirs, $dir);                                                                              // project
269         }
270
271         if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
272         {
273           foreach ($pluginDirs as $dir)
274           {
275             foreach ($dimensions as $dimension)
276             {
277               if(is_readable($dir.'/'.$dimension))
278               {
279                 array_push($dirs, $dir.'/'.$dimension);
280               }
281             }
282             array_push($dirs, $dir);                                                                              // plugin
283           }
284         }
285
286         if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
287         {
288           $dirs = array_merge($dirs, $bundledPluginDirs);                                                         // bundled plugin
289         }
290
291         // save cache
292         $this->dimension->getCache()->set($cacheKey, $dirs);
293       }
294     }
295     else
296     {
297       $dirs = parent::getGeneratorSkeletonDirs($class, $theme);
298     }
299
300     return $dirs;
301   }
302
303   /**
304    * Gets the template to use for a generator class.
305    *
306    * @param string The generator class name
307    * @param string The theme name
308    * @param string The template path
309    *
310    * @return string A template path
311    *
312    * @throws sfException
313    */
314   public function getGeneratorTemplate($class, $theme, $path)
315   {
316     // if there is a configuration dimension
317     if ($this->hasDimension())
318     {
319       $cacheKey = sprintf('sf_generator_templates_%s_%s_%s', $class, $theme, $path);
320
321       // if there is a cache return it
322       if($this->dimension->getCache()->has($cacheKey))
323       {
324        $dirs = $this->dimension->getCache()->get($cacheKey);
325       }
326       else
327       {
328         // otherwise create and store
329         $dirs = $this->getGeneratorTemplateDirs($class, $theme);
330
331         foreach ($dirs as $dir)
332         {
333           if (is_readable($dir.'/'.$path))
334           {
335             // save cache
336             $this->dimension->getCache()->set($cacheKey, $dir.'/'.$path);
337
338             return $dir.'/'.$path;
339           }
340         }
341
342         throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
343       }
344     }
345     else
346     {
347       $dirs = parent::getGeneratorTemplate($class, $theme, $path);
348     }
349
350     return $dirs;
351   }
352 }
353
Note: See TracBrowser for help on using the browser.