|
| 1 | +<?php namespace CodeIgniter\Config; |
| 2 | + |
| 3 | +/** |
| 4 | + * CodeIgniter |
| 5 | + * |
| 6 | + * An open source application development framework for PHP |
| 7 | + * |
| 8 | + * This content is released under the MIT License (MIT) |
| 9 | + * |
| 10 | + * Copyright (c) 2014-2018 British Columbia Institute of Technology |
| 11 | + * |
| 12 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | + * of this software and associated documentation files (the "Software"), to deal |
| 14 | + * in the Software without restriction, including without limitation the rights |
| 15 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | + * copies of the Software, and to permit persons to whom the Software is |
| 17 | + * furnished to do so, subject to the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be included in |
| 20 | + * all copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | + * THE SOFTWARE. |
| 29 | + * |
| 30 | + * @package CodeIgniter |
| 31 | + * @author CodeIgniter Dev Team |
| 32 | + * @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | + * @license https://opensource.org/licenses/MIT MIT License |
| 34 | + * @link https://codeigniter.com |
| 35 | + * @since Version 3.0.0 |
| 36 | + * @filesource |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * Class Config |
| 41 | + * |
| 42 | + * @package CodeIgniter\Config |
| 43 | + */ |
| 44 | +class Config |
| 45 | +{ |
| 46 | + /** |
| 47 | + * Cache for instance of any configurations that |
| 48 | + * have been requested as "shared" instance. |
| 49 | + * |
| 50 | + * @var array |
| 51 | + */ |
| 52 | + static private $instances = []; |
| 53 | + |
| 54 | + //-------------------------------------------------------------------- |
| 55 | + |
| 56 | + /** |
| 57 | + * Create new configuration instances or return |
| 58 | + * a shared instance |
| 59 | + * |
| 60 | + * @param string $name Configuration name |
| 61 | + * @param boolean $getShared Use shared instance |
| 62 | + * |
| 63 | + * @return mixed|null |
| 64 | + */ |
| 65 | + public static function get(string $name, bool $getShared = true) |
| 66 | + { |
| 67 | + $class = $name; |
| 68 | + if (($pos = strrpos($name, '\\')) !== false) |
| 69 | + { |
| 70 | + $class = substr($name, $pos + 1); |
| 71 | + } |
| 72 | + |
| 73 | + $class = strtolower($class); |
| 74 | + |
| 75 | + if (! $getShared) |
| 76 | + { |
| 77 | + return self::createClass($name); |
| 78 | + } |
| 79 | + |
| 80 | + if (! isset( self::$instances[$class] )) |
| 81 | + { |
| 82 | + self::$instances[$class] = self::createClass($name); |
| 83 | + } |
| 84 | + return self::$instances[$class]; |
| 85 | + } |
| 86 | + |
| 87 | + //-------------------------------------------------------------------- |
| 88 | + |
| 89 | + /** |
| 90 | + * Find configuration class and create instance |
| 91 | + * |
| 92 | + * @param string $name Classname |
| 93 | + * |
| 94 | + * @return mixed|null |
| 95 | + */ |
| 96 | + private static function createClass(string $name) |
| 97 | + { |
| 98 | + if (class_exists($name)) |
| 99 | + { |
| 100 | + return new $name(); |
| 101 | + } |
| 102 | + |
| 103 | + $locator = Services::locator(); |
| 104 | + $file = $locator->locateFile($name, 'Config'); |
| 105 | + |
| 106 | + if (empty($file)) |
| 107 | + { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + $name = $locator->getClassname($file); |
| 112 | + |
| 113 | + if (empty($name)) |
| 114 | + { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + return new $name(); |
| 119 | + } |
| 120 | + |
| 121 | + //-------------------------------------------------------------------- |
| 122 | +} |
0 commit comments