/** * Get CodeGenerator instance for specified class * * @param string $className * @param bool $alias * * @return bool|Zend_CodeGenerator_Php_Class */ public static function get($className, $alias = true) { if (!XDT_CLI_Helper::classExists($className, true, $alias)) { return false; } $classNameOriginal = $className; // Load class file if ($alias) { $className = XDT_CLI_Helper::loadClassAliased($className); } $class = Zend_CodeGenerator_Php_Class::fromReflection(new Zend_Reflection_Class($className)); if (!$class) { return false; } // Set tab indentation $class->setIndentation(' '); if ($alias) { $class->setName($classNameOriginal); } return $class; }
/** * Update an action * * This method is used whenever an action is updated from the * administration panel. * * @param string $file The file to update. * @param string $name The name of the file to update. * @param array $properties An array of properties related to an action. * * @return Either a generated/updated action or false. */ private function updateAction($file, $name, $properties) { $className = 'Action_' . $name; if (!class_exists($className)) { include $file; } // In case the file is empty or messed up, lets generate it again if (!class_exists($className)) { return $this->generateAction($name, $properties); } try { $class = Zend_CodeGenerator_Php_Class::fromReflection(new Zend_Reflection_Class($className)); $default = $class->getProperty('requiredParams'); if (is_object($default) && $default->getDefaultValue()->getValue() != $properties) { $default->setSourceDirty(true); $class->setProperty($default->setDefaultValue($properties)); } $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class); } catch (Exception $e) { return false; } return $file->generate(); }
/** * @group ZF-7909 */ public function testClassFromReflectionDiscardParentImplementedInterfaces() { if (!class_exists('Zend_CodeGenerator_Php_ClassWithInterface')) { require_once dirname(__FILE__) . "/_files/ClassAndInterfaces.php"; } require_once "Zend/Reflection/Class.php"; $reflClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_NewClassWithInterface'); $codeGen = Zend_CodeGenerator_Php_Class::fromReflection($reflClass); $codeGen->setSourceDirty(true); $code = $codeGen->generate(); $expectedClassDef = 'class Zend_CodeGenerator_Php_NewClassWithInterface extends Zend_CodeGenerator_Php_ClassWithInterface implements Zend_Code_Generator_Php_ThreeInterface'; $this->assertContains($expectedClassDef, $code); }
/** * fromReflection() * * @param Zend_Reflection_File $reflectionFile * @return Zend_CodeGenerator_Php_File */ public static function fromReflection(Zend_Reflection_File $reflectionFile) { $file = new self(); $file->setSourceContent($reflectionFile->getContents()); $file->setSourceDirty(false); $body = $reflectionFile->getContents(); // @todo this whole area needs to be reworked with respect to how body lines are processed foreach ($reflectionFile->getClasses() as $class) { $file->setClass(Zend_CodeGenerator_Php_Class::fromReflection($class)); $classStartLine = $class->getStartLine(true); $classEndLine = $class->getEndLine(); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $classStartLine) { $bodyReturn[] = str_replace('?', $class->getName(), self::$_markerClass); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */'; $lineNum = $classEndLine; } else { $bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion } } $body = implode("\n", $bodyReturn); unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine); } if ($reflectionFile->getDocComment() != '') { $docblock = $reflectionFile->getDocblock(); $file->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($docblock)); $bodyLines = explode("\n", $body); $bodyReturn = array(); for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) { if ($lineNum == $docblock->getStartLine()) { $bodyReturn[] = str_replace('?', $class->getName(), self::$_markerDocblock); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */'; $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; }
public function testAction() { $name = 'Testing1'; $dir = ROOT_PATH . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . 'Action'; $file = $dir . DIRECTORY_SEPARATOR . $name . '.php'; require_once $file; $class = Zend_CodeGenerator_Php_Class::fromReflection( new Zend_Reflection_Class('Action_' . $name) ); $error = array(); $errors = array(); foreach ($class->getMethods() as $key => $method) { $body = $method->getBody(); $methodName = $method->getName(); $toks = token_get_all('<?' . 'php ' . $body . '?>'); $it = new ArrayIterator($toks); for ($it->rewind(); $it->valid(); $it->next()) { $current = $it->current(); $key = $it->key(); $item = (double)$current[0]; if ($item == T_STRING && stristr('frapi_error', $current[1]) !== false) { if ($error = $this->getErrors($it, $methodName)) { $errors[] = $error; } } } } //print_r($errors); die(); }
/** * * @param string $pAction * @return string */ public function create_action($pAction = NULL, $pParams = NULL) { $action = $pAction ? $pAction : $this->get_action(); if ($pParams && array_key_exists('view_body', $pParams)) { $view_body = $pParams['view_body']; unset($pParams['view_body']); } else { $view_body = ''; if ($pParams && is_array($pParams) && count($pParams)) { foreach ($pParams as $param) { if (is_array($param)) { list($name, $alias, $default) = $param; $default = trim($default); } elseif (!trim($param)) { continue; } else { $name = $alias = trim($param); $default = NULL; } $name = trim($name); if (!$name) { continue; } ob_start(); ?> $this-><?php echo $name; ?> ; <?php $view_body .= ob_get_clean(); } } } $file = $this->controller_reflection(); $c = $file->getClass($this->controller_class_name()); $aname = "{$action}Action"; $class = Zend_CodeGenerator_Php_Class::fromReflection($c); if (!$class->hasMethod($aname)) { $body = ''; $reflect = ''; if ($pParams && is_array($pParams) && count($pParams)) { ob_start(); foreach ($pParams as $param) { if (!trim($param)) { continue; } elseif (is_array($param)) { list($name, $alias, $default) = $param; $default = trim($default); } else { $name = $alias = trim($param); $default = NULL; } $name = trim($name); if (!$name) { continue; } printf('$%s = $this->_getParam("%s", %s); ', $name, $alias, is_null($default) ? ' NULL ' : "'{$default}'"); ob_start(); printf('$this->view->%s = $%s;', $name, $name); ?> <?php $reflect .= ob_get_clean(); ?> <?php } $body = ob_get_clean() . "\n" . $reflect; } $old = $this->backup_controller(); $method = new Zend_CodeGenerator_Php_Method(); $method->setName($aname)->setBody($body); $class->setMethod($method); $file = new Zend_CodeGenerator_Php_File(); $file->setClass($class); $new_file = preg_replace('~[\\r\\n][\\s]*[\\r\\n]~', "\r", $file->generate()); file_put_contents($this->controller_path(), $new_file); $view_path = $this->view_path($action); if (!file_exists($view_path)) { $dir = dirname($view_path); if (!is_dir($dir)) { mkdir($dir, 0775, TRUE); } file_put_contents($view_path, "<?\n\$this->placeholder('page_title')->set('');\n{$view_body}\n"); } $exec = "diff {$this->_backup_path} {$this->controller_path()} "; $diff = shell_exec($exec); return array('old' => $old, 'new' => $new_file, 'diff' => $diff, 'backup_path' => $this->_backup_path, 'controller_path' => $this->controller_path()); } }