Example #1
0
 /**
  * Begin defining a mock object, and setting its expectations
  * if $test is set, it will be the parent test object (tenative? is this good?)
  * @access public
  * @param string $class_name class name to create a mock of
  * @param UnitTest a unit test object that calls it (tenative)
  */
 public function __construct($class_name)
 {
     // $this->test = $test;
     $this->requires_inheritance = FALSE;
     $this->requires_magic_methods = FALSE;
     $this->requires_static_methods = FALSE;
     $this->use_extends = FALSE;
     $this->has_constructor = FALSE;
     $this->interface_names = array();
     $this->methods = array();
     $this->signatures = array();
     $this->constructor_args = array();
     $this->counters = array();
     $this->mocked_class = $class_name;
     $this->constructed_object = null;
     // do some quick reflection on the class
     $reflected_class = new ReflectionClass($this->mocked_class);
     if ($reflected_class->isInterface()) {
         $this->interface_names[] = $class_name;
     } else {
         $this->use_extends = TRUE;
     }
     if (count($reflected_class->getInterfaces()) > 0) {
         foreach ($reflected_class->getInterfaces() as $k => $interface) {
             $this->interface_names[] = $interface->getName();
         }
     }
 }
 /**
  * Get an array of loaded file names in order of loading.
  *
  * @return array
  */
 public function getFilenames()
 {
     $files = array();
     foreach ($this->classList->getClasses() as $class) {
         // Push interfaces before classes if not already loaded
         try {
             $r = new \ReflectionClass($class);
             foreach ($r->getInterfaces() as $inf) {
                 $name = $inf->getFileName();
                 if ($name && !in_array($name, $files)) {
                     $files[] = $name;
                 }
             }
             if (!in_array($r->getFileName(), $files)) {
                 $files[] = $r->getFileName();
             }
         } catch (\ReflectionException $e) {
             // We ignore all exceptions related to reflection,
             // because in some cases class can't exists. This
             // can be if you use in your code constructions like
             //
             // if (class_exists('SomeClass')) { // <-- here will trigger autoload
             //      class SomeSuperClass extends SomeClass {
             //      }
             // }
             //
             // We ignore all problems with classes, interfaces and
             // traits.
         }
     }
     return $files;
 }
 /**
  * Identify concrete extensible interface name based on the class name.
  *
  * @param string $extensibleClassName
  * @return string
  */
 public function getExtensibleInterfaceName($extensibleClassName)
 {
     $exceptionMessage = "Class '{$extensibleClassName}' must implement an interface, " . "which extends from '" . self::EXTENSIBLE_INTERFACE_NAME . "'";
     $notExtensibleClassFlag = '';
     if (isset($this->classInterfaceMap[$extensibleClassName])) {
         if ($notExtensibleClassFlag === $this->classInterfaceMap[$extensibleClassName]) {
             throw new \LogicException($exceptionMessage);
         } else {
             return $this->classInterfaceMap[$extensibleClassName];
         }
     }
     $modelReflection = new \ReflectionClass($extensibleClassName);
     if ($modelReflection->isInterface() && $modelReflection->isSubClassOf(self::EXTENSIBLE_INTERFACE_NAME) && $modelReflection->hasMethod('getExtensionAttributes')) {
         $this->classInterfaceMap[$extensibleClassName] = $extensibleClassName;
         return $this->classInterfaceMap[$extensibleClassName];
     }
     foreach ($modelReflection->getInterfaces() as $interfaceReflection) {
         if ($interfaceReflection->isSubclassOf(self::EXTENSIBLE_INTERFACE_NAME) && $interfaceReflection->hasMethod('getExtensionAttributes')) {
             $this->classInterfaceMap[$extensibleClassName] = $interfaceReflection->getName();
             return $this->classInterfaceMap[$extensibleClassName];
         }
     }
     $this->classInterfaceMap[$extensibleClassName] = $notExtensibleClassFlag;
     throw new \LogicException($exceptionMessage);
 }
 /**
  * Fetch all doc comments for inherit values
  *
  * @return string
  */
 private function fetchRecursiveDocComment()
 {
     $currentMethodName = $this->reflection->getName();
     $docCommentList[] = $this->reflection->getDocComment();
     // fetch all doc blocks for method from parent classes
     $docCommentFetched = $this->fetchRecursiveDocBlockFromParent($this->classReflection, $currentMethodName);
     if ($docCommentFetched) {
         $docCommentList = array_merge($docCommentList, $docCommentFetched);
     }
     // fetch doc blocks from interfaces
     $interfaceReflectionList = $this->classReflection->getInterfaces();
     foreach ($interfaceReflectionList as $interfaceReflection) {
         if (!$interfaceReflection->hasMethod($currentMethodName)) {
             continue;
         }
         $docCommentList[] = $interfaceReflection->getMethod($currentMethodName)->getDocComment();
     }
     $normalizedDocCommentList = array_map(function ($docComment) {
         $docComment = str_replace('/**', '', $docComment);
         $docComment = str_replace('*/', '', $docComment);
         return $docComment;
     }, $docCommentList);
     $docComment = '/**' . implode(PHP_EOL, $normalizedDocCommentList) . '*/';
     return $docComment;
 }
Example #5
0
 /**
  *
  *
  * @param unknown_type $data
  */
 public function convert($data)
 {
     if (is_object($data)) {
         if (class_exists('Doctrine_Record') && $data instanceof Doctrine_Record) {
             $adapter = new DoctrineRecordAdapter();
         } else {
             if (class_exists('Doctrine_Collection') && $data instanceof Doctrine_Collection) {
                 $adapter = new DoctrineCollectionAdapter();
             } else {
                 $reflector = new ReflectionClass(get_class($data));
                 $interfaces = $reflector->getInterfaces();
                 if (array_key_exists("Persistent", $interfaces)) {
                     $adapter = new PropelAdapter();
                 }
             }
         }
         if (isset($adapter)) {
             return $adapter->run($data);
         } else {
             return $data;
         }
     } else {
         if (is_array($data)) {
             return $this->iterateArray($data);
         } else {
             return $data;
         }
     }
 }
 /**
  * @inheritdoc
  */
 public function evaluate($fqcn, $description = '', $returnResult = FALSE)
 {
     $refl = new \ReflectionClass($fqcn);
     foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC) as $meth) {
         // we skip static public and magic method
         if ($meth->isStatic() || preg_match('#^__#', $meth->name)) {
             continue;
         }
         $found = false;
         // we seek in each interface
         foreach ($refl->getInterfaces() as $interf) {
             if ($interf->hasMethod($meth->name)) {
                 $found = true;
                 break;
             }
         }
         // if not found, error or exception
         if (!$found) {
             if ($returnResult) {
                 return false;
             } else {
                 $this->fail($fqcn, $description . PHP_EOL . $meth->name . " has no declaring interface");
             }
         }
     }
     return true;
 }
 /**
  * Create extension attributes object, custom for each extensible class.
  *
  * @param string $extensibleClassName
  * @param array $data
  * @return object
  */
 public function create($extensibleClassName, $data = [])
 {
     $modelReflection = new \ReflectionClass($extensibleClassName);
     $implementsExtensibleInterface = false;
     $extensibleInterfaceName = 'Magento\\Framework\\Api\\ExtensibleDataInterface';
     foreach ($modelReflection->getInterfaces() as $interfaceReflection) {
         if ($interfaceReflection->isSubclassOf($extensibleInterfaceName) && $interfaceReflection->hasMethod('getExtensionAttributes')) {
             $implementsExtensibleInterface = true;
             break;
         }
     }
     if (!$implementsExtensibleInterface) {
         throw new \LogicException("Class '{$extensibleClassName}' must implement an interface, " . "which extends from 'Magento\\Framework\\Api\\ExtensibleDataInterface'");
     }
     $methodReflection = $interfaceReflection->getMethod('getExtensionAttributes');
     if ($methodReflection->getDeclaringClass() == $extensibleInterfaceName) {
         throw new \LogicException("Method 'getExtensionAttributes' must be overridden in the interfaces " . "which extend 'Magento\\Framework\\Api\\ExtensibleDataInterface'. " . "Concrete return type should be specified.");
     }
     $interfaceName = '\\' . $interfaceReflection->getName();
     $extensionClassName = substr($interfaceName, 0, -strlen('Interface')) . 'Extension';
     $extensionInterfaceName = $extensionClassName . 'Interface';
     /** Ensure that proper return type of getExtensionAttributes() method is specified */
     $methodDocBlock = $methodReflection->getDocComment();
     $pattern = "/@return\\s+" . str_replace('\\', '\\\\', $extensionInterfaceName) . "/";
     if (!preg_match($pattern, $methodDocBlock)) {
         throw new \LogicException("Method 'getExtensionAttributes' must be overridden in the interfaces " . "which extend 'Magento\\Framework\\Api\\ExtensibleDataInterface'. " . "Concrete return type must be specified. Please fix :" . $interfaceName);
     }
     $extensionFactoryName = $extensionClassName . 'Factory';
     $extensionFactory = $this->_objectManager->create($extensionFactoryName);
     return $extensionFactory->create($data);
 }
Example #8
0
 /**
  * Get all interfaces used by this class
  *
  * @return    Nerd\Design\Enumerable      Enumerable array of interfaces used by this class
  */
 public function getInterfaces()
 {
     $interfaces = parent::getInterfaces();
     foreach ($interfaces as $key => $interface) {
         $interfaces[$key] = new Klass($interface->getName());
     }
     return new Collection($interfaces);
 }
Example #9
0
 /**
  * Get all implemented interfaces.
  *
  * @return ReflectionClass[] An associative `array` of interfaces, with keys as interface names and
  * the array values as `ReflectionClass` objects.
  */
 public function getInterfaces()
 {
     $interfaces = array();
     foreach (parent::getInterfaces() as $name => $interface) {
         $interfaces[$name] = new ReflectionClass($interface);
     }
     return $interfaces;
 }
 static function getInterfaces($name)
 {
     $reflection = new ReflectionClass($name);
     if ($reflection->isInterface()) {
         return array($name);
     }
     return self::_onlyParents($reflection->getInterfaces());
 }
 public function getInterfaces()
 {
     $result = array();
     foreach (parent::getInterfaces() as $interface) {
         $result[] = $this->createReflectionAnnotatedClass($interface);
     }
     return $result;
 }
 /**
  * Tests the Model Singleton Factory Method 
  */
 public function testGetInstance()
 {
     // Test Factory Method
     $model = Model::getInstance();
     // test assertion - Model::getInstance() non-null
     $this->assertNotNull($model, "Expecting instance not null");
     // utilize reflection for the interface test assertion
     $this->classReflector = new ReflectionClass($model);
     $interfaces = $this->classReflector->getInterfaces();
     foreach ($interfaces as $interface) {
         $hasInterface = $interface->name == 'IModel' ? true : false;
         if ($hasInterface) {
             break;
         }
     }
     // test assertion - Model implement IModel
     $this->assertTrue($hasInterface, 'Class instance implements IModel');
 }
 /**
  * Replacement for the original getInterfaces() method which makes sure
  * that \TYPO3\Flow\Reflection\ClassReflection objects are returned instead of the
  * original ReflectionClass instances.
  *
  * @return array of \TYPO3\Flow\Reflection\ClassReflection Class reflection objects of the properties in this class
  */
 public function getInterfaces()
 {
     $extendedInterfaces = array();
     $interfaces = parent::getInterfaces();
     foreach ($interfaces as $interface) {
         $extendedInterfaces[] = new ClassReflection($interface->getName());
     }
     return $extendedInterfaces;
 }
 /**
  * @param \ReflectionClass $reflectionClass
  *
  * @return LegacyMethodTag[]|Method[]
  */
 private function getInterfacesTagList(\ReflectionClass $reflectionClass)
 {
     $interfaces = $reflectionClass->getInterfaces();
     $tagList = array();
     foreach ($interfaces as $interface) {
         $tagList = array_merge($tagList, $this->classRetriever->getTagList($interface));
     }
     return $tagList;
 }
Example #15
0
 public static function implementsInterface($className, $interfaceName)
 {
     $r = new ReflectionClass($className);
     foreach ($r->getInterfaces() as $in) {
         if (strtolower($in->getName()) == strtolower($interfaceName)) {
             return true;
         }
     }
     return false;
 }
Example #16
0
 public function getInterfaces()
 {
     $phpReflections = parent::getInterfaces();
     $zendReflections = array();
     while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
         $zendReflections[] = new Zend_Reflection_Class($phpReflection->getName());
         unset($phpReflection);
     }
     unset($phpReflections);
     return $zendReflections;
 }
Example #17
0
 /**
  * Функция - обработчик команды, создает объект класса команды и передает его слушателям
  * @param $command
  * @param \ArrayAccess $commandData
  */
 public function handle($command, \ArrayAccess $commandData = null)
 {
     $class = new \ReflectionClass($command);
     $this->object = $class->newInstanceArgs($commandData);
     /* Если у класса нет интерфейсов, то выполняем его в контексте класса selfHanding, иначе кидаем подписчикам, реализующим интерфейсы */
     if (empty($class->getInterfaces())) {
         SelfHandler::getInstance()->handle($this->object);
     } else {
         $this->notify();
     }
 }
Example #18
0
 protected function addDependency(array &$loadPaths, \ReflectionClass $dep)
 {
     if ($dep->getFileName() !== false) {
         $loadPaths[$dep->getName()] = $dep->getFileName();
     }
     if ($dep->getParentClass() instanceof \ReflectionClass) {
         $this->addDependency($loadPaths, $dep->getParentClass());
     }
     foreach ($dep->getInterfaces() as $interface) {
         $this->addDependency($loadPaths, $interface);
     }
 }
 /**
  * Gets a list of dependancys.
  *
  * This will generally be an array of class/interface names.
  *
  * @return Traversable
  */
 public function getDependancys()
 {
     $deps = array();
     if ($this->reflection->getParentClass()) {
         $deps[] = $this->reflection->getParentClass()->getName();
     }
     foreach ($this->reflection->getInterfaces() as $i) {
         $deps[] = $i->getName();
     }
     $deps = array_unique($deps);
     return new ArrayObject($deps);
 }
 public static function accept($instance)
 {
     $reflectionClass = new \ReflectionClass(static::class);
     $interfaceList = $reflectionClass->getInterfaces();
     $accept = true;
     if (!empty($interfaceList)) {
         /** @var $interface \ReflectionClass */
         $interface = reset($interfaceList);
         $accept = $interface->isInstance($instance);
     }
     return $accept;
 }
 /**
  * @return IReflectionReflection[]
  * @throws ReflectionException
  */
 public function getImplementsList()
 {
     if ($this->isInterface()) {
         throw new ReflectionException(sprintf('Requested implement list on the interface [%s]. That is not so good.', $this->getName()));
     }
     if ($this->implementsList === null) {
         $this->implementsList = [];
         foreach ($this->reflectionClass->getInterfaces() as $interface) {
             $this->implementsList[$interface->getName()] = new ReflectionReflection($interface);
         }
     }
     return $this->implementsList;
 }
Example #22
0
 /**
  * You can modify the container here before it is dumped to PHP code.
  *
  * @param ContainerBuilder $container
  */
 public function process(ContainerBuilder $container)
 {
     $resolver = $container->getDefinition('warlock.interface.resolver');
     $bindings = array();
     foreach ($container->getDefinitions() as $serviceId => $definition) {
         $reflector = new \ReflectionClass($definition->getClass());
         $interfaces = $reflector->getInterfaces();
         foreach ($interfaces as $interface) {
             $bindings[$interface->name][] = $serviceId;
         }
     }
     $resolver->addMethodCall('addBindings', array($bindings));
 }
 /**
  * Inspect interface.
  *
  * @return void
  */
 protected function inspectInterface()
 {
     $interfaces = $this->class->getInterfaces();
     if (!empty($interfaces)) {
         $isInterface = $this->class->isInterface();
         foreach ($interfaces as $interface) {
             if ($isInterface) {
                 $this->inspection['extends'][] = $this->getInheritanceInspection($interface);
             } else {
                 $this->inspection['implements'][] = $this->getInheritanceInspection($interface);
             }
         }
     }
 }
Example #24
0
 /**
  * Get all behavior of an adapter.
  *
  * @param Adapter $adapter
  *
  * @return string[]
  */
 public function allFromAdapter(Adapter $adapter)
 {
     if ($adapter instanceof KnowsItsBehaviors) {
         return $adapter->getBehaviors();
     }
     $rfl = new \ReflectionClass($adapter);
     $behaviors = array();
     foreach ($rfl->getInterfaces() as $interface) {
         if (true === $interface->isSubclassOf('Gaufrette\\Core\\Adapter\\Behavior')) {
             $behaviors[] = $interface->getName();
         }
     }
     return $behaviors;
 }
Example #25
0
 /**
  * Create a class.
  *
  * @param string $class_name
  *   The class name.
  * @param array $data
  *   An array of data.
  *
  * @throws \KeyParty\Exception\KeyPartyException
  * @return mixed
  *   The class, or NULL.
  */
 public function createSupportedObject($class_name, $data)
 {
     $new_object = NULL;
     $reflection = new \ReflectionClass($class_name);
     $interfaces = $reflection->getInterfaces();
     // Attempt to write data using the compatibility interfaces first.
     if (in_array('KeyParty\\Converter\\ConvertableObjectInterface', $interfaces)) {
         $new_object = new $class_name();
         /* @var ConvertableObjectInterface $new_object */
         $new_object->keyPartySetPropertiesFromData($data);
         return $new_object;
     }
     // Try to create the object 'manually'.
     return $this->createObject($class_name, $data);
 }
 function getInterfaces()
 {
     if ($this->isTop) {
         $interfaces = parent::getInterfaces();
     } else {
         if ($this->superParent) {
             $interfaces = $this->superParent->getInterfaces();
         } else {
             $interfaces = array();
         }
     }
     foreach ($interfaces as &$i) {
         $i = new self($i->name);
     }
     return $interfaces;
 }
Example #27
0
 protected static function loadConsumers()
 {
     $cachePath = kConf::get('cache_root_path') . '/EventConsumers.cache';
     if (file_exists($cachePath)) {
         self::$consumers = unserialize(file_get_contents($cachePath));
         return;
     }
     $coreConsumers = kConf::get('event_consumers');
     $pluginConsumers = array();
     $pluginInstances = KalturaPluginManager::getPluginInstances('IKalturaEventConsumers');
     foreach ($pluginInstances as $pluginInstance) {
         foreach ($pluginInstance->getEventConsumers() as $pluginConsumer) {
             $pluginConsumers[] = $pluginConsumer;
         }
     }
     $consumers = array_merge($coreConsumers, $pluginConsumers);
     $consumersLists = array();
     foreach ($consumers as $consumer) {
         if (!class_exists($consumer)) {
             continue;
         }
         $clazz = new ReflectionClass($consumer);
         $interfaces = $clazz->getInterfaces();
         foreach ($interfaces as $interface) {
             if ($interface->name == self::BASE_CONSUMER_INTERFACE) {
                 continue;
             }
             if (!$interface->implementsInterface(self::BASE_CONSUMER_INTERFACE)) {
                 continue;
             }
             if (!isset($consumersLists[$interface->name])) {
                 $consumersLists[$interface->name] = array();
             }
             $consumersLists[$interface->name][] = $consumer;
         }
     }
     foreach ($consumersLists as $interfaceName => $interfaceConsumersArray) {
         usort($interfaceConsumersArray, array('kEventsManager', 'compareConsumers'));
         self::$consumers[$interfaceName] = $interfaceConsumersArray;
     }
     $cacheDir = dirname($cachePath);
     if (!file_exists($cacheDir)) {
         kFile::fullMkfileDir($cacheDir, 0777, true);
     }
     @file_put_contents($cachePath, serialize(self::$consumers));
 }
 protected function recursivelyIntrospectInterface($interfaceName)
 {
     $allInterfaces = [];
     $reflectionInterface = new \ReflectionClass($interfaceName);
     $interfaces = $reflectionInterface->getInterfaces();
     foreach ($interfaces as $interface) {
         $interfaceList = $this->recursivelyIntrospectInterface($interface);
         $allInterfaces = array_merge($allInterfaces, $interfaceList);
     }
     while (($parentInterface = $reflectionInterface->getParentClass()) != false) {
         $allInterfaces[] = $parentInterface->getName();
         $interfaceList = $this->recursivelyIntrospectInterface($parentInterface->getName());
         $allInterfaces = array_merge($allInterfaces, $interfaceList);
         $reflectionInterface = $parentInterface;
     }
     return $allInterfaces;
 }
Example #29
0
 private function findMatchingProvider($class)
 {
     $reflection = new \ReflectionClass($class);
     while ($reflection) {
         $normalized = $this->normalizeClass($reflection->getName());
         if (array_key_exists($normalized, $this->providers)) {
             return $this->providers[$normalized];
         }
         $reflection = $reflection->getParentClass();
     }
     $reflection = new \ReflectionClass($class);
     foreach ($reflection->getInterfaces() as $interface) {
         $normalized = $this->normalizeClass($interface->getName());
         if (array_key_exists($normalized, $this->providers)) {
             return $this->providers[$normalized];
         }
     }
     return $this->providers['stdclass'];
 }
 /**
  * Discover Magical API
  *
  * @param ClassNode $node
  */
 public function apply(ClassNode $node)
 {
     $parentClass = $node->getParentClass();
     $reflectionClass = new \ReflectionClass($parentClass);
     $phpdoc = new DocBlock($reflectionClass->getDocComment());
     $tagList = $phpdoc->getTagsByName('method');
     $interfaces = $reflectionClass->getInterfaces();
     foreach ($interfaces as $interface) {
         $phpdoc = new DocBlock($interface);
         $tagList = array_merge($tagList, $phpdoc->getTagsByName('method'));
     }
     foreach ($tagList as $tag) {
         $methodName = $tag->getMethodName();
         if (!$reflectionClass->hasMethod($methodName)) {
             $methodNode = new MethodNode($tag->getMethodName());
             $methodNode->setStatic($tag->isStatic());
             $node->addMethod($methodNode);
         }
     }
 }