2013-05-20 12:18:07 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Core\Injector;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
|
2013-05-20 12:18:07 +02:00
|
|
|
/**
|
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.
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
class SilverStripeServiceConfigurationLocator implements 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) {
|
2014-03-12 02:58:49 +01:00
|
|
|
// Check direct or cached result
|
|
|
|
$config = $this->configFor($name);
|
2016-05-03 08:38:49 +02:00
|
|
|
if(!$config) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-12 02:58:49 +01:00
|
|
|
|
2016-05-03 08:38:49 +02:00
|
|
|
// If config is in `%$Source` format then inherit from the named config
|
|
|
|
if(is_string($config) && stripos($config, '%$') === 0) {
|
|
|
|
$name = substr($config, 2);
|
|
|
|
return $this->locateConfigFor($name);
|
2014-03-12 02:58:49 +01:00
|
|
|
}
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2016-05-03 08:38:49 +02:00
|
|
|
// Return the located 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
|
|
|
/**
|
|
|
|
* 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
|
2016-05-03 08:38:49 +02:00
|
|
|
* @return mixed Get config for this service
|
2014-03-12 02:58:49 +01:00
|
|
|
*/
|
|
|
|
protected function configFor($name) {
|
|
|
|
// Return cached result
|
2016-05-03 08:38:49 +02:00
|
|
|
if (array_key_exists($name, $this->configs)) {
|
|
|
|
return $this->configs[$name];
|
2014-03-12 02:58:49 +01:00
|
|
|
}
|
2015-02-27 01:10:32 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$config = Config::inst()->get('SilverStripe\\Core\\Injector\\Injector', $name);
|
2016-05-03 08:38:49 +02:00
|
|
|
$this->configs[$name] = $config;
|
|
|
|
return $config;
|
2013-05-20 12:18:07 +02:00
|
|
|
}
|
2014-03-12 02:58:49 +01:00
|
|
|
}
|