Exemplo n.º 1
0
 /**
  * @param  FileReflection $fileReflection
  * @return FileGenerator
  */
 public static function fromReflection(FileReflection $fileReflection)
 {
     $file = new static();
     $file->setSourceContent($fileReflection->getContents());
     $file->setSourceDirty(false);
     $body = $fileReflection->getContents();
     $uses = $fileReflection->getUses();
     foreach ($fileReflection->getClasses() as $class) {
         $phpClass = ClassGenerator::fromReflection($class);
         $phpClass->setContainingFileGenerator($file);
         foreach ($uses as $fileUse) {
             $phpClass->addUse($fileUse['use'], $fileUse['as']);
         }
         $file->setClass($phpClass);
     }
     $namespace = $fileReflection->getNamespace();
     if ($namespace != '') {
         $file->setNamespace($namespace);
     }
     if ($uses) {
         $file->setUses($uses);
     }
     if ($fileReflection->getDocComment() != '') {
         $docBlock = $fileReflection->getDocBlock();
         $file->setDocBlock(DocBlockGenerator::fromReflection($docBlock));
     }
     return $file;
 }
Exemplo n.º 2
0
 /**
  * @param  FileReflection $fileReflection
  * @return FileGenerator
  */
 public static function fromReflection(FileReflection $fileReflection)
 {
     $file = new static();
     $file->setSourceContent($fileReflection->getContents());
     $file->setSourceDirty(false);
     $body = $fileReflection->getContents();
     $uses = $fileReflection->getUses();
     foreach ($fileReflection->getClasses() as $class) {
         $phpClass = ClassGenerator::fromReflection($class);
         $phpClass->setContainingFileGenerator($file);
         foreach ($uses as $fileUse) {
             $phpClass->addUse($fileUse['use'], $fileUse['as']);
         }
         $file->setClass($phpClass);
         $classStartLine = $class->getStartLine(true);
         $classEndLine = $class->getEndLine();
         $bodyLines = explode("\n", $body);
         $bodyReturn = array();
         for ($lineNum = 1, $count = count($bodyLines); $lineNum <= $count; $lineNum++) {
             if ($lineNum == $classStartLine) {
                 $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_Code_Generator_Php_File-ClassMarker: {?} */');
                 $lineNum = $classEndLine;
             } else {
                 $bodyReturn[] = $bodyLines[$lineNum - 1];
                 // adjust for index -> line conversion
             }
         }
         $body = implode("\n", $bodyReturn);
         unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
     }
     $namespace = $fileReflection->getNamespace();
     if ($namespace != '') {
         $file->setNamespace($namespace);
     }
     if ($uses) {
         $file->setUses($uses);
     }
     if ($fileReflection->getDocComment() != '') {
         $docBlock = $fileReflection->getDocBlock();
         $file->setDocBlock(DocBlockGenerator::fromReflection($docBlock));
         $bodyLines = explode("\n", $body);
         $bodyReturn = array();
         for ($lineNum = 1, $count = count($bodyLines); $lineNum <= $count; $lineNum++) {
             if ($lineNum == $docBlock->getStartLine()) {
                 $bodyReturn[] = str_replace('?', $class->getName(), '/* Zend_Code_Generator_FileGenerator-DocBlockMarker */');
                 $lineNum = $docBlock->getEndLine();
             } else {
                 $bodyReturn[] = $bodyLines[$lineNum - 1];
                 // adjust for index -> line conversion
             }
         }
         $body = implode("\n", $bodyReturn);
         unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
     }
     $file->setBody($body);
     return $file;
 }
Exemplo n.º 3
0
 public function testCheckDependencies()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $componentRegistrar = new ComponentRegistrar();
         $fileReflection = new FileReflection($file);
         $tokens = new Tokens($fileReflection->getContents(), new ParserFactory());
         $tokens->parseContent();
         $dependencies = array_merge((new Injectable())->getDependencies($fileReflection), $tokens->getDependencies());
         $pattern = '#^(\\\\|)' . implode('|', $this->getForbiddenNamespaces()) . '\\\\#';
         foreach ($dependencies as $dependency) {
             $dependencyPaths = explode('/', $dependency);
             $dependencyPaths = array_slice($dependencyPaths, 2);
             $dependency = implode('\\', $dependencyPaths);
             $libraryPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
             foreach ($libraryPaths as $libraryPath) {
                 $filePath = str_replace('\\', '/', $libraryPath . '/' . $dependency . '.php');
                 if (preg_match($pattern, $dependency) && !file_exists($filePath)) {
                     $this->errors[$fileReflection->getFileName()][] = $dependency;
                 }
             }
         }
         if (!empty($this->errors)) {
             $this->fail($this->getFailMessage());
         }
     }, $this->libraryDataProvider());
 }
Exemplo n.º 4
0
 /**
  * Test check dependencies in library from application
  *
  * @test
  * @dataProvider libraryDataProvider
  */
 public function testCheckDependencies($file)
 {
     $fileReflection = new FileReflection($file);
     $tokens = new Tokens($fileReflection->getContents(), new ParserFactory());
     $tokens->parseContent();
     $dependencies = array_merge((new Injectable())->getDependencies($fileReflection), $tokens->getDependencies());
     foreach ($dependencies as $dependency) {
         if (preg_match('#^(\\\\|)' . implode('|', $this->getForbiddenNamespaces()) . '\\\\#', $dependency) && !file_exists(BP . '/lib/internal/' . str_replace('\\', '/', $dependency) . '.php')) {
             $this->errors[$fileReflection->getFileName()][] = $dependency;
         }
     }
     if ($this->hasErrors()) {
         $this->fail($this->getFailMessage());
     }
 }