/** * 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; }
/** * 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; }
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); } }