Пример #1
0
 public function testClassScannerReturnsPropertyWithNoDefault()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/BazClass.php');
     $class = $file->getClass('BazClass');
     $method = $class->getMethod('__construct');
     $this->assertTrue($method->isPublic());
 }
Пример #2
0
 public function getMapDataForFile($file)
 {
     $file = realpath($file);
     $datas = array();
     $fs = new FileScanner($file);
     // problem in TokenArrayScanner.php line #579, needs fix (notice undefined offset 1)
     @($classes = $fs->getClassNames());
     foreach ($classes as $class) {
         $newNamespace = str_replace('_', '\\', substr($class, 0, strrpos($class, '_')));
         if (strpos($class, '_') !== false) {
             $newClass = substr($class, strrpos($class, '_') + 1);
         } else {
             $newClass = $class;
         }
         $rootDir = $this->findRootDirectory($file, $class);
         if ($newNamespace) {
             $newFile = $rootDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $newNamespace) . DIRECTORY_SEPARATOR . $newClass . '.php';
         } else {
             $newFile = $file;
         }
         //$root = substr($file, 0, strpos($file, str_replace('\\', DIRECTORY_SEPARATOR, $newNamespace)));
         $data = array('root_directory' => $rootDir, 'original_class' => $class, 'original_file' => $file, 'new_namespace' => $newNamespace, 'new_class' => $newClass, 'new_file' => $newFile);
         // per-file transformations
         $this->transformInterfaceName($data);
         $this->transformAbstractName($data);
         $this->transformReservedWords($data);
         $datas[] = $data;
         // per-set transformationss
     }
     return $datas;
 }
Пример #3
0
 /**
  * Compile class definitions
  *
  * @param string $path
  * @param bool $validate
  * @return void
  */
 public function compile($path, $validate = true)
 {
     $rdi = new \RecursiveDirectoryIterator(realpath($path));
     $recursiveIterator = new \RecursiveIteratorIterator($rdi, 1);
     /** @var $item \SplFileInfo */
     foreach ($recursiveIterator as $item) {
         if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') {
             $fileScanner = new FileScanner($item->getRealPath());
             $classNames = $fileScanner->getClassNames();
             foreach ($classNames as $className) {
                 $this->_current = $className;
                 if (!class_exists($className)) {
                     require_once $item->getRealPath();
                 }
                 try {
                     if ($validate) {
                         $this->_validator->validate($className);
                     }
                     $signatureReader = new \Magento\Framework\Code\Reader\ClassReader();
                     $this->_definitions[$className] = $signatureReader->getConstructor($className);
                     $this->_relations[$className] = $signatureReader->getParents($className);
                 } catch (\Magento\Framework\Code\ValidationException $exception) {
                     $this->_log->add(Log::COMPILATION_ERROR, $className, $exception->getMessage());
                 } catch (\ReflectionException $e) {
                     $this->_log->add(Log::COMPILATION_ERROR, $className, $e->getMessage());
                 }
                 $this->_processedClasses[$className] = 1;
             }
         }
     }
 }
Пример #4
0
 public function testPropertyScannerHasPropertyInformation()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\FooClass');
     $property = $class->getProperty('bar');
     $this->assertEquals('bar', $property->getName());
     $this->assertEquals('value', $property->getValue());
     $this->assertFalse($property->isPublic());
     $this->assertTrue($property->isProtected());
     $this->assertFalse($property->isPrivate());
     $this->assertTrue($property->isStatic());
     $property = $class->getProperty('foo');
     $this->assertEquals('foo', $property->getName());
     $this->assertEquals('value2', $property->getValue());
     $this->assertTrue($property->isPublic());
     $this->assertFalse($property->isProtected());
     $this->assertFalse($property->isPrivate());
     $this->assertFalse($property->isStatic());
     $property = $class->getProperty('baz');
     $this->assertEquals('baz', $property->getName());
     $this->assertEquals(3, $property->getValue());
     $this->assertFalse($property->isPublic());
     $this->assertFalse($property->isProtected());
     $this->assertTrue($property->isPrivate());
     $this->assertFalse($property->isStatic());
 }
Пример #5
0
    /**
     * Retrieves list of classes for given path
     *
     * @param string $path
     * @return array
     * @throws FileSystemException
     */
    public function getList($path)
    {
        $realPath = realpath($path);
        if (!(bool)$realPath) {
            throw new FileSystemException(new \Magento\Framework\Phrase('Invalid path: %1', [$path]));
        }
        $recursiveIterator = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        $classes = [];
        foreach ($recursiveIterator as $fileItem) {
            /** @var $fileItem \SplFileInfo */
            if ($fileItem->getExtension() !== 'php') {
                continue;
            }
            foreach ($this->excludePatterns as $excludePattern) {
                if (preg_match($excludePattern, $fileItem->getRealPath())) {
                    continue 2;
                }
            }
            $fileScanner = new FileScanner($fileItem->getRealPath());
            $classNames = $fileScanner->getClassNames();
            foreach ($classNames as $className) {
                if (!class_exists($className)) {
                    require_once $fileItem->getRealPath();
                }
                $classes[] = $className;
            }
        }
        return $classes;
    }
Пример #6
0
 public function testMethodScannerReturnsBodyMethods()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/BarClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\BarClass');
     $method = $class->getMethod('three');
     $expected = "\n" . '        $x = 5 + 5;' . "\n" . '        $y = \'this string\';' . "\n    ";
     $this->assertEquals($expected, $method->getBody());
 }
Пример #7
0
 public function testFileScannerReturnsClassesWithClassScanner()
 {
     $fileScanner = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php');
     $classes = $fileScanner->getClasses(true);
     $this->assertInternalType('array', $classes);
     foreach ($classes as $class) {
         $this->assertInstanceOf('Zend\\Code\\Scanner\\ClassScanner', $class);
     }
 }
Пример #8
0
 public function testMethodScannerMethodSignatureLatestOptionalParamHasParentheses()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/BarClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\BarClass');
     $method = $class->getMethod('four');
     $paramTwo = $method->getParameter(1);
     $optionalValue = $paramTwo->getDefaultValue();
     $this->assertEquals('array(array(array(\'default\')))', $optionalValue);
 }
Пример #9
0
 public function testClassScannerReturnsMethodsWithMethodScanners()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\FooClass');
     $methods = $class->getMethods(true);
     foreach ($methods as $method) {
         $this->assertInstanceOf('Zend\\Code\\Scanner\\MethodScanner', $method);
     }
 }
Пример #10
0
 public function testClassScannerCanReturnLineNumbers()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\FooClass');
     $this->assertEquals(11, $class->getLineStart());
     $this->assertEquals(23, $class->getLineEnd());
     $file = new FileScanner(__DIR__ . '/../TestAsset/BarClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\BarClass');
     $this->assertEquals(10, $class->getLineStart());
     $this->assertEquals(33, $class->getLineEnd());
 }
Пример #11
0
 /**
  * @return AnnotationCollection
  */
 public function getAnnotations(Annotation\AnnotationManager $annotationManager)
 {
     if (($docComment = $this->getDocComment()) == '') {
         return false;
     }
     if (!$this->annotations) {
         $fileScanner = new FileScanner($this->getFileName());
         $nameInformation = $fileScanner->getClassNameInformation($this->getDeclaringClass()->getName());
         $this->annotations = new AnnotationScanner($annotationManager, $docComment, $nameInformation);
     }
     return $this->annotations;
 }
Пример #12
0
 public function testConstantScannerHasConstantInformation()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/FooClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\FooClass');
     $constant = $class->getConstant('BAR');
     $this->assertEquals('BAR', $constant->getName());
     $this->assertEquals(5, $constant->getValue());
     $constant = $class->getConstant('FOO');
     $this->assertEquals('FOO', $constant->getName());
     $this->assertEquals(5, $constant->getValue());
     $constant = $class->getConstant('BAZ');
     $this->assertEquals('BAZ', $constant->getName());
     $this->assertEquals('baz', $constant->getValue());
     $this->assertNotNull('Some comment', $constant->getDocComment());
 }
Пример #13
0
 public function testParameterScannerHasParameterInformation()
 {
     $file = new FileScanner(__DIR__ . '/../TestAsset/BarClass.php');
     $class = $file->getClass('ZendTest\\Code\\TestAsset\\BarClass');
     $method = $class->getMethod('three');
     $parameter = $method->getParameter('t');
     $this->assertEquals('ZendTest\\Code\\TestAsset\\BarClass', $parameter->getDeclaringClass());
     $this->assertEquals('three', $parameter->getDeclaringFunction());
     $this->assertEquals('t', $parameter->getName());
     $this->assertEquals(2, $parameter->getPosition());
     $this->assertEquals('2', $parameter->getDefaultValue());
     $this->assertFalse($parameter->isArray());
     $this->assertTrue($parameter->isDefaultValueAvailable());
     $this->assertTrue($parameter->isOptional());
     $this->assertTrue($parameter->isPassedByReference());
 }
Пример #14
0
 /**
  * Retrieves list of classes and arguments for given path
  * [CLASS NAME => ConstructorArgument[]]
  *
  * @param string $path
  * @return array
  * @throws FilesystemException
  */
 public function getList($path)
 {
     $realPath = realpath($path);
     if (!(bool) $realPath) {
         throw new FilesystemException();
     }
     $classes = [];
     $recursiveIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS), \RecursiveIteratorIterator::SELF_FIRST);
     /** @var $fileItem \SplFileInfo */
     foreach ($recursiveIterator as $fileItem) {
         if (!$this->isPhpFile($fileItem)) {
             continue;
         }
         $fileScanner = new FileScanner($fileItem->getRealPath());
         $classNames = $fileScanner->getClassNames();
         foreach ($classNames as $className) {
             if (!class_exists($className)) {
                 require_once $fileItem->getRealPath();
             }
             $classes[$className] = $this->classReaderDecorator->getConstructor($className);
         }
     }
     return $classes;
 }
 /**
  * 
  * @param ModuleEvent $event
  */
 public function onMergeConfig(ModuleEvent $event)
 {
     $config = $event->getConfigListener()->getMergedConfig(false);
     $resolvedControllers = $this->resolveControllers($config);
     $controllerInvokables = empty($config['controllers']['invokables']) ? [] : $config['controllers']['invokables'];
     $controllerFactories = empty($config['controllers']['factories']) ? [] : $config['controllers']['factories'];
     $controllers = array_merge($controllerInvokables, $controllerFactories);
     $injections = array();
     foreach ($controllers as $controller) {
         $ref = new ReflectionClass($controller);
         $fileScanner = new FileScanner($ref->getFileName());
         $classScanner = $fileScanner->getClass($controller);
         $methods = $classScanner->getMethods();
         $className = $classScanner->getName();
         $controllerName = $resolvedControllers[$className];
         foreach ($methods as $method) {
             $methodName = $method->getName();
             $actionName = preg_replace('/Action$/', '', $methodName);
             foreach ($method->getParameters() as $parameter) {
                 $parameterScanner = $method->getParameter($parameter);
                 $parameterName = $parameterScanner->getName();
                 $class = $parameterScanner->getClass();
                 if ($class) {
                     $objectManager = $this->detectObjectManager($class, $config['mvc']['doctrine_object_injector']);
                     if (null !== $objectManager) {
                         $injections[$controllerName][$actionName][$parameterName] = [$class, $objectManager, !$parameterScanner->isOptional()];
                     }
                 } else {
                     $injections[$controllerName][$actionName][$parameterName] = null;
                 }
             }
         }
     }
     $config['mvc']['doctrine_object_injector']['injections'] = $injections;
     $event->getConfigListener()->setMergedConfig($config);
 }
Пример #16
0
 /**
  * Determine what classes are present in the cache
  *
  * @return void
  */
 protected function reflectClassCache()
 {
     $scanner = new FileScanner(ZF_CLASS_CACHE);
     $this->knownClasses = array_unique($scanner->getClassNames());
 }
 /**
  * @return array
  */
 public function getFunctions()
 {
     return $this->fileScanner->getFunctions();
 }
Пример #18
0
 public function testClassScannerCanScanInterface()
 {
     $file  = new FileScanner(__DIR__ . '/../TestAsset/FooInterface.php');
     $class = $file->getClass('ZendTest\Code\TestAsset\FooInterface');
     $this->assertEquals('ZendTest\Code\TestAsset\FooInterface', $class->getName());
 }
 /**
  * Retrieve any traits used by the class.
  *
  * @return ClassScanner[]
  */
 public function getTraits()
 {
     if (!empty($this->traits)) {
         return $this->traits;
     }
     // get list of trait names
     $traitNames = $this->getTraitNames();
     foreach ($traitNames as $traitName) {
         $r = new ReflectionClass($traitName);
         if (!$r->isTrait()) {
             throw new Exception\RuntimeException(sprintf('Non-trait class detected as a trait: %s', $traitName));
         }
         $fileName = $r->getFileName();
         $file = new FileScanner($fileName);
         $this->traits[] = $file->getClass($traitName);
     }
     return $this->traits;
 }
Пример #20
0
 public function testClassScannerCanScanAnnotations()
 {
     $file = new FileScanner(__DIR__ . '/../Annotation/TestAsset/EntityWithAnnotations.php');
     $class = $file->getClass('ZendTest\\Code\\Annotation\\TestAsset\\EntityWithAnnotations');
     $annotations = $class->getAnnotations($this->manager);
     $this->assertTrue($annotations->hasAnnotation('ZendTest\\Code\\Annotation\\TestAsset\\Foo'));
     $this->assertTrue($annotations->hasAnnotation('ZendTest\\Code\\Annotation\\TestAsset\\Bar'));
     $this->assertEquals('first', $annotations[0]->content);
     $this->assertEquals('second', $annotations[1]->content);
     $this->assertEquals('third', $annotations[2]->content);
 }
Пример #21
0
 /**
  * Determine what classes are present in the cache
  *
  * @param $classCacheFilename
  * @return void
  */
 protected function reflectClassCache($classCacheFilename)
 {
     $scanner = new FileScanner($classCacheFilename);
     $this->knownClasses = array_unique($scanner->getClassNames());
 }
Пример #22
0
 public function testFileScannerCanReturnClasses()
 {
     $tokenScanner = new FileScanner(__DIR__ . '/../TestAsset/MultipleNamespaces.php');
     $this->assertEquals('ZendTest\\Code\\TestAsset\\Baz', $tokenScanner->getClass('ZendTest\\Code\\TestAsset\\Baz')->getName());
     $this->assertEquals('Foo', $tokenScanner->getClass('Foo')->getName());
 }
Пример #23
0
 /**
  * Builds and populates Action object based on the provided class name
  *
  * @param  string $className
  * @return Action
  */
 public function buildAction($className)
 {
     $classReflection = new ClassReflection($className);
     $scanner = new FileScanner($classReflection->getFileName(), $this->annotationManager);
     $classScanner = $scanner->getClass($classReflection->getName());
     $action = new Action($classScanner->getName());
     foreach ($classScanner->getMethods() as $classMethod) {
         if ($classMethod->isPublic() && $classMethod->getName() != '__construct') {
             $action->addMethod($this->buildMethod($classMethod));
         }
     }
     return $action;
 }