123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- <?php
- class sfProjectConfiguration
- {
- protected
- $rootDir = null,
- $symfonyLibDir = null,
- $plugins = array('sfPropelPlugin'),
- $pluginPaths = array(),
- $overriddenPluginPaths = array(),
- $pluginConfigurations = array(),
- $pluginsLoaded = false;
- static protected
- $active = null;
-
- public function __construct($rootDir = null, sfEventDispatcher $dispatcher = null)
- {
- if (is_null(sfProjectConfiguration::$active) || $this instanceof sfApplicationConfiguration)
- {
- sfProjectConfiguration::$active = $this;
- }
- $this->rootDir = is_null($rootDir) ? self::guessRootDir() : realpath($rootDir);
- $this->symfonyLibDir = realpath(dirname(__FILE__).'/..');
- $this->dispatcher = is_null($dispatcher) ? new sfEventDispatcher() : $dispatcher;
- ini_set('magic_quotes_runtime', 'off');
- sfConfig::set('sf_symfony_lib_dir', $this->symfonyLibDir);
- $this->setRootDir($this->rootDir);
- $this->setup();
- $this->loadPlugins();
- }
-
- public function setup()
- {
- }
-
- 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;
- }
-
- public function setRootDir($rootDir)
- {
- $this->rootDir = $rootDir;
- sfConfig::add(array(
- 'sf_root_dir' => $rootDir,
-
- '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_doc_dir' => $rootDir.DIRECTORY_SEPARATOR.'doc',
- 'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
- ));
- $this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
- $this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
- }
-
- public function getRootDir()
- {
- return $this->rootDir;
- }
-
- public function setCacheDir($cacheDir)
- {
- sfConfig::set('sf_cache_dir', $cacheDir);
- }
-
- public function setLogDir($logDir)
- {
- sfConfig::set('sf_log_dir', $logDir);
- }
-
- public function setWebDir($webDir)
- {
- sfConfig::add(array(
- 'sf_web_dir' => $webDir,
- 'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.'uploads',
- ));
- }
-
- public function getModelDirs()
- {
- return array_merge(
- $this->getPluginSubPaths('/lib/model'),
- array(sfConfig::get('sf_lib_dir').'/model')
- );
- }
-
- public function getGeneratorTemplateDirs($class, $theme)
- {
- return array_merge(
- array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'),
- $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/template'),
- array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/template'),
- $this->getPluginSubPaths('/data/generator/'.$class.'/default/template')
- );
- }
-
- public function getGeneratorSkeletonDirs($class, $theme)
- {
- return array_merge(
- array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'),
- $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/skeleton'),
- array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/skeleton'),
- $this->getPluginSubPaths('/data/generator/'.$class.'/default/skeleton')
- );
- }
-
- 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)));
- }
-
- public function getConfigPaths($configPath)
- {
- $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
- $files = array(
- sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath,
- );
- foreach ($this->getPluginPaths() as $path)
- {
- if (is_file($file = $path.'/'.$globalConfigPath))
- {
- $files[] = $file;
- }
- }
- $files = array_merge($files, array(
- sfConfig::get('sf_root_dir').'/'.$globalConfigPath,
- sfConfig::get('sf_root_dir').'/'.$configPath,
- ));
- foreach ($this->getPluginPaths() as $path)
- {
- if (is_file($file = $path.'/'.$configPath))
- {
- $files[] = $file;
- }
- }
- $configs = array();
- foreach (array_unique($files) as $file)
- {
- if (is_readable($file))
- {
- $configs[] = $file;
- }
- }
- return $configs;
- }
-
- public function setPlugins(array $plugins)
- {
- if ($this->pluginsLoaded)
- {
- throw new LogicException('Plugins have already been loaded.');
- }
- $this->plugins = $plugins;
- $this->pluginPaths = array();
- }
-
- public function enablePlugins($plugins)
- {
- $this->setPlugins(array_merge($this->plugins, is_array($plugins) ? $plugins : array($plugins)));
- }
-
- public function disablePlugins($plugins)
- {
- if ($this->pluginsLoaded)
- {
- throw new LogicException('Plugins have already been loaded.');
- }
- if (!is_array($plugins))
- {
- $plugins = array($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 = array();
- }
-
- public function enableAllPluginsExcept($plugins = array())
- {
- if ($this->pluginsLoaded)
- {
- throw new LogicException('Plugins have already been loaded.');
- }
- $this->plugins = array();
- foreach ($this->getAllPluginPaths() as $plugin => $path)
- {
- $this->plugins[] = $plugin;
- }
- $this->disablePlugins($plugins);
- }
-
- public function getPlugins()
- {
- return $this->plugins;
- }
-
- public function getPluginSubPaths($subPath = '')
- {
- if (array_key_exists($subPath, $this->pluginPaths))
- {
- return $this->pluginPaths[$subPath];
- }
- $this->pluginPaths[$subPath] = array();
- $pluginPaths = $this->getPluginPaths();
- foreach ($pluginPaths as $pluginPath)
- {
- if (is_dir($pluginPath.$subPath))
- {
- $this->pluginPaths[$subPath][] = $pluginPath.$subPath;
- }
- }
- return $this->pluginPaths[$subPath];
- }
-
- public function getPluginPaths()
- {
- if (array_key_exists('', $this->pluginPaths))
- {
- return $this->pluginPaths[''];
- }
- $pluginPaths = $this->getAllPluginPaths();
- $this->pluginPaths[''] = array();
- 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[''];
- }
-
- public function getAllPluginPaths()
- {
- $pluginPaths = array();
- $finder = sfFinder::type('dir')->maxdepth(0)->follow_link()->name('*Plugin');
- $dirs = array(
- sfConfig::get('sf_symfony_lib_dir').'/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;
- }
-
- public function setPluginPath($plugin, $path)
- {
- $this->overriddenPluginPaths[$plugin] = realpath($path);
- }
-
- 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];
- }
-
- public function getEventDispatcher()
- {
- return $this->dispatcher;
- }
-
- public function getSymfonyLibDir()
- {
- return $this->symfonyLibDir;
- }
-
- static public function getActive()
- {
- if (is_null(sfProjectConfiguration::$active))
- {
- throw new RuntimeException('There is no active configuration.');
- }
- return sfProjectConfiguration::$active;
- }
- static public function guessRootDir()
- {
- $r = new ReflectionClass('ProjectConfiguration');
- return realpath(dirname($r->getFileName()).'/..');
- }
-
- static public function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
- {
- $class = $application.'Configuration';
- if (is_null($rootDir))
- {
- $rootDir = self::guessRootDir();
- }
- if (!file_exists($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);
- }
-
- public function __call($method, $arguments)
- {
- $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', array('method' => $method, 'arguments' => $arguments)));
- if (!$event->isProcessed())
- {
- throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
- }
- return $event->getReturnValue();
- }
- }
|