/** * Create (or modify) given class * * @param string $className * @param Zend_CodeGenerator_Php_Class $class * * @return Zend_CodeGenerator_Php_Class */ public static function create($className, Zend_CodeGenerator_Php_Class $class = null) { // If no class data is given and the class already exists there's no point in "creating" it if ($class == null and XDT_CLI_Helper::classExists($className)) { return self::get($className); } // Only create class if the file is available or we have class data $filePath = self::getClassPath($className); $fileContents = file_exists($filePath) ? file_get_contents($filePath) : false; if (empty($fileContents) or $class != null) { // Load blank CodeGenerator file $file = new Zend_CodeGenerator_Php_File(); // Create CodeGenerato Class if it wasn't provided as a parm if ($class == null) { $class = new Zend_CodeGenerator_Php_Class(); $class->setName($className); } // Append class to file $file->setClass($class); // Write to file XDT_CLI_Helper::writeToFile($filePath, $file->generate(), true); XDT_CLI_Abstract::getInstance()->printMessage("File: " . $filePath); } else { XDT_CLI_Abstract::getInstance()->bail("File already exists: " . $filePath); } return self::get($className); }
/** * Write config to file * * @param object $config * @param null|string $file * * @return object */ public static function writeConfig($config, $file = null) { if (!is_array($config) and !is_object($config)) { return false; } if (empty($file)) { $file = self::xfBaseDir() . DIRECTORY_SEPARATOR . '.xfcli-config'; } $existingConfig = self::loadConfigJson($file); $config = XDT_CLI_Helper::objectMerge($existingConfig, $config); if (!XDT_CLI_Helper::writeToFile($file, XDT_CLI_Helper::jsonEncode($config))) { return false; } return $config; }
/** * Load class under an alias, so that it can be modified without invalidating the class namespace for this session * * @param string $className * @param string|null $alias * * @return bool|string */ public static function loadClassAliased($className, $alias = null) { if ($alias == null) { $alias = $className . '_alias_' . uniqid(); } $path = XDT_CLI_Autoloader::getClassPath($className); if (!$path) { return false; } $contents = file_get_contents($path); $contents = preg_replace('/(.*class)\\s*?' . $className . '/', '$1 ' . $alias, $contents); $filePath = tempnam(sys_get_temp_dir(), 'xfcli'); XDT_CLI_Helper::writeToFile($filePath, $contents); require_once $filePath; return $alias; }