| @@ 12-50 (lines=39) @@ | ||
| 9 | ||
| 10 | namespace SugiPHP\Config; |
|
| 11 | ||
| 12 | class IniLoader implements LoaderInterface |
|
| 13 | { |
|
| 14 | protected $locator; |
|
| 15 | ||
| 16 | public function __construct(LocatorInterface $locator = null) |
|
| 17 | { |
|
| 18 | $this->locator = $locator; |
|
| 19 | } |
|
| 20 | ||
| 21 | /** |
|
| 22 | * {@inheritdoc} |
|
| 23 | */ |
|
| 24 | public function load($resource) |
|
| 25 | { |
|
| 26 | // check the extension. If it's not provided we'll add .ini |
|
| 27 | if (pathinfo($resource, PATHINFO_EXTENSION) === "") { |
|
| 28 | $resource .= ".ini"; |
|
| 29 | } |
|
| 30 | ||
| 31 | $file = false; |
|
| 32 | ||
| 33 | if ($this->locator) { |
|
| 34 | // pass it to the locator (if set) and than include the file |
|
| 35 | $file = $this->locator->locate($resource); |
|
| 36 | } elseif (is_file($resource) && is_readable($resource)) { |
|
| 37 | // check if the $resource is a real file and include it |
|
| 38 | $file = $resource; |
|
| 39 | } |
|
| 40 | ||
| 41 | if ($file) { |
|
| 42 | // By setting the process_sections parameter (second param) to TRUE, you get a |
|
| 43 | // multidimensional array, with the section names and settings included. |
|
| 44 | // The default for process_sections is FALSE |
|
| 45 | return parse_ini_file($file, true); |
|
| 46 | } |
|
| 47 | ||
| 48 | return null; |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| @@ 12-48 (lines=37) @@ | ||
| 9 | ||
| 10 | namespace SugiPHP\Config; |
|
| 11 | ||
| 12 | class JsonLoader implements LoaderInterface |
|
| 13 | { |
|
| 14 | protected $locator; |
|
| 15 | ||
| 16 | public function __construct(LocatorInterface $locator = null) |
|
| 17 | { |
|
| 18 | $this->locator = $locator; |
|
| 19 | } |
|
| 20 | ||
| 21 | /** |
|
| 22 | * {@inheritdoc} |
|
| 23 | */ |
|
| 24 | public function load($resource) |
|
| 25 | { |
|
| 26 | // check the extension. If it's not provided we'll add .php |
|
| 27 | if (pathinfo($resource, PATHINFO_EXTENSION) === "") { |
|
| 28 | $resource .= ".json"; |
|
| 29 | } |
|
| 30 | ||
| 31 | $file = false; |
|
| 32 | ||
| 33 | if ($this->locator) { |
|
| 34 | // pass it to the locator (if set) and than include the file |
|
| 35 | $file = $this->locator->locate($resource); |
|
| 36 | } elseif (is_file($resource) && is_readable($resource)) { |
|
| 37 | // check if the $resource is a real file and include it |
|
| 38 | $file = $resource; |
|
| 39 | } |
|
| 40 | ||
| 41 | if ($file) { |
|
| 42 | $json = file_get_contents($file); |
|
| 43 | return json_decode($json, true); |
|
| 44 | } |
|
| 45 | ||
| 46 | return null; |
|
| 47 | } |
|
| 48 | } |
|
| 49 | ||
| @@ 12-47 (lines=36) @@ | ||
| 9 | ||
| 10 | namespace SugiPHP\Config; |
|
| 11 | ||
| 12 | class NativeLoader implements LoaderInterface |
|
| 13 | { |
|
| 14 | protected $locator; |
|
| 15 | ||
| 16 | public function __construct(LocatorInterface $locator = null) |
|
| 17 | { |
|
| 18 | $this->locator = $locator; |
|
| 19 | } |
|
| 20 | ||
| 21 | /** |
|
| 22 | * {@inheritdoc} |
|
| 23 | */ |
|
| 24 | public function load($resource) |
|
| 25 | { |
|
| 26 | // check the extension. If it's not provided we'll add .php |
|
| 27 | if (pathinfo($resource, PATHINFO_EXTENSION) === "") { |
|
| 28 | $resource .= ".php"; |
|
| 29 | } |
|
| 30 | ||
| 31 | $file = false; |
|
| 32 | ||
| 33 | if ($this->locator) { |
|
| 34 | // pass it to the locator (if set) and than include the file |
|
| 35 | $file = $this->locator->locate($resource); |
|
| 36 | } elseif (is_file($resource) && is_readable($resource)) { |
|
| 37 | // check if the $resource is a real file and include it |
|
| 38 | $file = $resource; |
|
| 39 | } |
|
| 40 | ||
| 41 | if ($file) { |
|
| 42 | return include $file; |
|
| 43 | } |
|
| 44 | ||
| 45 | return null; |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| @@ 16-54 (lines=39) @@ | ||
| 13 | ||
| 14 | use Symfony\Component\Yaml\Yaml; |
|
| 15 | ||
| 16 | class YamlLoader implements LoaderInterface |
|
| 17 | { |
|
| 18 | protected $locator; |
|
| 19 | ||
| 20 | public function __construct(LocatorInterface $locator = null) |
|
| 21 | { |
|
| 22 | $this->locator = $locator; |
|
| 23 | } |
|
| 24 | ||
| 25 | /** |
|
| 26 | * {@inheritdoc} |
|
| 27 | */ |
|
| 28 | public function load($resource) |
|
| 29 | { |
|
| 30 | // check the extension. If it's not provided we'll add .php |
|
| 31 | if (pathinfo($resource, PATHINFO_EXTENSION) === "") { |
|
| 32 | $resource .= ".yml"; |
|
| 33 | } |
|
| 34 | ||
| 35 | $file = false; |
|
| 36 | ||
| 37 | if ($this->locator) { |
|
| 38 | // pass it to the locator (if set) and than include the file |
|
| 39 | $file = $this->locator->locate($resource); |
|
| 40 | } elseif (is_file($resource) && is_readable($resource)) { |
|
| 41 | // check if the $resource is a real file and include it |
|
| 42 | $file = $resource; |
|
| 43 | } |
|
| 44 | ||
| 45 | if ($file) { |
|
| 46 | // Passing a file as an input is a deprecated feature and will be removed in Symfony\Yaml 3.0. |
|
| 47 | $yaml = file_get_contents($file); |
|
| 48 | ||
| 49 | return Yaml::parse($yaml); |
|
| 50 | } |
|
| 51 | ||
| 52 | return null; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||