Development

Changeset 2875

You must first sign up to be able to contribute.

Changeset 2875

Show
Ignore:
Timestamp:
11/29/06 17:59:19 (2 years ago)
Author:
fabien
Message:

added a new sfSimpleAutoload() method to sfCore to ease unit testing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/util/sfCore.class.php

    r2809 r2875  
    2020  static protected 
    2121    $autoloadCallables = array(), 
    22     $classes              = array(); 
     22    $classes           = array(); 
    2323 
    2424  static public function bootstrap($sf_symfony_lib_dir, $sf_symfony_data_dir) 
     
    129129  } 
    130130 
    131   static public function getautoloadCallables() 
     131  static public function getAutoloadCallables() 
    132132  { 
    133133    return self::$autoloadCallables; 
     
    188188      function __autoload($class) 
    189189      { 
    190         foreach (sfCore::getautoloadCallables() as $callable) 
     190        foreach (sfCore::getAutoloadCallables() as $callable) 
    191191        { 
    192192          if (call_user_func($callable, $class)) 
     
    211211    self::addAutoloadCallable(array('sfCore', 'splAutoload')); 
    212212  } 
     213 
     214  static public function splSimpleAutoload($class) 
     215  { 
     216    // class already exists 
     217    if (class_exists($class, false)) 
     218    { 
     219      return true; 
     220    } 
     221 
     222    // we have a class path, let's include it 
     223    if (isset(self::$classes[$class])) 
     224    { 
     225      require(self::$classes[$class]); 
     226 
     227      return true; 
     228    } 
     229 
     230    return false; 
     231  } 
     232 
     233  static public function initSimpleAutoload($dirs) 
     234  { 
     235    require_once(dirname(__FILE__).'/sfFinder.class.php'); 
     236    self::$classes = array(); 
     237    $finder = sfFinder::type('file')->ignore_version_control()->name('*.php'); 
     238    foreach ((array) $dirs as $dir) 
     239    { 
     240      $files = $finder->in(glob($dir)); 
     241      if (is_array($files)) 
     242      { 
     243        foreach ($files as $file) 
     244        { 
     245          preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes); 
     246          foreach ($classes[1] as $class) 
     247          { 
     248            self::$classes[$class] = $file; 
     249          } 
     250        } 
     251      } 
     252    } 
     253 
     254    if (function_exists('spl_autoload_register')) 
     255    { 
     256      ini_set('unserialize_callback_func', 'spl_autoload_call'); 
     257 
     258      spl_autoload_register(array('sfCore', 'splSimpleAutoload')); 
     259    } 
     260    elseif (!function_exists('__autoload')) 
     261    { 
     262      ini_set('unserialize_callback_func', '__autoload'); 
     263 
     264      function __autoload($class) 
     265      { 
     266        return sfCore::splSimpleAutolaod($class); 
     267      } 
     268    } 
     269  } 
    213270}