silverstripe-framework/control/injector/SilverStripeServiceConfigurationLocator.php

50 lines
1.1 KiB
PHP
Raw Normal View History

<?php
/**
2014-08-15 08:53:05 +02:00
* Use the SilverStripe configuration system to lookup config for a
* particular service.
*
* @package framework
* @subpackage injector
*/
class SilverStripeServiceConfigurationLocator {
2014-08-15 08:53:05 +02:00
private $configs = array();
2014-08-15 08:53:05 +02:00
public function locateConfigFor($name) {
2014-08-15 08:53:05 +02:00
if (isset($this->configs[$name])) {
return $this->configs[$name];
}
2014-08-15 08:53:05 +02:00
$config = Config::inst()->get('Injector', $name);
if ($config) {
$this->configs[$name] = $config;
return $config;
}
2014-08-15 08:53:05 +02:00
// do parent lookup if it's a class
if (class_exists($name)) {
$parents = array_reverse(array_keys(ClassInfo::ancestry($name)));
array_shift($parents);
foreach ($parents as $parent) {
2014-08-15 08:53:05 +02:00
// have we already got for this?
if (isset($this->configs[$parent])) {
return $this->configs[$parent];
}
$config = Config::inst()->get('Injector', $parent);
if ($config) {
$this->configs[$name] = $config;
return $config;
} else {
$this->configs[$parent] = false;
}
}
2014-08-15 08:53:05 +02:00
// there is no parent config, so we'll record that as false so we don't do the expensive
// lookup through parents again
$this->configs[$name] = false;
}
}
2014-08-15 08:53:05 +02:00
}