services['name'])) { throw new \RuntimeException( sprintf('Service "%s" is already registered.', $name) ); } $this->services[$name] = $instance; } /** * @param string $name Service-Name oder FQCN * * @return object */ public function get($name) { if ($this->has($name)) { return $this->services[$name]; } return $this->createService($name); } /** * @param string $name * * @return bool */ public function has($name) { return isset($this->services[$name]); } /** * @param string $name * * @return object */ private function createService($name) { $createServiceMethod = 'create' . $name . 'Service'; if (!method_exists($this, $createServiceMethod)) { throw new \RuntimeException( sprintf( 'Service "%s" could not be created. Container method "%s" is missing.', $name, $createServiceMethod ) ); } $this->services[$name] = $this->$createServiceMethod(); return $this->services[$name]; } /** * @param string $contollerClass * @param Request|null $request * * @return AbstractController */ public function getApiController($contollerClass, Request $request = null) { // @todo /*$interfaces = class_implements($contollerClass, true); if (!in_array('Xentral\Modules\Api\Version1\Controller\ControllerInterface', $interfaces, true)) { throw new \CountryInvalidArgumentException(sprintf( '"%s" must implement "%s"', $contollerClass, 'Xentral\Modules\Api\Version1\Controller\ControllerInterface' )); }*/ $parents = class_parents($contollerClass, true); if (!in_array(AbstractController::class, $parents, true)) { throw new \InvalidArgumentException(sprintf( '"%s" must implement "%s"', $contollerClass, AbstractController::class )); } // Controller nicht sharen! // Resourcen können sich Controller teilen return new $contollerClass( $this->get('LegacyApi'), $this->get('Database'), $this->get('Converter'), $request ?: $this->get('Request'), $this->get('ResourceManager') ); } /** * @param string $resourceClass * * @return AbstractApiResource * * @throws \ReflectionException */ public function getApiResource($resourceClass) { $resourceReflection = new ReflectionClass($resourceClass); $resourceName = $resourceReflection->getShortName(); if ($this->has($resourceName)) { return $this->services[$resourceName]; } $parents = class_parents($resourceClass, true); if (!in_array(AbstractApiResource::class, $parents, true)) { throw new \InvalidArgumentException(sprintf( '"%s" must extend "%s"', $resourceClass, AbstractApiResource::class )); } // @todo /*$interfaces = class_implements($resourceClass, false); if (!in_array(ApiResourceInterface::class, $interfaces, true)) { throw new \CountryInvalidArgumentException(sprintf( '"%s" must implement "%s"', $resourceClass, ApiResourceInterface::class )); }*/ // Resource erzeugen $resource = new $resourceClass( $this->get('Database'), $this->get('Validator') ); // Resource sharen $this->add($resourceName, $resource); return $resource; } /** * @return ResourceManager */ private function createResourceManagerService() { return new ResourceManager( $this->get('Database'), $this->get('Validator'), $this->get('LegacyApi') ); } /** * @return LegacyApiLazyProxy */ private function createLegacyApiService() { return new LegacyApiLazyProxy(); } /** * @return LegacyApplication */ private function createLegacyApplicationService() { return new LegacyApplication(); } /** * @return DigestAuth */ private function createDigestAuthService() { return new DigestAuth($this->get('Database'), $this->get('Request')); } /** * @return Database */ private function createDatabaseService() { /** @var LegacyApplication $legacyApp */ $legacyApp = $this->get('LegacyApplication'); return $legacyApp->Container->get('Database'); } /** * @return Validator */ private function createValidatorService() { $validator = new Validator(); $validator->addValidator('db_value', new DbValueRule($this->get('Database'))); $validator->addValidator('boolean', new BooleanRule()); $validator->addValidator('decimal', new DecimalRule()); $validator->addValidator('length', new LengthRule()); $validator->addValidator('lower', new LowerRule()); $validator->addValidator('not_present', new NotPresentRule()); $validator->addValidator('time', new TimeRule()); $validator->addValidator('unique', new UniqueRule($this->get('Database'))); $validator->addValidator('upper', new UpperRule()); return $validator; } /** * @return Request */ private function createRequestService() { /** @var LegacyApplication $legacyApp */ $legacyApp = $this->get('LegacyApplication'); return $legacyApp->Container->get('Request'); } /** * @return ApiRouter */ private function createApiRouterService() { return new ApiRouter(); } /** * @return Converter */ private function createConverterService() { return new Converter($this->get('XmlConverter'), $this->get('JsonConverter')); } /** * @return OpenTransConverter */ private function createOpenTransConverterService() { return new OpenTransConverter(); } /** * @return XmlConverter */ private function createXmlConverterService() { return new XmlConverter(); } /** * @return JsonConverter */ private function createJsonConverterService() { return new JsonConverter(); } public function __clone() { } public function __wakeup() { } }