* @package tao */ abstract class tao_models_classes_Service { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Contains the references of each service instance. * The service name is used as key. * * @access private * @var array */ private static $instances = []; /** * pattern to create service dynamically. * Use the printf syntax, where %1$ is the short name of the service * * @access private * @var string */ const namePattern = 'tao%1$s_models_classes_%1$sService'; // --- OPERATIONS --- /** * protected constructor to enforce the singleton pattern * * @access protected * @author Joel Bout, * @return mixed */ protected function __construct() { } /** * returns an instance of the service defined by servicename. Always returns * same instance for a class * * @access public * @author Joel Bout, * @deprecated * * @param string $serviceName * * @return tao_models_classes_Service * @throws common_exception_Error */ public static function getServiceByName($serviceName) { $returnValue = null; $className = (!class_exists($serviceName) || !preg_match("/^(tao|wf)/", $serviceName)) ? sprintf(self::namePattern, ucfirst(strtolower($serviceName))) : $serviceName; // does the class exist if (!class_exists($className)) { throw new common_exception_Error('Tried to init abstract class ' . $className); } $class = new ReflectionClass($className); // is it concrete if ($class->isAbstract()) { throw new common_exception_Error('Tried to init abstract class ' . $className . ' for param \'' . $serviceName . '\''); } // does it extend Service if (!$class->isSubclassOf('tao_models_classes_Service')) { throw new common_exception_Error("$className must referr to a class extending the tao_models_classes_Service"); } //create the instance only once if (!isset(self::$instances[$className])) { self::$instances[$className] = new $className(); } //get the instance $returnValue = self::$instances[$className]; return $returnValue; } /** * returns an instance of the service the function was called from. Always * the same instance for a class * * @access public * @author Joel Bout, * @return $this */ public static function singleton() { $returnValue = null; $serviceName = get_called_class(); if (!isset(self::$instances[$serviceName])) { self::$instances[$serviceName] = new $serviceName(); } $returnValue = self::$instances[$serviceName]; return $returnValue; } /** * Placeholder until the servicemanage is passed properly * * @return \oat\oatbox\service\ServiceManager */ public function getServiceLocator() { return ServiceManager::getServiceManager(); } }