/** * Default run method * TODO: Needs more work * @return void */ public function run() { $this->printMessage('Generating code event listener...'); $config = XDT_CLI_Application::getConfig(); $namespace = $config['namespace'] . '_'; $className = $namespace . 'Listener'; if (!$namespace) { $namespace = ''; } if (!XDT_CLI_Helper::classExists($className, false)) { //$extendName = 'XenForo_Controller' . XfCli_Helpers::camelcaseString($type, false) . '_Abstract'; $class = new Zend_CodeGenerator_Php_Class(); $class->setName($className); //$class->setExtendedClass($extendName); XDT_CLI_ClassGenerator::create($className, $class); $this->printMessage('ok'); } else { $this->printMessage('skipped (already exists)'); } $this->printMessage($namespace); if (!empty($config['name'])) { $this->printMessage($this->colorText('Active Add-on: ', XDT_CLI_Abstract::BROWN), false); $this->printMessage($config['name']); } else { $this->printMessage($this->colorText('No add-on selected.', XDT_CLI_Abstract::RED)); } }
/** * Attempt to retreive addon config file for given addon * * @param array|string $addon * * @return bool|string */ public function getAddonConfigFile($addon) { // Convert string to the array input we're expecting if (is_string($addon)) { $addon = array('addon_id' => $addon, 'title' => $addon); } // Validate input if (!is_array($addon) or !isset($addon['addon_id'], $addon['title'])) { return false; } // Define the addon folder names we will be checking for $names = array($addon['addon_id'], strtolower($addon['addon_id']), XDT_CLI_Helper::camelcaseString($addon['addon_id'], false), XDT_CLI_Helper::camelcaseString($addon['title'], false)); // If title contains the '-' character, turn it into folder bits $bits = explode('-', $addon['title']); if (count($bits) > 1) { foreach ($bits as &$bit) { $bit = XDT_CLI_Helper::camelcaseString($bit, false); } $names[] = implode('/', $bits); $names[] = strtolower(implode('/', $bits)); } // Set variations (with and without library folder) $variations = array(); foreach ($names as $name) { $variations = array_merge($variations, array($name, 'library/' . $name)); } // Locate the config file $base = XDT_CLI_Application::xfBaseDir(); return XDT_CLI_Helper::locate('.xfcli-config', $variations, $base, array($base)); }
/** * 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; }
/** * 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); }
/** * 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; }