Inheritance: use trait Webiny\Component\StdLib\StdLibTrait, use trait Webiny\Component\StdLib\SingletonTrait
Esempio n. 1
0
 /**
  * Get real FactoryArgument value
  * @throws ServiceManagerException
  *
  * @return mixed
  */
 public function value()
 {
     // Get real arguments values from Argument instances
     $arguments = [];
     foreach ($this->arguments as $arg) {
         $arguments[] = $arg->value();
     }
     $this->arguments = $arguments;
     if ($this->value->startsWith('@')) {
         // Service can only be called in a NON-STATIC context
         $arguments = $this->arr($this->arguments)->count() > 0 ? $this->arguments : null;
         return ServiceManager::getInstance()->getService($this->value->val(), $arguments);
     } else {
         // CLASS can be instantiated or called statically
         $value = $this->value->val();
         if (class_exists($value) && !$this->static) {
             $reflection = new \ReflectionClass($value);
             return $reflection->newInstanceArgs($this->arguments);
         } elseif (class_exists($value) && $this->static) {
             // Return class name for static call
             return $this->value->val();
         }
         throw new ServiceManagerException(ServiceManagerException::SERVICE_CLASS_DOES_NOT_EXIST, [$this->value]);
     }
 }
 public function testInheritance()
 {
     $servicesConfig = new ConfigObject(self::$services);
     ServiceManager::getInstance()->registerServices('Inheritance', $servicesConfig);
     $service = ServiceManager::getInstance()->getService('Inheritance.Real');
     $this->assertEquals('Webiny', $service->getValue());
 }
Esempio n. 3
0
 /**
  * Returns instance of cache driver.
  * If instance with the given $cacheId doesn't exist, ServiceManagerException is thrown.
  *
  * @param string $cacheId Name of the cache driver
  *
  * @throws \Webiny\Component\ServiceManager\ServiceManagerException
  * @return CacheStorage
  */
 protected static function cache($cacheId)
 {
     try {
         return ServiceManager::getInstance()->getService('Cache.' . $cacheId);
     } catch (ServiceManagerException $e) {
         throw $e;
     }
 }
Esempio n. 4
0
 /**
  * Get storage
  *
  * @param string $storageName Storage name
  *
  * @throws \Webiny\Component\ServiceManager\ServiceManagerException
  * @return Storage
  */
 protected static function storage($storageName)
 {
     try {
         return ServiceManager::getInstance()->getService('Storage.' . $storageName);
     } catch (ServiceManagerException $e) {
         throw $e;
     }
 }
Esempio n. 5
0
 /**
  * Get logger.
  * Just provide the logger name without the 'logger.' prefix.
  * The name must match the name of your service.
  *
  * @param string $name Logger service name.
  *
  * @return LoggerDriverInterface
  * @throws ServiceManagerException
  */
 public static function logger($name = 'Webiny')
 {
     try {
         return ServiceManager::getInstance()->getService('Logger.' . $name);
     } catch (ServiceManagerException $e) {
         if ($e->getCode() == ServiceManagerException::SERVICE_DEFINITION_NOT_FOUND) {
             return new Logger($name, new NullDriver());
         }
         throw $e;
     }
 }
Esempio n. 6
0
 /**
  * Loads the image and returns an instance of Image class.
  *
  * @param string|File    $image             This can either be image file name that corresponds to File $key parameter,
  *                                          or it can be an instance of Webiny\Component\Storage\File\File.
  * @param string|Storage $storage           This can either be the name of the storage service or it can be an
  *                                          instance of Webiny\Component\Storage\Storage.
  *                                          NOTE: This parameter is not required if you pass the $image as
  *                                          instance of Webiny\Component\Storage\File\File.
  *
  * @return ImageInterface
  */
 public function image($image, $storage = 'local')
 {
     if ($image instanceof File) {
         return ImageLoader::load($image->getContents());
     } else {
         if (!$storage instanceof Storage) {
             $storage = ServiceManager::getInstance()->getService('Storage.' . $storage);
         }
         $file = new File($image, $storage);
         return ImageLoader::load($file->getContents());
     }
 }
Esempio n. 7
0
 /**
  * Set the configuration file.
  * The root of your yaml config file must match the component class name, or an exception will be thrown.
  * If you wish to define a default config, just create a static array called $defaultConfig.
  * When setting/updating a config, it is always merged with the default config and current loaded config.
  *
  * @param string|ConfigObject $componentConfig Path to the configuration YAML file or ConfigObject instance
  *
  * @throws Exception\Exception
  */
 public static function setConfig($componentConfig)
 {
     // get component name
     $component = new StringObject(__CLASS__);
     $component = $component->explode('\\')->last();
     // check if we already have a config
     /*if (!self::$componentConfig) {
                 $defaultConfigArray = [];
     
                 // check if we have default config
                 if (isset(self::$defaultConfig)) {
                     $defaultConfigArray = self::$defaultConfig;
                 }
     
                 self::$componentConfig = new ConfigObject($defaultConfigArray);
             }*/
     // check if we have default config
     if (isset(self::$defaultConfig)) {
         self::$componentConfig = new ConfigObject(self::$defaultConfig);
     } else {
         self::$componentConfig = new ConfigObject([]);
     }
     // validate config
     if ($componentConfig instanceof ConfigObject) {
         $config = $componentConfig;
     } else {
         $config = Config::getInstance()->yaml($componentConfig)->get($component, false);
     }
     if (!$config) {
         throw new Exception\Exception('Invalid configuration file for ' . $component . ' component.');
     }
     // merge current config with new config
     self::$componentConfig->mergeWith($config);
     // automatically assign parameters to ServiceManager
     if (self::$componentConfig->get('Parameters', false)) {
         ServiceManager::getInstance()->registerParameters(self::$componentConfig->get('Parameters'));
     }
     // automatically register services
     if (self::$componentConfig->get('Services', false)) {
         ServiceManager::getInstance()->registerServices($component, self::$componentConfig->get('Services'), true);
     }
     // automatically register class loader libraries
     if (self::$componentConfig->get('ClassLoader', false)) {
         ClassLoader::getInstance()->registerMap(self::$componentConfig->get('ClassLoader')->toArray());
     }
     // trigger callback
     self::postSetConfig();
 }
Esempio n. 8
0
 public function testServiceManager()
 {
     $mainServiceConfig = new ConfigObject(self::$services['MainService']);
     $secondServiceConfig = new ConfigObject(self::$services['SecondService']);
     ServiceManager::getInstance()->registerParameters(self::$services['Parameters']);
     ServiceManager::getInstance()->registerService('MainService', $mainServiceConfig);
     ServiceManager::getInstance()->registerService('SecondService', $secondServiceConfig);
     /* @var $mainService MainService */
     $mainService = ServiceManager::getInstance()->getService('MainService');
     $this->assertEquals('InjectedServiceValue', $mainService->getInjectedServiceValue());
     $this->assertEquals('Webiny', $mainService->getCallValue());
     $this->assertEquals('FirstArgument', $mainService->getFirstArgumentValue());
     $checkInstance = '\\Webiny\\Component\\ServiceManager\\Tests\\Classes\\ConstructorArgumentClass';
     $this->assertInstanceOf($checkInstance, $mainService->getSomeInstance());
     $this->assertEquals('SomeParameter', $mainService->getSomeInstance()->getConstructorParameterValue());
 }
Esempio n. 9
0
 /**
  * Check if current service has a parent service and merge its config with parent service config.
  *
  * @throws ServiceManagerException
  */
 private function manageInheritance()
 {
     $config = $this->serviceConfig;
     if ($config->keyExists('Parent')) {
         $parentServiceName = $this->str($config->key('Parent'))->trimLeft('@')->val();
         $parentConfig = ServiceManager::getInstance()->getServiceConfig($parentServiceName)->toArray(true);
         if (!$parentConfig->keyExists('Abstract')) {
             throw new ServiceManagerException(ServiceManagerException::SERVICE_IS_NOT_ABSTRACT, [$config->key('Parent')]);
         }
         $config = $this->extendConfig($config, $parentConfig);
     }
     // Check if it's a potentially valid service definition
     if (!$config->keyExists('Class') && !$config->keyExists('Factory')) {
         throw new ServiceManagerException(ServiceManagerException::SERVICE_CLASS_KEY_NOT_FOUND, [$this->serviceName]);
     }
     $this->serviceConfig = $config;
 }
Esempio n. 10
0
 /**
  * Create proper argument value
  *
  * @param mixed $object
  * @param array $arguments
  *
  * @throws ServiceManagerException
  *
  * @return mixed|object
  */
 private function createValue($object, $arguments = [])
 {
     if ($this->isInstanceOf($arguments, '\\Webiny\\Component\\Config\\ConfigObject')) {
         $arguments = $arguments->toArray();
     }
     if (!$this->isArray($arguments)) {
         throw new ServiceManagerException(ServiceManagerException::INVALID_SERVICE_ARGUMENTS_TYPE, [$object]);
     }
     if (!$this->isString($object)) {
         return $object;
     }
     $object = $this->str($object);
     if ($object->startsWith('@')) {
         return ServiceManager::getInstance()->getService($object->trimLeft('@')->val());
     } else {
         $value = $object->val();
         if (class_exists($value)) {
             $reflection = new \ReflectionClass($value);
             return $reflection->newInstanceArgs($arguments);
         }
         return $value;
     }
 }
Esempio n. 11
0
 /**
  * @param string $database Mongo service name (Default: Webiny)
  *
  * @return Mongo
  */
 protected static function mongo($database = 'Webiny')
 {
     return ServiceManager::getInstance()->getService('Mongo.' . $database);
 }
Esempio n. 12
0
 public function testCustomValidatorService()
 {
     $service = new ConfigObject(['Class' => 'Webiny\\Component\\Validation\\Tests\\Lib\\CustomValidator', 'Tags' => ['validation-plugin']]);
     ServiceManager::getInstance()->registerService('CustomValidator', $service);
     Validation::deleteInstance();
     $this->assertTrue($this->validation()->validate(12, 'customValidator'));
     $this->assertInternalType('string', $this->validation()->validate(9, 'customValidator', false));
     $this->assertInternalType('string', $this->validation()->validate(13, 'customValidator', false));
 }
 /**
  * Using services that reference each other should throw a ServiceManagerException
  */
 public function testCircularReferencingException()
 {
     $this->setExpectedException('\\Webiny\\Component\\ServiceManager\\ServiceManagerException');
     ServiceManager::getInstance()->getService('Exception.Third');
 }