2013-05-20 12:18:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2015-02-27 01:10:32 +01:00
|
|
|
* Use the SilverStripe configuration system to lookup config for a
|
2013-05-20 12:18:07 +02:00
|
|
|
* particular service.
|
|
|
|
*
|
|
|
|
* @package framework
|
|
|
|
* @subpackage injector
|
|
|
|
*/
|
2014-03-12 02:58:49 +01:00
|
|
|
class SilverStripeServiceConfigurationLocator extends ServiceConfigurationLocator {
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
/**
|
|
|
|
* List of Injector configurations cached from Config in class => config format.
|
2015-02-27 01:10:32 +01:00
|
|
|
* If any config is false, this denotes that this class and all its parents
|
2014-03-12 02:58:49 +01:00
|
|
|
* have no configuration specified.
|
2015-02-27 01:10:32 +01:00
|
|
|
*
|
2014-03-12 02:58:49 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $configs = array();
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
public function locateConfigFor($name) {
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
// Check direct or cached result
|
|
|
|
$config = $this->configFor($name);
|
|
|
|
if($config !== null) return $config;
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
// do parent lookup if it's a class
|
|
|
|
if (class_exists($name)) {
|
2015-02-27 01:10:32 +01:00
|
|
|
$parents = array_reverse(array_values(ClassInfo::ancestry($name)));
|
2013-05-20 12:18:07 +02:00
|
|
|
array_shift($parents);
|
2014-03-12 02:58:49 +01:00
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
foreach ($parents as $parent) {
|
2015-02-27 01:10:32 +01:00
|
|
|
// have we already got for this?
|
2014-03-12 02:58:49 +01:00
|
|
|
$config = $this->configFor($parent);
|
|
|
|
if($config !== null) {
|
|
|
|
// Cache this result
|
2013-05-20 12:18:07 +02:00
|
|
|
$this->configs[$name] = $config;
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 02:58:49 +01:00
|
|
|
}
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01: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;
|
|
|
|
}
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the config for a named service without performing a hierarchy walk
|
2015-02-27 01:10:32 +01:00
|
|
|
*
|
2014-03-12 02:58:49 +01:00
|
|
|
* @param string $name Name of service
|
2015-02-27 01:10:32 +01:00
|
|
|
* @return mixed Returns either the configuration data, if there is any. A missing config is denoted
|
2014-03-12 02:58:49 +01:00
|
|
|
* by a value of either null (there is no direct config assigned and a hierarchy walk is necessary)
|
2015-02-27 01:10:32 +01:00
|
|
|
* or false (there is no config for this class, nor within the hierarchy for this class).
|
2014-03-12 02:58:49 +01:00
|
|
|
*/
|
|
|
|
protected function configFor($name) {
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
// Return cached result
|
|
|
|
if (isset($this->configs[$name])) {
|
|
|
|
return $this->configs[$name]; // Potentially false
|
|
|
|
}
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2014-03-12 02:58:49 +01:00
|
|
|
$config = Config::inst()->get('Injector', $name);
|
|
|
|
if ($config) {
|
|
|
|
$this->configs[$name] = $config;
|
|
|
|
return $config;
|
|
|
|
} else {
|
|
|
|
return null;
|
2013-05-20 12:18:07 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-12 02:58:49 +01:00
|
|
|
}
|