Development

Changeset 20397

You must first sign up to be able to contribute.

Changeset 20397

Show
Ignore:
Timestamp:
07/22/09 09:16:13 (4 years ago)
Author:
fabien
Message:

[dependency_injection] removed getServices() methods, replaced with getServiceIds(), optimized performance of the sfServiceContainer iterator by lazy loading the services

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • components/dependency_injection/trunk/lib/sfServiceContainer.php

    r19691 r20397  
    4646{ 
    4747  protected 
    48     $allServices = array(), 
     48    $serviceIds = array(), 
    4949    $parameters  = array(), 
    5050    $services    = array(), 
     
    189189 
    190190  /** 
    191    * Gets all services. 
    192    * 
    193    * Calling this method should be avoided as it creates all the services 
    194    * defined for this service container. 
    195    * 
    196    * It is mostly useful for testing purpose. 
    197    * 
    198    * @return array An array of services 
    199    */ 
    200   public function getServices() 
    201   { 
    202     $services = array(); 
     191   * Gets all service ids. 
     192   * 
     193   * @return array An array of all defined service ids 
     194   */ 
     195  public function getServiceIds() 
     196  { 
     197    $ids = array(); 
    203198    $r = new ReflectionClass($this); 
    204199    foreach ($r->getMethods() as $method) 
     
    206201      if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) 
    207202      { 
    208         $services[self::underscore($match[1])] = $this->$name(); 
     203        $ids[] = self::underscore($match[1]); 
    209204      } 
    210205    } 
    211206 
    212     return array_merge($services, $this->services); 
     207    return array_merge($ids, array_keys($this->services)); 
    213208  } 
    214209 
     
    308303  public function rewind() 
    309304  { 
    310     $this->allServices = $this->getServices(); 
    311  
    312     $this->count = count($this->allServices); 
     305    $this->serviceIds = $this->getServiceIds(); 
     306 
     307    $this->count = count($this->serviceIds); 
    313308  } 
    314309 
     
    320315  public function key() 
    321316  { 
    322     return key($this->allServices); 
     317    return current($this->serviceIds); 
    323318  } 
    324319 
     
    330325  public function current() 
    331326  { 
    332     return current($this->allServices); 
     327    return $this->getService(current($this->serviceIds)); 
    333328  } 
    334329 
     
    338333  public function next() 
    339334  { 
    340     next($this->allServices); 
     335    next($this->serviceIds); 
    341336 
    342337    --$this->count; 
  • components/dependency_injection/trunk/lib/sfServiceContainerBuilder.php

    r20362 r20397  
    7979 
    8080  /** 
    81    * Gets all services. 
    82    * 
    83    * Calling this method should be avoided as it creates all the services 
    84    * defined for this service container. 
    85    * 
    86    * It is mostly useful for testing purpose. 
    87    * 
    88    * @return array An array of services 
    89    */ 
    90   public function getServices() 
    91   { 
    92     $allServices = parent::getServices(); 
    93  
    94     $services = array(); 
    95     foreach ($this->getServiceDefinitions() as $id => $definition) 
    96     { 
    97       $services[$id] = $this->getService($id); 
    98     } 
    99  
    100     return array_merge($services, $allServices); 
     81   * Gets all service ids. 
     82   * 
     83   * @return array An array of all defined service ids 
     84   */ 
     85  public function getServiceIds() 
     86  { 
     87    return array_unique(array_merge(array_keys($this->getServiceDefinitions()), parent::getServiceIds())); 
    10188  } 
    10289 
  • components/dependency_injection/trunk/test/unit/sfServiceContainerBuilderTest.php

    r19679 r20397  
    9090$t->ok($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared'); 
    9191 
    92 // ->getServices() 
    93 $t->diag('->getServices()'); 
     92// ->getServiceIds() 
     93$t->diag('->getServiceIds()'); 
    9494$builder = new sfServiceContainerBuilder(); 
    9595$builder->register('foo', 'stdClass'); 
    9696$builder->bar = $bar = new stdClass(); 
    9797$builder->register('bar', 'stdClass'); 
    98 $services = $builder->getServices(); 
    99 $t->is(array_keys($services), array('foo', 'bar', 'service_container'), '->getServices() returns all services'); 
     98$t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids'); 
    10099 
    101100// ->createService() # file 
  • components/dependency_injection/trunk/test/unit/sfServiceContainerTest.php

    r19679 r20397  
    1313sfServiceContainerAutoloader::register(); 
    1414 
    15 $t = new lime_test(40); 
     15$t = new lime_test(41); 
    1616 
    1717// __construct() 
     
    112112$t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); 
    113113$t->ok(!isset($sc->bar), '->__isset() returns false if the service is not defined'); 
     114 
     115// ->getServiceIds() 
     116$t->diag('->getServiceIds()'); 
     117$sc = new sfServiceContainer(); 
     118$sc->setService('foo', $obj = new stdClass()); 
     119$sc->setService('bar', $obj = new stdClass()); 
     120$t->is($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids'); 
    114121 
    115122class ProjectServiceContainer extends sfServiceContainer