<?php
namespace Moto\System; use Moto; class ServiceProviderManager { protected static $_instance; protected $_initialized = false; protected $_providers = []; protected $_autoBoot = false; protected function __construct() { } public static function getInstance() { if (!static::$_instance) { static::$_instance = new static(); } return static::$_instance; } public function bootAll() { $this->_autoBoot = true; foreach ($this->_providers as $provider) { $this->_bootProvider($provider['instance']); } } public function registerProviders(array $providers = []) { foreach ($providers as $provider) { $this->register($provider); } } public function register($provider) { if ($registered = $this->getProvider($provider)) { return $registered; } if (is_string($provider)) { $provider = $this->_createProvider($provider); } if (!($provider instanceof ServiceProvider)) { if (Moto\System::isDevelopmentStage()) { Moto\System\Log::emergency('Cant register provider by checking on instanceof ServiceProvider', [ 'provider' => (is_object($provider) ? get_class($provider) : gettype($provider)), ]); throw new Moto\Exception('Invalid provider'); } return false; } if (method_exists($provider, 'register')) { $provider->register(); } $this->_providers[get_class($provider)] = [ 'instance' => $provider, 'class' => get_class($provider), 'booted' => false, ]; if ($this->_autoBoot) { $this->_bootProvider($provider); } return $provider; } protected function _createProvider($provider) { return new $provider(); } protected function _bootProvider(Moto\System\ServiceProvider $provider) { $class = get_class($provider); if ($this->_providers[$class]['booted']) { return; } $this->_providers[$class]['booted'] = true; if (method_exists($provider, 'boot')) { $provider->boot(); } } public function getProvider($provider) { if ($provider instanceof Moto\System\ServiceProvider) { return $provider; } if (array_key_exists($provider, $this->_providers)) { return $this->_providers[$provider]['instance']; } return null; } } 