Changeset 20397
- Timestamp:
- 07/22/09 09:16:13 (4 years ago)
- Files:
-
- components/dependency_injection/trunk/lib/sfServiceContainer.php (modified) (7 diffs)
- components/dependency_injection/trunk/lib/sfServiceContainerBuilder.php (modified) (1 diff)
- components/dependency_injection/trunk/test/unit/sfServiceContainerBuilderTest.php (modified) (1 diff)
- components/dependency_injection/trunk/test/unit/sfServiceContainerTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
components/dependency_injection/trunk/lib/sfServiceContainer.php
r19691 r20397 46 46 { 47 47 protected 48 $ allServices= array(),48 $serviceIds = array(), 49 49 $parameters = array(), 50 50 $services = array(), … … 189 189 190 190 /** 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(); 203 198 $r = new ReflectionClass($this); 204 199 foreach ($r->getMethods() as $method) … … 206 201 if (preg_match('/^get(.+)Service$/', $name = $method->getName(), $match)) 207 202 { 208 $ services[self::underscore($match[1])] = $this->$name();203 $ids[] = self::underscore($match[1]); 209 204 } 210 205 } 211 206 212 return array_merge($ services, $this->services);207 return array_merge($ids, array_keys($this->services)); 213 208 } 214 209 … … 308 303 public function rewind() 309 304 { 310 $this-> allServices = $this->getServices();311 312 $this->count = count($this-> allServices);305 $this->serviceIds = $this->getServiceIds(); 306 307 $this->count = count($this->serviceIds); 313 308 } 314 309 … … 320 315 public function key() 321 316 { 322 return key($this->allServices);317 return current($this->serviceIds); 323 318 } 324 319 … … 330 325 public function current() 331 326 { 332 return current($this->allServices);327 return $this->getService(current($this->serviceIds)); 333 328 } 334 329 … … 338 333 public function next() 339 334 { 340 next($this-> allServices);335 next($this->serviceIds); 341 336 342 337 --$this->count; components/dependency_injection/trunk/lib/sfServiceContainerBuilder.php
r20362 r20397 79 79 80 80 /** 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())); 101 88 } 102 89 components/dependency_injection/trunk/test/unit/sfServiceContainerBuilderTest.php
r19679 r20397 90 90 $t->ok($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared'); 91 91 92 // ->getService s()93 $t->diag('->getService s()');92 // ->getServiceIds() 93 $t->diag('->getServiceIds()'); 94 94 $builder = new sfServiceContainerBuilder(); 95 95 $builder->register('foo', 'stdClass'); 96 96 $builder->bar = $bar = new stdClass(); 97 97 $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'); 100 99 101 100 // ->createService() # file components/dependency_injection/trunk/test/unit/sfServiceContainerTest.php
r19679 r20397 13 13 sfServiceContainerAutoloader::register(); 14 14 15 $t = new lime_test(4 0);15 $t = new lime_test(41); 16 16 17 17 // __construct() … … 112 112 $t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); 113 113 $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'); 114 121 115 122 class ProjectServiceContainer extends sfServiceContainer