Development

Changeset 19559

You must first sign up to be able to contribute.

Changeset 19559

Show
Ignore:
Timestamp:
06/25/09 18:47:22 (8 months ago)
Author:
fabien
Message:

[dependency_injection] fixed Iterator bug + defined an order between services defined directly and the ones defined via a method

Files:

Legend:

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

    r16741 r19559  
    2525{ 
    2626  protected 
    27     $parameters = array(), 
    28     $services   = array(), 
    29     $count      = 0; 
     27    $allServices = array(), 
     28    $parameters  = array(), 
     29    $services    = array(), 
     30    $count       = 0; 
    3031 
    3132  /** 
     
    8182   * @return mixed  The parameter value 
    8283   * 
    83    * @throw  RuntimeException if the parameter is not defined 
     84   * @throw  InvalidArgumentException if the parameter is not defined 
    8485   */ 
    8586  public function getParameter($name) 
     
    8788    if (!$this->hasParameter($name)) 
    8889    { 
    89       throw new RuntimeException(sprintf('The parameter "%s" must be defined.', $name)); 
     90      throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name)); 
    9091    } 
    9192 
     
    142143   * Gets a service. 
    143144   * 
     145   * If a service is both defined through a setService() method and 
     146   * with a set*Service() method, the former has always precedence. 
     147   * 
    144148   * @param  string $id The service identifier 
    145149   * 
     
    150154  public function getService($id) 
    151155  { 
     156    if (isset($this->services[$id])) 
     157    { 
     158      return $this->services[$id]; 
     159    } 
     160 
    152161    if (method_exists($this, $method = 'get'.self::camelize($id).'Service')) 
    153162    { 
     
    155164    } 
    156165 
    157     if (!isset($this->services[$id])) 
    158     { 
    159       throw new InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); 
    160     } 
    161  
    162     return $this->services[$id]; 
     166    throw new InvalidArgumentException(sprintf('The service "%s" does not exist.', $id)); 
    163167  } 
    164168 
     
    258262  public function rewind() 
    259263  { 
    260     reset($this->services); 
    261  
    262     $this->count = count($this->services); 
     264    $services = array(); 
     265    $r = new ReflectionClass($this); 
     266    foreach ($r->getMethods() as $method) 
     267    { 
     268      if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) 
     269      { 
     270        $services[self::underscore($match[1])] = $this->$name(); 
     271      } 
     272    } 
     273 
     274    $this->allServices = array_merge($services, $this->services); 
     275 
     276    $this->count = count($this->allServices); 
    263277  } 
    264278 
     
    270284  public function key() 
    271285  { 
    272     return key($this->services); 
     286    return key($this->allServices); 
    273287  } 
    274288 
     
    280294  public function current() 
    281295  { 
    282     return current($this->services); 
     296    return current($this->allServices); 
    283297  } 
    284298 
     
    288302  public function next() 
    289303  { 
    290     next($this->services); 
     304    next($this->allServices); 
    291305 
    292306    --$this->count; 
     
    307321    return preg_replace(array('#\.#', '#/(.?)#e', '/(^|_|-)+(.)/e'), array('_', "'::'.strtoupper('\\1')", "strtoupper('\\2')"), $id); 
    308322  } 
     323 
     324  static public function underscore($id) 
     325  { 
     326    return strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), $id)); 
     327  } 
    309328} 
  • components/dependency_injection/trunk/test

    • Property svn:externals set to
      lime http://svn.symfony-project.com/tools/lime/trunk/lib

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.