Development

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

You must first sign up to be able to contribute.

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

Revision 22934, 4.5 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.2] rolled back r22929

  • 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  * sfPluginConfiguration represents a configuration for a symfony plugin.
13  *
14  * @package    symfony
15  * @subpackage config
16  * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 abstract class sfPluginConfiguration
20 {
21   protected
22     $configuration = null,
23     $dispatcher    = null,
24     $name          = null,
25     $rootDir       = null;
26
27   /**
28    * Constructor.
29    *
30    * @param sfProjectConfiguration $configuration The project configuration
31    * @param string                 $rootDir       The plugin root directory
32    * @param string                 $name          The plugin name
33    */
34   public function __construct(sfProjectConfiguration $configuration, $rootDir = null, $name = null)
35   {
36     $this->configuration = $configuration;
37     $this->dispatcher = $configuration->getEventDispatcher();
38     $this->rootDir = is_null($rootDir) ? $this->guessRootDir() : realpath($rootDir);
39     $this->name = is_null($name) ? $this->guessName() : $name;
40
41     $this->setup();
42     $this->configure();
43
44     if (!$this->configuration instanceof sfApplicationConfiguration)
45     {
46       $this->initializeAutoload();
47       $this->initialize();
48     }
49   }
50
51   /**
52    * Sets up the plugin.
53    *
54    * This method can be used when creating a base plugin configuration class for other plugins to extend.
55    */
56   public function setup()
57   {
58   }
59
60   /**
61    * Configures the plugin.
62    *
63    * This method is called before the plugin's classes have been added to sfAutoload.
64    */
65   public function configure()
66   {
67   }
68
69   /**
70    * Initializes the plugin.
71    *
72    * This method is called after the plugin's classes have been added to sfAutoload.
73    *
74    * @return boolean|null If false sfApplicationConfiguration will look for a config.php (maintains BC with symfony < 1.2)
75    */
76   public function initialize()
77   {
78   }
79
80   /**
81    * Returns the plugin root directory.
82    *
83    * @return string
84    */
85   public function getRootDir()
86   {
87     return $this->rootDir;
88   }
89
90   /**
91    * Returns the plugin name.
92    *
93    * @return string
94    */
95   public function getName()
96   {
97     return $this->name;
98   }
99
100   /**
101    * Initializes autoloading for the plugin.
102    *
103    * This method is called when a plugin is initialized in a project
104    * configuration. Otherwise, autoload is handled in
105    * {@link sfApplicationConfiguration} using {@link sfAutoload}.
106    *
107    * @see sfSimpleAutoload
108    */
109   public function initializeAutoload()
110   {
111     $autoload = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
112
113     if (is_readable($file = $this->rootDir.'/config/autoload.yml'))
114     {
115       $this->configuration->getEventDispatcher()->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
116
117       $config = new sfAutoloadConfigHandler();
118       $mappings = $config->evaluate(array($file));
119
120       foreach ($mappings as $class => $file)
121       {
122         $autoload->setClassPath($class, $file);
123       }
124     }
125     else
126     {
127       $autoload->addDirectory($this->rootDir.'/lib');
128     }
129
130     $autoload->register();
131   }
132
133   /**
134    * Filters sfAutoload configuration values.
135    *
136    * @param sfEvent $event 
137    * @param array   $config
138    *
139    * @return array
140    */
141   public function filterAutoloadConfig(sfEvent $event, array $config)
142   {
143     // use array_merge so config is added to the front of the autoload array
144     if (!isset($config['autoload'][$this->name.'_lib']))
145     {
146       $config['autoload'] = array_merge(array(
147         $this->name.'_lib' => array(
148           'path'      => $this->rootDir.'/lib',
149           'recursive' => true,
150         ),
151       ), $config['autoload']);
152     }
153
154     if (!isset($config['autoload'][$this->name.'_module_libs']))
155     {
156       $config['autoload'] = array_merge(array(
157         $this->name.'_module_libs' => array(
158           'path'      => $this->rootDir.'/modules/*/lib',
159           'recursive' => true,
160           'prefix'    => 1,
161         ),
162       ), $config['autoload']);
163     }
164
165     return $config;
166   }
167
168   /**
169    * Guesses the plugin root directory.
170    *
171    * @return string
172    */
173   protected function guessRootDir()
174   {
175     $r = new ReflectionClass(get_class($this));
176     return realpath(dirname($r->getFilename()).'/..');
177   }
178
179   /**
180    * Guesses the plugin name.
181    *
182    * @return string
183    */
184   protected function guessName()
185   {
186     return substr(get_class($this), 0, -13);
187   }
188 }
189
Note: See TracBrowser for help on using the browser.