public function getDependentServices(Tokenize $tokenize)
 {
     /**
             0000163(00027) - T_VARIABLE: '$container'
             0000164(00027) - T_OBJECT_OPERATOR: '->'
             0000165(00027) - T_STRING: 'get'
             0000166(-----) - CHARACTER: '('
             0000167(00027) - T_CONSTANT_ENCAPSED_STRING: ''annotation_reader''
             0000168(-----) - CHARACTER: ')'
              **/
     $i = 0;
     $containerGets = array();
     while ($i = $tokenize->findExactOffsetAfter($i, T_VARIABLE, '$container')) {
         if (preg_match('/(\\$[\\w]+)\\s+=\\s+\\$container->get\\(/', $tokenize->getLineAt($i), $match)) {
             $i = $tokenize->findExactOffsetAfter($i, T_STRING, 'get');
             $i = $tokenize->findAfter($i, T_CONSTANT_ENCAPSED_STRING);
             $containerGets[$tokenize->extractStringAt($i)] = $match[1];
         }
     }
     $methodsToMock = array();
     foreach ($containerGets as $serviceId => $instance) {
         $methods = array();
         $i = 0;
         while ($i = $tokenize->findExactOffsetAfter($i, T_VARIABLE, $instance)) {
             //$tokenize->dumpAround($i, 5);
             if (T_WHITESPACE == $tokenize->getTokenTypeAt($i + 1)) {
                 continue;
             }
             $i = $tokenize->findAfter($i, T_STRING);
             $methods[] = $tokenize->getStringAt($i);
         }
         $methodsToMock[$serviceId] = $methods;
     }
     return $methodsToMock;
 }
Beispiel #2
0
 /**
  *
  * @return \Cogen\Tokenize
  */
 private function getTokenize()
 {
     if (!empty($this->tok)) {
         return $this->tok;
     }
     $this->tok = \Cogen\Tokenize::createFromString($this->getBody());
     return $this->tok;
 }
Beispiel #3
0
 public function resolve($arg)
 {
     if (is_dir($arg)) {
         echo "FOLDER\n";
         $classes = array();
         foreach (new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($arg)) as $file) {
             /* @var $file \SplFileInfo */
             if (!$file->isFile()) {
                 continue;
             }
             if (strpos($file->getRealPath(), '.svn')) {
                 continue;
             }
             $tok = new Cogen\Tokenize($file->getRealPath());
             $className = $tok->getSingleNamespacedClass();
             if (!$className) {
                 continue;
             }
             $classes[] = $className;
         }
         var_dump($classes);
         die("Stopped at " . __FILE__ . ':' . __LINE__);
     } elseif (is_file($arg)) {
         $tok = new Cogen\Tokenize($arg);
         $ns = $tok->getNamespace();
         $namespaceName = $this->getRootNamespace($this->moduleMainFolder, $ns) . '\\Tests';
         $className = $tok->getSingleNamespacedClass();
         $destinationFile = str_replace($this->moduleMainFolder, $this->testsFolder, realpath($arg));
         $destinationFile = str_replace('.php', 'Test.php', $destinationFile);
         $destFolder = dirname($destinationFile);
         if (!file_exists($destFolder)) {
             mkdir($destFolder, 0755, true);
             echo "Making folder {$destFolder}\n";
         }
         $contents = "<?php \n" . $this->forClass($className)->setNamespaceName($namespaceName);
         if (is_file($destinationFile)) {
             echo "File {$destinationFile} exists\n";
             return;
         }
         //$destinationFile = "php://stdout";
         $bytes = file_put_contents($destinationFile, $contents);
         echo "Writing {$bytes} bytes to " . str_replace($this->moduleMainFolder . '/', '', $destinationFile) . "\n";
     } else {
         echo $this->forClass($arg);
     }
 }
Beispiel #4
0
 /**
  * @param type $path
  * @return DependencyIndex
  */
 public function buildIdex($path)
 {
     $debug = false;
     $index = new DependencyIndex();
     $tokenizers = Helper::getMethodsStartingWith(new \ReflectionClass(__CLASS__), 'tokenize');
     foreach ($this->getDirectoryFiles($path) as $relpath => $filename) {
         $tokenize = new Tokenize($filename);
         #echo $tokenize->dumpAllTokens(array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT));
         /**
          * Get namespace for a file, as if it was PSR0
          */
         $namespace = $tokenize->getNamespace();
         /**
          * Get all classes and prepend a namespace
          */
         foreach ($tokenize->getAllClasses() as $className) {
             $index->addClass($relpath, $namespace, $className);
         }
         /**
          * Classes and interfaces fit the same array
          */
         foreach ($tokenize->getAllInterfaces() as $interfaceName) {
             $index->addClass($relpath, $namespace, $interfaceName);
         }
         /**
          * This option could be configurable, as we may want to process
          * files like bootstraps or smth. But later.
          */
         if (!$index->hasClasses($relpath)) {
             continue;
         }
         if ($debug) {
             echo "Processing {$relpath}", PHP_EOL;
         }
         if ($debug) {
             echo "Classes found: ", join(', ', $index->getClasses($relpath)), PHP_EOL;
         }
         $uses = $tokenize->getUses();
         foreach ($uses as $alias => $className) {
             $index->addAliasedDependency($relpath, $alias, $className);
         }
         foreach (Helper::reduceMethodGroupResultToArray($tokenizers, $this, $tokenize) as $alias) {
             //echo "ALIAS: $relpath: $alias\n";
             $index->addNamespacedDependency($relpath, $namespace, $alias);
         }
         if ($index->hasDependencies($relpath)) {
             if ($debug) {
                 echo "Dependencies found: ", join(', ', $index->getDependencies($relpath)), PHP_EOL;
             }
         }
     }
     return $index;
 }