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; } }