Exemplo n.º 1
0
 /**
  * @param string
  * @param string|NULL
  * @return object
  * @throws ServiceNotFoundException
  * @throws InvalidServiceFactoryException
  * @throws ServiceNotInstanceOfException if $instanceof not match with service
  */
 public function getService($name, $instanceof = NULL)
 {
     $this->hasService($name, true);
     $s = $this->services[$name];
     if ($s->service === NULL) {
         if (is_string($s->factory) and !(strpos($s->factory, '::') or strncmp($s->factory, "lambda_", 8) === 0)) {
             $s->service = new $s->factory();
             unset($s->factory);
         } else {
             if (Callback::is($s->factory) or $s->factory instanceof Closure or is_string($s->factory) or is_array($s->factory)) {
                 $tmp = Callback::create($s->factory)->invoke($this);
                 if (!is_object($tmp)) {
                     $tmp = gettype($tmp);
                     throw new InvalidServiceFactoryException("Factory for service '{$name}' returns invalid result. Object expected, {$tmp} given.");
                 }
                 $s->service = $tmp;
                 unset($s->factory);
             } else {
                 if (is_object($s->factory)) {
                     $s->service = $s->factory;
                     unset($s->factory);
                 } else {
                     $tmp = gettype($s->factory);
                     throw new InvalidServiceFactoryException("Service '{$name}' has invalid factory. Callback, class name or object expected, {$tmp} given.");
                 }
             }
         }
     }
     if ($instanceof !== NULL and !$s->service instanceof $instanceof) {
         throw new ServiceNotInstanceOfException("Service '{$name}' is not instance of '{$instanceof}'.");
     }
     return $s->service;
 }
Exemplo n.º 2
0
 /**
  * @param IRepository
  * @param ArrayAccess
  */
 public function __construct(IRepository $repository, ArrayAccess $cache)
 {
     if (!static::$keyCallback) {
         return;
     }
     $this->repositoryClass = get_class($repository);
     if (self::$toLoad === NULL) {
         $key = static::$keyCallback ? (string) Callback::create(static::$keyCallback)->invoke() : NULL;
         $key = $key ? $key : '*';
         if (strlen($key) > 50) {
             $key = substr($key, 0, 20) . md5($key);
         }
         self::$toLoad = isset($cache[$key]) ? $cache[$key] : NULL;
         if (!self::$toLoad) {
             self::$toLoad = array();
         }
         if ($key === '*') {
             self::$toSave = self::$toLoad;
         }
         register_shutdown_function(function ($cache, $key) {
             // @codeCoverageIgnoreStart
             $cache[$key] = \Orm\PerformanceHelper::$toSave;
             // @codeCoverageIgnoreEnd
         }, $cache, $key);
     }
     if (!isset(self::$toSave[$this->repositoryClass])) {
         self::$toSave[$this->repositoryClass] = array();
     }
     $this->access =& self::$toSave[$this->repositoryClass];
 }
Exemplo n.º 3
0
 /**
  * @see self::create()
  * @param mixed class, object, function, callback
  * @param string method
  */
 protected function __construct($t, $m = NULL)
 {
     if ($m === NULL) {
         if (is_string($t)) {
             $t = explode('::', $t, 2);
             $this->cb = isset($t[1]) ? $t : $t[0];
         } else {
             if (is_object($t)) {
                 if ($t instanceof Closure) {
                     $this->cb = $t;
                 } else {
                     if (Callback::is($t)) {
                         $this->cb = $t->getNative();
                     } else {
                         $this->cb = array($t, '__invoke');
                     }
                 }
             } else {
                 $this->cb = $t;
             }
         }
     } else {
         $this->cb = array($t, $m);
     }
     if (!is_callable($this->cb, TRUE)) {
         throw new InvalidArgumentException("Invalid callback.");
     }
 }
Exemplo n.º 4
0
 /**
  * <code>
  * 	$p->register('mapper', 'Orm\IRepository', function ($repositoryClass) {
  * 		return $repositoryClass . 'Mapper';
  * 	});
  * </code>
  *
  * @param string
  * @param string interface name
  * @param Callback|Closure|NULL
  * @return AnnotationClassParser
  * @throws AnnotationClassParserException
  */
 public function register($annotation, $interface, $defaultClassFallback = NULL)
 {
     if (isset($this->registered[$annotation])) {
         throw new AnnotationClassParserException("Parser '{$annotation}' is already registered");
     }
     if (!interface_exists($interface)) {
         throw new AnnotationClassParserException("'{$interface}' is not valid interface");
     }
     if ($defaultClassFallback !== NULL and !is_callable($defaultClassFallback) and !Callback::is($defaultClassFallback)) {
         $tmp = is_string($defaultClassFallback) ? $defaultClassFallback : (is_object($defaultClassFallback) ? get_class($defaultClassFallback) : gettype($defaultClassFallback));
         throw new AnnotationClassParserException("'{$tmp}' is not valid callback");
     }
     $tmp = (object) array('annotation' => $annotation, 'interface' => $interface, 'defaultClassFallback' => $defaultClassFallback, 'cache' => array());
     $this->registered[$annotation] = $tmp;
     return $this;
 }
Exemplo n.º 5
0
 /**
  * @param IEntity
  * @param scalar|NULL id
  * @param string update|insert
  * @return array
  */
 protected function toArray(IEntity $entity, $id, $operation)
 {
     $values = $entity->toArray();
     if ($id !== NULL) {
         $values['id'] = $id;
     }
     $params = array('id' => isset($values['id'])) + (array) $this->params;
     $params += array_fill_keys(array_keys($values), true);
     if ($this->whichParams !== NULL) {
         $params = array('id' => $params['id']) + array_fill_keys($this->whichParams, true);
     }
     if ($this->whichParamsNot !== NULL) {
         $tmp = array_fill_keys($this->whichParamsNot, false);
         unset($tmp['id']);
         $params = $tmp + $params;
     }
     $arguments = array('params' => $params, 'values' => $values, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_BEFORE, $entity, $arguments);
     $params = $arguments['params'];
     $values = $arguments['values'];
     $result = array();
     foreach ($params as $key => $do) {
         if (array_key_exists($key, $values)) {
             $value = $values[$key];
         } else {
             // pokusi se precist, muze existovat getter, jinak vyhodi exception
             $value = $entity->{$key};
         }
         if ($do === false or $operation === 'update' and $entity->hasParam($key) and !$entity->isChanged($key)) {
             continue;
         }
         if ($do !== true) {
             $value = Callback::create($do)->invoke($value, $entity);
         }
         $result[$key] = $this->scalarizeValue($value, $key, $entity);
         if ($value instanceof IRelationship and $result[$key] === NULL) {
             unset($result[$key]);
         }
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_AFTER, $entity, $arguments);
     $result = $arguments['values'];
     $result = $this->conventional->formatEntityToStorage($result);
     $primaryKey = $this->conventional->getPrimaryKey();
     if ($primaryKey !== $this->primaryKey and array_key_exists($primaryKey, $result)) {
         $id = $result[$primaryKey];
         unset($result[$primaryKey]);
         $result = array($this->primaryKey => $id) + $result;
     }
     $arguments = array('values' => $result, 'operation' => $operation);
     $this->events->fireEvent(Events::SERIALIZE_CONVENTIONAL, $entity, $arguments);
     $result = $arguments['values'];
     return $result;
 }
Exemplo n.º 6
0
 /**
  * Upravi vstupni parametry pro enum, kdyz jsou zadavany jako string (napr. v anotaci)
  * Vytvori pole z hodnot rozdelenych carkou, umoznuje zapis konstant.
  * Nebo umoznuje zavolat statickou tridu ktera vrati pole hodnot (pouzijou se klice)
  *
  * <code>
  * 1, 2, 3
  * bla1, 'bla2', "bla3"
  * TRUE, false, NULL, self::CONSTANT, Foo::CONSTANT
  * self::tadyZiskejHodnoty()
  * </code>
  *
  * @param string
  * @return array
  * @see MetaDataProperty::setEnum()
  */
 public function builtParamsEnum($string)
 {
     if (preg_match('#^([a-z0-9_\\\\]+::[a-z0-9_]+)\\(\\)$#si', trim($string), $tmp)) {
         $enum = Callback::create($this->parseSelf($tmp[1]))->invoke();
         if (!is_array($enum)) {
             throw new AnnotationMetaDataException("'{$this->class}' '{enum {$string}}': callback must return array, " . (is_object($enum) ? get_class($enum) : gettype($enum)) . ' given');
         }
         $original = $enum = array_keys($enum);
     } else {
         $original = $enum = array();
         foreach (explode(',', $string) as $d) {
             $d = $this->parseSelf($d);
             $value = $this->parseString($d, "{enum {$string}}");
             $enum[] = $value;
             $original[] = $d;
         }
     }
     return array($enum, implode(', ', $original));
 }
Exemplo n.º 7
0
 /**
  * Inject same class around value in entity.
  *
  * <code>
  * 	$entity->foo = array('bar'); // call ArrayInjection::setInjectedValue(array('bar'))
  * 	$entity->foo implements ArrayInjection;
  *
  * 	$repo->persist($entity) // call ArrayInjection::getInjectedValue()
  * </code>
  *
  * Type must by class implements IEntityInjection.
  * @param Callback|Closure|string|IEntityInjectionLoader|NULL
  * null mean load from IEntityInjectionStaticLoader which is specify in type
  * @return MetaDataProperty $this
  * @see AnnotationMetaData::builtParamsInjection()
  */
 public function setInjection($factory = NULL)
 {
     if (isset($this->data['injection'])) {
         throw new MetaDataException("Already has injection in {$this->class}::\${$this->name}");
     }
     $class = $this->originalTypes;
     if (count($this->data['types']) != 1) {
         throw new MetaDataException("Injection expecte type as one class implements Orm\\IInjection, '{$class}' given in {$this->class}::\${$this->name}");
     }
     if (!class_exists($class)) {
         throw new MetaDataException("Injection expecte type as class implements Orm\\IInjection, '{$class}' given in {$this->class}::\${$this->name}");
     }
     $reflection = new ReflectionClass($class);
     $class = $reflection->getName();
     if (!$reflection->implementsInterface('Orm\\IEntityInjection')) {
         throw new MetaDataException("{$class} does not implements Orm\\IEntityInjection in {$this->class}::\${$this->name}");
     }
     if (!$reflection->isInstantiable()) {
         throw new MetaDataException("{$class} is abstract or not instantiable in {$this->class}::\${$this->name}");
     }
     if ($factory instanceof IEntityInjectionLoader) {
         $factory = Callback::create($factory, 'create');
     } else {
         if (Callback::is($factory) or $factory instanceof Closure or is_string($factory) and (strpos($factory, '::') or strncmp($factory, "lambda_", 8) === 0)) {
             $factory = Callback::create($factory);
         } else {
             if (!$factory and $reflection->implementsInterface('Orm\\IEntityInjectionStaticLoader')) {
                 $factory = Callback::create($class, 'create');
             } else {
                 if (!$factory) {
                     throw new MetaDataException("There is not factory callback for injection in {$this->class}::\${$this->name}, specify one or use Orm\\IEntityInjectionStaticLoader");
                 }
                 $tmp = is_object($factory) ? get_class($factory) : (is_string($factory) ? $factory : gettype($factory));
                 throw new MetaDataException("Injection expected valid callback, '{$tmp}' given in {$this->class}::\${$this->name}, specify one or use Orm\\IEntityInjectionStaticLoader");
             }
         }
     }
     $this->data['injection'] = InjectionFactory::create($factory, $class);
     return $this;
 }