pgleadershipbook
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
@ 2
pgleadershipbook / trunk / system / Config / BaseService.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
<?php /** * CodeIgniter * * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * * Copyright (c) 2014-2019 British Columbia Institute of Technology * Copyright (c) 2019-2020 CodeIgniter Foundation * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @package CodeIgniter * @author CodeIgniter Dev Team * @copyright 2019-2020 CodeIgniter Foundation * @license https://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 4.0.0 * @filesource */ namespace CodeIgniter\Config; use CodeIgniter\Autoloader\Autoloader; use CodeIgniter\Autoloader\FileLocator; use Config\Autoload; use Config\Modules; /** * Services Configuration file. * * Services are simply other classes/libraries that the system uses * to do its job. This is used by CodeIgniter to allow the core of the * framework to be swapped out easily without affecting the usage within * the rest of your application. * * This is used in place of a Dependency Injection container primarily * due to its simplicity, which allows a better long-term maintenance * of the applications built on top of CodeIgniter. A bonus side-effect * is that IDEs are able to determine what class you are calling * whereas with DI Containers there usually isn't a way for them to do this. * * @see http://blog.ircmaxell.com/2015/11/simple-easy-risk-and-change.html * @see http://www.infoq.com/presentations/Simple-Made-Easy */ class BaseService { /** * Cache for instance of any services that * have been requested as a "shared" instance. * Keys should be lowercase service names. * * @var array */ static protected $instances = []; /** * Mock objects for testing which are returned if exist. * * @var array */ static protected $mocks = []; /** * Have we already discovered other Services? * * @var boolean */ static protected $discovered = false; /** * A cache of other service classes we've found. * * @var array */ static protected $services = []; //-------------------------------------------------------------------- /** * Returns a shared instance of any of the class' services. * * $key must be a name matching a service. * * @param string $key * @param array ...$params * * @return mixed */ protected static function getSharedInstance(string $key, ...$params) { $key = strtolower($key); // Returns mock if exists if (isset(static::$mocks[$key])) { return static::$mocks[$key]; } if (! isset(static::$instances[$key])) { // Make sure $getShared is false array_push($params, false); static::$instances[$key] = static::$key(...$params); } return static::$instances[$key]; } //-------------------------------------------------------------------- /** * The Autoloader class is the central class that handles our * spl_autoload_register method, and helper methods. * * @param boolean $getShared * * @return \CodeIgniter\Autoloader\Autoloader */ public static function autoloader(bool $getShared = true) { if ($getShared) { if (empty(static::$instances['autoloader'])) { static::$instances['autoloader'] = new Autoloader(); } return static::$instances['autoloader']; } return new Autoloader(); } //-------------------------------------------------------------------- /** * The file locator provides utility methods for looking for non-classes * within namespaced folders, as well as convenience methods for * loading 'helpers', and 'libraries'. * * @param boolean $getShared * * @return \CodeIgniter\Autoloader\FileLocator */ public static function locator(bool $getShared = true) { if ($getShared) { if (empty(static::$instances['locator'])) { static::$instances['locator'] = new FileLocator( static::autoloader() ); } return static::$instances['locator']; } return new FileLocator(static::autoloader()); } //-------------------------------------------------------------------- /** * Provides the ability to perform case-insensitive calling of service * names. * * @param string $name * @param array $arguments * * @return mixed */ public static function __callStatic(string $name, array $arguments) { $name = strtolower($name); if (method_exists(Services::class, $name)) { return Services::$name(...$arguments); } return static::discoverServices($name, $arguments); } //-------------------------------------------------------------------- /** * Reset shared instances and mocks for testing. * * @param boolean $init_autoloader Initializes autoloader instance */ public static function reset(bool $init_autoloader = false) { static::$mocks = []; static::$instances = []; if ($init_autoloader) { static::autoloader()->initialize(new Autoload(), new Modules()); } } //-------------------------------------------------------------------- /** * Inject mock object for testing. * * @param string $name * @param $mock */ public static function injectMock(string $name, $mock) { $name = strtolower($name); static::$mocks[$name] = $mock; } //-------------------------------------------------------------------- /** * Will scan all psr4 namespaces registered with system to look * for new Config\Services files. Caches a copy of each one, then * looks for the service method in each, returning an instance of * the service, if available. * * @param string $name * @param array $arguments * * @return mixed */ protected static function discoverServices(string $name, array $arguments) { if (! static::$discovered) { $config = config('Modules'); if ($config->shouldDiscover('services')) { $locator = static::locator(); $files = $locator->search('Config/Services'); if (empty($files)) { // no files at all found - this would be really, really bad return null; } // Get instances of all service classes and cache them locally. foreach ($files as $file) { $classname = $locator->getClassname($file); if (! in_array($classname, ['CodeIgniter\\Config\\Services'])) { static::$services[] = new $classname(); } } } static::$discovered = true; } if (! static::$services) { // we found stuff, but no services - this would be really bad return null; } // Try to find the desired service method foreach (static::$services as $class) { if (method_exists(get_class($class), $name)) { return $class::$name(...$arguments); } } return null; } } |
Commits for pgleadershipbook/trunk/system/Config/BaseService.php
Revision | Author | Commited | Message |
---|---|---|---|
2 | tporter | Thu 04 Feb, 2021 09:33:04 +0000 | PG Leadership book initial commit |