public function init()
 {
     if (!ClassChecker::exists($this->value)) {
         throw new UnexpectedValueException(sprintf('Class `%s` not found on @EntityManager annotation, on model `%s`', $this->value, $this->name));
     }
     $this->getEntity()->entityManager = $this->value;
 }
Example #2
0
 private static function _resolve(DocumentPropertyMeta $meta, DocumentTypeMeta $modelMeta)
 {
     // Sanitizer is explicitly set
     if (!empty($meta->sanitizer)) {
         if (is_array($meta->sanitizer)) {
             $name = $meta->sanitizer['class'];
         } else {
             $name = $meta->sanitizer;
         }
         // If short name is used add default namespace
         if (strstr($name, '\\') === false) {
             $className = sprintf('%s\\%s', PassThrough::Ns, $name);
         } else {
             $className = $name;
         }
         if (!ClassChecker::exists($className)) {
             throw new ManganException(sprintf("Sanitizer class `%s` not found for field `%s` in model `%s`", $className, $meta->name, $modelMeta->name));
         }
         return $className;
     }
     // Guess sanitizer
     switch (gettype($meta->default)) {
         case 'boolean':
             return BooleanSanitizer::class;
         case 'double':
             return DoubleSanitizer::class;
         case 'integer':
             return IntegerSanitizer::class;
         case 'string':
             return StringSanitizer::class;
     }
     // Fallback to passthrough
     return PassThrough::class;
 }
 public function decorate(MatcherInterface $matcher, &$value)
 {
     $reflection = ReflectionHelper::getReflectionClass($matcher->getPlugins()->reflection);
     $resolved = UseResolver::resolve($reflection, $value);
     if (ClassChecker::exists($resolved)) {
         $value = $resolved;
     }
 }
 public function matches($string, &$value)
 {
     $matches = [];
     $regex = '([A-Z\\\\][a-zA-Z0-9_\\\\]+)';
     if (preg_match("/^{$regex}/", $string, $matches)) {
         $value = $this->process($matches);
         if (!ClassChecker::exists($value)) {
             return false;
         }
         return strlen($matches[0]);
     }
     $value = false;
     return false;
 }
 public function init()
 {
     $data = ParamsExpander::expand($this, ['class']);
     $class = '';
     if (isset($data['class'])) {
         $class = $data['class'];
     }
     // Log only, as it is designed as soft-fail
     if (empty($class) || !ClassChecker::exists($class)) {
         (new Signal())->getLogger()->warning(sprintf('Class not found for SignalFor annotation on model `%s`', $this->getMeta()->type()->name));
         return;
     }
     NameNormalizer::normalize($class);
     $this->getEntity()->signalFor[] = $class;
 }
Example #6
0
 public static function ensureClass($model, $name, &$dbValue)
 {
     if (!is_array($dbValue) || !array_key_exists('_class', $dbValue) || empty($dbValue['_class'])) {
         $class = static::getClassName($model, $name);
     } else {
         $class = $dbValue['_class'];
     }
     if (!ClassChecker::exists($class)) {
         $event = new ClassNotFound($model);
         $event->notFound = $class;
         if (Event::hasHandler($model, NotFoundResolver::EventClassNotFound) && Event::handled($model, NotFoundResolver::EventClassNotFound, $event)) {
             $class = $event->replacement;
         } else {
             throw new ManganException(sprintf("Embedded model class `%s` not found in model `%s` field `%s`", $class, get_class($model), $name));
         }
     }
     $dbValue['_class'] = $class;
 }
Example #7
0
 /**
  * Check target constraints
  * @param AnnotationInterface $annotation Annotation
  * @param ReflectionClass|ReflectionMethod|ReflectionProperty|bool $target
  * @return type
  * @throws TargetException
  */
 public static function check($annotation, $target)
 {
     $reflection = new ReflectionAnnotatedClass($annotation);
     if (!$reflection->hasAnnotation('Target')) {
         return;
     }
     $value = $reflection->getAnnotation('Target')->value;
     $values = is_array($value) ? $value : [$value];
     foreach ($values as $value) {
         if ($value == TargetAnnotation::TargetClass && $target instanceof ReflectionClass) {
             return;
         }
         if ($value == TargetAnnotation::TargetMethod && $target instanceof ReflectionMethod) {
             return;
         }
         if ($value == TargetAnnotation::TargetProperty && $target instanceof ReflectionProperty) {
             return;
         }
         if ($value == TargetAnnotation::TargetNested && $target === false) {
             return;
         }
     }
     if ($target !== false && $value && !in_array($value, [TargetAnnotation::TargetClass, TargetAnnotation::TargetMethod, TargetAnnotation::TargetProperty, TargetAnnotation::TargetNested])) {
         if ($target instanceof ReflectionClass) {
             $interfaceTarget = $target;
         } else {
             /* @var $target ReflectionProperty */
             $interfaceTarget = new ReflectionClass($target->class);
         }
         if (!ClassChecker::exists($value)) {
             throw new TargetException(sprintf('Annotation "%s" used in "%s" is only allowed on instances of "%s", but this class does not exists (see @Target)', basename($reflection->name), $interfaceTarget->name, $value));
         }
         if (!$interfaceTarget->implementsInterface($value)) {
             throw new TargetException(sprintf('Annotation "%s" used in "%s" is only allowed on instances of "%s" (see @Target)', basename($reflection->name), $interfaceTarget->name, $value));
         }
     }
     if ($target === false && $value == TargetAnnotation::TargetNested) {
         throw new TargetException("Annotation '" . get_class($annotation) . "' nesting not allowed");
     } elseif (in_array($value, TargetAnnotation::getTargets())) {
         throw new TargetException(sprintf("Annotation '%s' not allowed on %s, it's target is %s  (see @Target)", get_class($annotation), ReflectionName::createName($target), $value));
     }
 }
Example #8
0
 public function init()
 {
     $params = ['class'];
     if (is_string($this->value)) {
         $this->class = $this->value;
     } elseif (is_array($this->value)) {
         foreach (array_keys($this->value) as $key) {
             if (!is_numeric($key)) {
                 $params[] = $key;
             }
         }
     }
     $config = ParamsExpander::expand($this, $params);
     if (empty($config['class'])) {
         throw new UnexpectedValueException(sprintf('@Sanitizer expects class name for model `%s` field `%s`', $this->getMeta()->type()->name, $this->getEntity()->name));
     } elseif ($config['class'] !== 'None' && !ClassChecker::exists($config['class']) && !ClassChecker::exists(sprintf('%s\\%s', PassThrough::Ns, $config['class']))) {
         throw new UnexpectedValueException(sprintf('Class `%s` for @Sanitizer not found on model `%s` field `%s`', $config['class'], $this->getMeta()->type()->name, $this->getEntity()->name));
     }
     $this->getEntity()->sanitizer = $config;
 }
Example #9
0
 public static function create(Signal $signals, $signal, $fqn, $injection)
 {
     // Clone signal, as it might be modified by slot
     $cloned = clone $signal;
     // Constructor injection
     if (true === $injection) {
         $slot = new $fqn($cloned);
         // Slot aware call
         if ($cloned instanceof SlotAwareInterface) {
             $cloned->setSlot($slot);
         }
         return $cloned;
     }
     // Check if class exists and log if doesn't
     // @codeCoverageIgnoreStart
     if (!ClassChecker::exists($fqn)) {
         $signals->getLogger()->debug(sprintf("Class `%s` not found while emiting signal `%s`", $fqn, get_class($signal)));
         return false;
     }
     // @codeCoverageIgnoreEnd
     // Other type injection
     $slot = new $fqn();
     // Slot aware call
     if ($cloned instanceof SlotAwareInterface) {
         $cloned->setSlot($slot);
     }
     if (strstr($injection, '()')) {
         // Method injection
         $methodName = str_replace('()', '', $injection);
         $slot->{$methodName}($cloned);
     } else {
         // field injection
         $slot->{$injection} = $cloned;
     }
     return $cloned;
 }
Example #10
0
 /**
  * Call for signals from slot
  * @param object $slot
  * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot
  */
 public function gather($slot, $interface = null)
 {
     $name = get_class($slot);
     NameNormalizer::normalize($name);
     if (!empty($interface)) {
         NameNormalizer::normalize($interface);
     }
     if (empty(self::$config)) {
         $this->init();
     }
     if (!isset(self::$config[self::Slots][$name])) {
         self::$config[self::Slots][$name] = [];
         $this->loggerInstance->debug('No signals found for slot `{name}`, skipping', ['name' => $name]);
     }
     $result = [];
     foreach ((array) self::$config[self::Slots][$name] as $fqn => $emit) {
         if (false === $emit) {
             continue;
         }
         if (!PreFilter::filter($this, $fqn, $slot)) {
             continue;
         }
         // Check if class exists and log if doesn't
         if (!ClassChecker::exists($fqn)) {
             $this->loggerInstance->debug(sprintf("Class `%s` not found while gathering slot `%s`", $fqn, get_class($slot)));
             continue;
         }
         if (null === $interface) {
             $injected = new $fqn();
             if (!PostFilter::filter($this, $injected, $slot)) {
                 continue;
             }
             $result[] = $injected;
             continue;
         }
         // Check if it's same as interface
         if ($fqn === $interface) {
             $injected = new $fqn();
             if (!PostFilter::filter($this, $injected, $slot)) {
                 continue;
             }
             $result[] = $injected;
             continue;
         }
         $info = new ReflectionClass($fqn);
         // Check if class is instance of base class
         if ($info->isSubclassOf($interface)) {
             $injected = new $fqn();
             if (!PostFilter::filter($this, $injected, $slot)) {
                 continue;
             }
             $result[] = $injected;
             continue;
         }
         $interfaceInfo = new ReflectionClass($interface);
         // Check if class implements interface
         if ($interfaceInfo->isInterface() && $info->implementsInterface($interface)) {
             $injected = new $fqn();
             if (!PostFilter::filter($this, $injected, $slot)) {
                 continue;
             }
             $result[] = $injected;
             continue;
         }
     }
     return $result;
 }
Example #11
0
 /**
  * Get class name
  * @param AnnotatedInterface|object|string $class
  * @return string
  */
 private static function getName($class)
 {
     if (is_object($class)) {
         $class = get_class($class);
     } else {
         if (!ClassChecker::exists($class)) {
             throw new UnexpectedValueException(sprintf("Class `%s` not found", $class));
         }
     }
     return ltrim($class, '\\');
 }
Example #12
0
 public function has($name)
 {
     return ClassChecker::exists($name);
 }
Example #13
0
 /**
  * Create new instance of annotation
  * @param string $class
  * @param mixed[] $parameters
  * @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $targetReflection
  * @return boolean|object
  */
 public function instantiateAnnotation($class, $parameters, $targetReflection = false)
 {
     $class = ucfirst($class) . "Annotation";
     // If namespaces are empty assume global namespace
     $fqn = $this->normalizeFqn('\\', $class);
     foreach ($this->addendum->namespaces as $ns) {
         $fqn = $this->normalizeFqn($ns, $class);
         if (Blacklister::ignores($fqn)) {
             continue;
         }
         try {
             if (!ClassChecker::exists($fqn)) {
                 $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]);
                 Blacklister::ignore($fqn);
             } else {
                 // Class exists, exit loop
                 break;
             }
         } catch (Exception $e) {
             // Ignore class autoloading errors
         }
     }
     if (Blacklister::ignores($fqn)) {
         return false;
     }
     try {
         // NOTE: was class_exists here, however should be safe to use ClassChecker::exists here
         if (!ClassChecker::exists($fqn)) {
             $this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]);
             Blacklister::ignore($fqn);
             return false;
         }
     } catch (Exception $e) {
         // Ignore autoload errors and return false
         Blacklister::ignore($fqn);
         return false;
     }
     $resolvedClass = Addendum::resolveClassName($fqn);
     if ((new ReflectionClass($resolvedClass))->implementsInterface(AnnotationInterface::class) || $resolvedClass == AnnotationInterface::class) {
         return new $resolvedClass($parameters, $targetReflection);
     }
     return false;
 }
 /**
  * Register event handlers for child item of parent-child relation.
  *
  * @param AnnotatedInterface $child
  * @param string $parentClass
  * @throws UnexpectedValueException
  */
 public function registerChild(AnnotatedInterface $child, $parentClass)
 {
     if (!ClassChecker::exists($parentClass)) {
         throw new UnexpectedValueException(sprintf('Class `%s` not found', $parentClass));
     }
     // Prevent restoring item if parent does not exists
     $beforeRestore = function (ModelEvent $event) use($child, $parentClass) {
         $model = $event->sender;
         if ($model instanceof $child) {
             $parent = new $parentClass();
             $criteria = new Criteria(null, $parent);
             $criteria->_id = $model->parentId;
             if (!$parent->exists($criteria)) {
                 $event->isValid = false;
                 return false;
             }
         }
         $event->isValid = true;
     };
     Event::on($child, TrashInterface::EventBeforeRestore, $beforeRestore);
 }