/**
  * @param cfhCompile_CodeWriter $codeWriter
  * @param cfhCompile_Class_Interface $class
  * @param unknown_type $sourceCode
  * @param cfhCompile_ClassRegistry $classRegistry
  * @return String
  */
 public function preWrite(cfhCompile_CodeWriter $codeWriter, cfhCompile_Class_Interface $class, $sourceCode, cfhCompile_ClassRegistry $classRegistry)
 {
     if (is_null($sourceCode)) {
         return NULL;
     }
     $tokens = token_get_all('<?php' . PHP_EOL . $sourceCode);
     $sourceCode = '';
     foreach ($tokens as $token) {
         if (is_string($token)) {
             $sourceCode .= $token;
         } else {
             list($token, $text) = $token;
             if ($token == T_FILE) {
                 $text = "'" . $class->getFileName() . "'";
             }
             $sourceCode .= $text;
         }
     }
     return substr($sourceCode, strlen('<?php' . PHP_EOL));
 }
 /**
  * Register a class in the registry
  *
  * @param cfhCompile_Class_Interface $class
  * @return cfhCompile_Class_Interface
  * @throws cfhCompile_ClassRegistry_Exception
  */
 public function register(cfhCompile_Class_Interface $class)
 {
     if (isset($this->registry[$class->getName()])) {
         if ($this->registry[$class->getName()] === $class) {
             return $class;
         }
         throw new cfhCompile_ClassRegistry_Exception('Class ' . $class->getName() . ' has already been registered.');
     }
     $this->registry[$class->getName()] = $class;
     return $class;
 }
Пример #3
0
 /**
  * Gets the source code for the specified class.
  *
  * @param cfhCompile_Class_Interface $class
  * @return String
  */
 public function getSourceCode(cfhCompile_Class_Interface $class)
 {
     if ($class->getFileName() === NULL) {
         return NULL;
     }
     $code = @file($class->getFileName(), FILE_IGNORE_NEW_LINES);
     if ($code === FALSE) {
         throw new cfhCompile_CodeReader_Exception('Unable to read contents of ' . $class->getFileName());
     }
     $offset = 0;
     $length = PHP_INT_MAX;
     if ($class->getStartLine() > 0) {
         $offset = $class->getStartLine() - 1;
     }
     if ($class->getEndLine() > 0) {
         $length = $class->getEndLine() - $offset;
     }
     if ($length < 1) {
         $length = PHP_INT_MAX;
     }
     $code = trim(implode(PHP_EOL, array_slice($code, $offset, $length)));
     if (!preg_match('/^(<\\?(php){0,1}\\s)/', $code)) {
         // make sure that we start in PHP mode for the tokenizer.
         $code = '<?php' . PHP_EOL . $code;
     }
     $tokens = token_get_all($code);
     $code = '';
     $depth = -1;
     foreach ($tokens as $token) {
         switch ($depth) {
             case -1:
                 if (!is_string($token)) {
                     list($token, $text) = $token;
                     switch ($token) {
                         case T_ABSTRACT:
                         case T_FINAL:
                             $code .= $text;
                             break;
                         case T_CLASS:
                         case T_INTERFACE:
                             $code .= $text;
                             $depth = 0;
                             break;
                         case T_WHITESPACE:
                             if ($code != '') {
                                 $code .= $text;
                             }
                             break;
                     }
                 }
                 break;
             case 0:
                 list($token, $text) = $token;
                 switch ($token) {
                     case T_WHITESPACE:
                         $code .= $text;
                         break;
                     case T_STRING:
                         if ($text == $class->getName()) {
                             $code .= $text;
                             $depth = 1;
                         } else {
                             $code = '';
                             $depth = -1;
                         }
                         break;
                 }
                 break;
             default:
                 if (is_string($token)) {
                     $code .= $token;
                     switch ($token) {
                         case '{':
                             $depth++;
                             break;
                         case '}':
                             $depth--;
                             if ($depth == 1) {
                                 break 3;
                             }
                             break;
                     }
                 } else {
                     list($token, $text) = $token;
                     $code .= $text;
                     switch ($token) {
                         case T_CURLY_OPEN:
                         case T_DOLLAR_OPEN_CURLY_BRACES:
                             $depth++;
                             break;
                     }
                 }
                 break;
         }
     }
     return $code;
 }
Пример #4
0
 protected function write(cfhCompile_Class_Interface $class)
 {
     if (isset($this->classesWritten[$class->getName()])) {
         return;
     }
     $this->classesWritten[$class->getName()] = TRUE;
     $source = $this->codeReader->getSourceCode($class);
     if ($source !== NULL) {
         $this->notify(cfhCompile_Compiler_Event::EVENT_WRITE, $class);
         $this->codeWriter->write($class, $source, $this->classRegistry);
     } else {
         $this->notify(cfhCompile_Compiler_Event::EVENT_SKIP, $class);
     }
 }
Пример #5
0
 /**
  * @param cfhCompile_Class_Interface $class
  * @return Integer
  */
 protected function getFileId(cfhCompile_Class_Interface $class)
 {
     $this->stmtFileSelect->bindValue(':file_name', $class->getFileName(), PDO::PARAM_STR);
     $this->stmtFileSelect->execute();
     $id = $this->stmtFileSelect->fetchColumn();
     $this->stmtFileSelect->closeCursor();
     if (!$id) {
         $this->stmtFileInsert->bindValue(':file_name', $class->getFileName(), PDO::PARAM_STR);
         $this->stmtFileInsert->execute();
         $id = $this->dbh->lastInsertId();
     }
     return $id;
 }