[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/config/ -> sfLoader.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 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   * sfLoader is a class which contains the logic to look for files/classes in symfony.
  13   *
  14   * @package    symfony
  15   * @subpackage util
  16   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  17   * @version    SVN: $Id: sfLoader.class.php 3313 2007-01-20 07:00:37Z fabien $
  18   */
  19  class sfLoader
  20  {
  21    /**
  22     * Gets directories where model classes are stored.
  23     *
  24     * @return array An array of directories
  25     */
  26    static public function getModelDirs()
  27    {
  28      $dirs = array(sfConfig::get('sf_lib_dir').'/model' ? sfConfig::get('sf_lib_dir').'/model' : 'lib/model'); // project
  29      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model'))
  30      {
  31        $dirs = array_merge($dirs, $pluginDirs);                                                                // plugins
  32      }
  33  
  34      return $dirs;
  35    }
  36  
  37    /**
  38     * Gets directories where controller classes are stored for a given module.
  39     *
  40     * @param string The module name
  41     *
  42     * @return array An array of directories
  43     */
  44    static public function getControllerDirs($moduleName)
  45    {
  46      $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_action_dir_name');
  47  
  48      $dirs = array();
  49      foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  50      {
  51        $dirs[$key.'/'.$suffix] = $value;
  52      }
  53  
  54      $dirs[sfConfig::get('sf_app_module_dir').'/'.$suffix] = false;                                     // application
  55  
  56      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  57      {
  58        $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), true))); // plugins
  59      }
  60  
  61      $dirs[sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix] = true;                            // core modules
  62  
  63      return $dirs;
  64    }
  65  
  66    /**
  67     * Gets directories where template files are stored for a given module.
  68     *
  69     * @param string The module name
  70     *
  71     * @return array An array of directories
  72     */
  73    static public function getTemplateDirs($moduleName)
  74    {
  75      $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_template_dir_name');
  76  
  77      $dirs = array();
  78      foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  79      {
  80        $dirs[] = $key.'/'.$suffix;
  81      }
  82  
  83      $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$suffix;                        // application
  84  
  85      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix))
  86      {
  87        $dirs = array_merge($dirs, $pluginDirs);                                       // plugins
  88      }
  89  
  90      $dirs[] = sfConfig::get('sf_symfony_data_dir').'/modules/'.$suffix;              // core modules
  91      $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($suffix);         // generated templates in cache
  92  
  93      return $dirs;
  94    }
  95  
  96    /**
  97     * Gets the template directory to use for a given module and template file.
  98     *
  99     * @param string The module name
 100     * @param string The template file
 101     *
 102     * @return string A template directory
 103     */
 104    static public function getTemplateDir($moduleName, $templateFile)
 105    {
 106      $dirs = self::getTemplateDirs($moduleName);
 107      foreach ($dirs as $dir)
 108      {
 109        if (is_readable($dir.'/'.$templateFile))
 110        {
 111          return $dir;
 112        }
 113      }
 114  
 115      return null;
 116    }
 117  
 118    /**
 119     * Gets the template to use for a given module and template file.
 120     *
 121     * @param string The module name
 122     * @param string The template file
 123     *
 124     * @return string A template path
 125     */
 126    static public function getTemplatePath($moduleName, $templateFile)
 127    {
 128      $dir = self::getTemplateDir($moduleName, $templateFile);
 129  
 130      return $dir ? $dir.'/'.$templateFile : null;
 131    }
 132  
 133    /**
 134     * Gets the i18n directory to use for a given module.
 135     *
 136     * @param string The module name
 137     *
 138     * @return string An i18n directory
 139     */
 140    static public function getI18NDir($moduleName)
 141    {
 142      $suffix = $moduleName.'/'.sfConfig::get('sf_app_module_i18n_dir_name');
 143  
 144      // application
 145      $dir = sfConfig::get('sf_app_module_dir').'/'.$suffix;
 146      if (is_dir($dir))
 147      {
 148        return $dir;
 149      }
 150  
 151      // plugins
 152      $dirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$suffix);
 153      if (isset($dirs[0]))
 154      {
 155        return $dirs[0];
 156      }
 157    }
 158  
 159    /**
 160     * Gets directories where template files are stored for a generator class and a specific theme.
 161     *
 162     * @param string The generator class name
 163     * @param string The theme name
 164     *
 165     * @return array An array of directories
 166     */
 167    static public function getGeneratorTemplateDirs($class, $theme)
 168    {
 169      $dirs = array();
 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      $dirs[] = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template';                       // project
 177      $dirs[] = sfConfig::get('sf_symfony_data_dir').'/generator/'.$class.'/default/template';                  // default theme
 178  
 179      return $dirs;
 180    }
 181  
 182    /**
 183     * Gets directories where the skeleton is stored for a generator class and a specific theme.
 184     *
 185     * @param string The generator class name
 186     * @param string The theme name
 187     *
 188     * @return array An array of directories
 189     */
 190    static public function getGeneratorSkeletonDirs($class, $theme)
 191    {
 192      $dirs = array();
 193  
 194      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
 195      {
 196        $dirs = array_merge($dirs, $pluginDirs);                                                                // plugin
 197      }
 198  
 199      $dirs[] = sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton';                       // project
 200      $dirs[] = sfConfig::get('sf_symfony_data_dir').'/generator/'.$class.'/default/skeleton';                  // default theme
 201  
 202      return $dirs;
 203    }
 204  
 205    /**
 206     * Gets the template to use for a generator class.
 207     *
 208     * @param string The generator class name
 209     * @param string The theme name
 210     * @param string The template path
 211     *
 212     * @return string A template path
 213     *
 214     * @throws sfException
 215     */
 216    static public function getGeneratorTemplate($class, $theme, $path)
 217    {
 218      $dirs = self::getGeneratorTemplateDirs($class, $theme);
 219      foreach ($dirs as $dir)
 220      {
 221        if (is_readable($dir.'/'.$path))
 222        {
 223          return $dir.'/'.$path;
 224        }
 225      }
 226  
 227      throw new sfException(sprintf('Unable to load "%s" generator template in: %s', $path, implode(', ', $dirs)));
 228    }
 229  
 230    /**
 231     * Gets the configuration file paths for a given relative configuration path.
 232     *
 233     * @param string The configuration path
 234     *
 235     * @return array An array of paths
 236     */
 237    static public function getConfigPaths($configPath)
 238    {
 239      $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
 240  
 241      $files = array(
 242        sfConfig::get('sf_symfony_data_dir').'/'.$globalConfigPath,                    // symfony
 243        sfConfig::get('sf_symfony_data_dir').'/'.$configPath,                          // core modules
 244      );
 245  
 246      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$globalConfigPath))
 247      {
 248        $files = array_merge($files, $pluginDirs);                                     // plugins
 249      }
 250  
 251      $files = array_merge($files, array(
 252        sfConfig::get('sf_root_dir').'/'.$globalConfigPath,                            // project
 253        sfConfig::get('sf_root_dir').'/'.$configPath,                                  // project
 254        sfConfig::get('sf_app_dir').'/'.$globalConfigPath,                             // application
 255        sfConfig::get('sf_cache_dir').'/'.$configPath,                                 // generated modules
 256      ));
 257  
 258      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$configPath))
 259      {
 260        $files = array_merge($files, $pluginDirs);                                     // plugins
 261      }
 262  
 263      $files[] = sfConfig::get('sf_app_dir').'/'.$configPath;                          // module
 264  
 265      $configs = array();
 266      foreach (array_unique($files) as $file)
 267      {
 268        if (is_readable($file))
 269        {
 270          $configs[] = $file;
 271        }
 272      }
 273  
 274      return $configs;
 275    }
 276  
 277    /**
 278     * Gets the helper directories for a given module name.
 279     *
 280     * @param string The module name
 281     *
 282     * @return array An array of directories
 283     */
 284    static public function getHelperDirs($moduleName = '')
 285    {
 286      $dirs = array();
 287  
 288      if ($moduleName)
 289      {
 290        $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/'.sfConfig::get('sf_app_module_lib_dir_name').'/helper'; // module
 291  
 292        if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/lib/helper'))
 293        {
 294          $dirs = array_merge($dirs, $pluginDirs);                                                                              // module plugins
 295        }
 296      }
 297  
 298      $dirs[] = sfConfig::get('sf_app_lib_dir').'/helper';                                                                      // application
 299  
 300      $dirs[] = sfConfig::get('sf_lib_dir').'/helper';                                                                          // project
 301  
 302      if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/helper'))
 303      {
 304        $dirs = array_merge($dirs, $pluginDirs);                                                                                // plugins
 305      }
 306  
 307      $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/helper';                                                                  // global
 308  
 309      return $dirs;
 310    }
 311  
 312    /**
 313     * Loads helpers.
 314     *
 315     * @param array  An array of helpers to load
 316     * @param string A module name (optional)
 317     *
 318     * @throws sfViewException
 319     */
 320    static public function loadHelpers($helpers, $moduleName = '')
 321    {
 322      static $loaded = array();
 323  
 324      $dirs = self::getHelperDirs($moduleName);
 325      foreach ((array) $helpers as $helperName)
 326      {
 327        if (isset($loaded[$helperName]))
 328        {
 329          continue;
 330        }
 331  
 332        $fileName = $helperName.'Helper.php';
 333        foreach ($dirs as $dir)
 334        {
 335          $included = false;
 336          if (is_readable($dir.'/'.$fileName))
 337          {
 338            include($dir.'/'.$fileName);
 339            $included = true;
 340            break;
 341          }
 342        }
 343  
 344        if (!$included)
 345        {
 346          // search in the include path
 347          if ((@include('helper/'.$fileName)) != 1)
 348          {
 349            $dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path()));
 350  
 351            // remove sf_root_dir from dirs
 352            foreach ($dirs as &$dir)
 353            {
 354              $dir = str_replace('%SF_ROOT_DIR%', sfConfig::get('sf_root_dir'), $dir);
 355            }
 356  
 357            throw new sfViewException(sprintf('Unable to load "%sHelper.php" helper in: %s', $helperName, implode(', ', $dirs)));
 358          }
 359        }
 360  
 361        $loaded[$helperName] = true;
 362      }
 363    }
 364  
 365    static public function loadPluginConfig()
 366    {
 367      if ($pluginConfigs = glob(sfConfig::get('sf_plugins_dir').'/*/config/config.php'))
 368      {
 369        foreach ($pluginConfigs as $config)
 370        {
 371          include($config);
 372        }
 373      }
 374    }
 375  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7