/** * Method to run the application routines. Most likely you will want to instantiate a controller * and execute it, or perform some sort of task directly. * * @return void * * @since 1.0 */ protected function doExecute() { $package = $this->io->getArgument(0); $class = $this->io->getArgument(1); $class = StringNormalise::toClassNamespace($class); if (!class_exists($class)) { $class = 'Windwalker\\' . ucfirst($package) . '\\' . $class; } if (!class_exists($class)) { $this->stop('Class not exists: ' . $class); } $packagePath = WINDWALKER_ROOT . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $package; $classPath = ReflectionHelper::getPath($class); $testPath = $packagePath . DIRECTORY_SEPARATOR . 'Test'; $testClass = $this->io->getArgument(2, ReflectionHelper::getShortName($class) . 'Test'); $testClass = StringNormalise::toClassNamespace($testClass); $testFile = $testPath . DIRECTORY_SEPARATOR . $testClass . '.php'; $realTestClass = 'Windwalker\\' . ucfirst($package) . '\\Test\\' . $testClass; $autoload = WINDWALKER_ROOT . '/vendor/autoload.php'; $command = sprintf('vendor/phpunit/phpunit-skeleton-generator/phpunit-skelgen generate-test --bootstrap="%s" %s %s %s %s', $autoload, $class, $classPath, $realTestClass, $testFile); $command = 'php ' . WINDWALKER_ROOT . '/' . $command; if (!defined('PHP_WINDOWS_VERSION_MAJOR')) { // Replace '\' to '\\' in MAC $command = str_replace('\\', '\\\\', $command); } \Windwalker\Filesystem\Folder::create(dirname($testFile)); $this->exec($command); }
/** * prepareData * * @param \Windwalker\Data\Data $data * * @return void */ protected function prepareData($data) { $layout = $this->getLayout(); $method = StringNormalise::toCamelCase(str_replace('.', '_', $layout)); if (is_callable(array($this, $method))) { $this->{$method}($data); } $this->setTitle(); }
/** * Constructor. * * @param \Windwalker\DI\Container $container * @param \Muse\IO\IOInterface $io * @param Structure $config * * @throws \InvalidArgumentException */ public function __construct(Container $container, IOInterface $io, Structure $config = null) { // Get item & list name $ctrl = $config['ctrl'] ?: $io->getArgument(1); $ctrl = explode('.', $ctrl); $inflector = StringInflector::getInstance(); if (empty($ctrl[0])) { $ctrl[0] = 'item'; } if (empty($ctrl[1])) { $ctrl[1] = $inflector->toPlural($ctrl[0]); } list($itemName, $listName) = $ctrl; // Prepare package name $class = explode('\\', str_replace('/', '\\', $config['name'])); $name = array_pop($class); $class = StringNormalise::toClassNamespace(implode('\\', $class)); $class = $class ? $class . '\\' : null; // Check keywords if (in_array(strtolower($name), static::$keywords)) { throw new \InvalidArgumentException('Do not use reserved keywords: ' . $name); } if (in_array(strtolower($listName), static::$keywords)) { throw new \InvalidArgumentException('Do not use reserved keywords: ' . $listName); } if (in_array(strtolower($itemName), static::$keywords)) { throw new \InvalidArgumentException('Do not use reserved keywords: ' . $itemName); } $config['package.name'] = $name; $config['package.namespace'] = $class; $this->replace = new Structure(); $this->replace['package.namespace'] = $class; $this->replace['package.name.lower'] = lcfirst($name); $this->replace['package.name.upper'] = strtoupper($name); $this->replace['package.name.cap'] = ucfirst($name); $this->replace['controller.list.name.lower'] = strtolower($listName); $this->replace['controller.list.name.upper'] = strtoupper($listName); $this->replace['controller.list.name.cap'] = ucfirst($listName); $this->replace['controller.item.name.lower'] = strtolower($itemName); $this->replace['controller.item.name.upper'] = strtoupper($itemName); $this->replace['controller.item.name.cap'] = ucfirst($itemName); // Set replace to config. $config->mergeTo('replace', $this->replace); // Set copy dir. $config->set('dir.dest', WINDWALKER_SOURCE . '/' . $this->replace['package.namespace'] . $this->replace['package.name.cap']); $config->set('dir.tmpl', PHOENIX_TEMPLATES . '/package/' . $config['template']); $config->set('dir.src', $config->get('dir.tmpl')); // Replace DS $config['dir.dest'] = Path::clean($config['dir.dest']); $config['dir.tmpl'] = Path::clean($config['dir.tmpl']); $config['dir.src'] = Path::clean($config['dir.src']); // Push container $this->container = $container; parent::__construct($io, $config, $this->replace->toArray()); }
/** * Execute this command. * * @return int * * @since 2.0 */ protected function doExecute() { $package = $this->getArgument(0); if (!$package) { throw new WrongArgumentException('Please enter package name.'); } $package = ConsoleHelper::getAllPackagesResolver()->getPackage($package); if (!$package) { throw new \InvalidArgumentException(sprintf('Package: %s not found', $this->getArgument(0))); } $recordClass = $this->getArgument(1); if (!$recordClass) { throw new WrongArgumentException('Please enter record name or class.'); } $recordClass = StringNormalise::toClassNamespace($recordClass); $pkgNamespace = ReflectionHelper::getNamespaceName($package); if (!class_exists($recordClass)) { $recordClass = $pkgNamespace . '\\Record\\' . ucfirst($recordClass) . 'Record'; } $table = null; if (class_exists($recordClass)) { try { /** @var Record $record */ $record = new $recordClass(); $table = $record->getTableName(); } catch (\Exception $e) { // Nothing } } if ($this->getArgument(2)) { $table = $this->getArgument(2); } $columns = Ioc::getDatabase()->getTable($table, true)->getColumnDetails(true); $fields = []; $dataType = Ioc::getDatabase()->getTable($table)->getDataType(); foreach ($columns as $column) { $fields[] = ['name' => $column->Field, 'type' => $dataType::getPhpType(explode('(', $column->Type)[0])]; } // Prepare Trait name $name = end(explode('\\', $recordClass)); $name = str_replace('Record', '', $name); $shortName = ucfirst($name) . 'DataTrait'; $data = ['package_namespace' => $pkgNamespace . '\\Record\\Traits', 'short_name' => $shortName, 'columns' => $fields]; $content = (new Edge(new EdgeStringLoader()))->render($this->getTemplate(), $data); $file = $package->getDir() . '/Record/Traits/' . $shortName . '.php'; if (is_file($file) && !(new BooleanPrompter())->ask('File: <comment>' . $file . '</comment> exists, do you want to override it? [N/y]: ', false)) { throw new \RuntimeException(' Canceled...'); } File::write($file, $content); $this->out()->out('Writing file: <info>' . $file . '</info> success.'); return true; }
/** * getReflection * * @param string|object $class The class or object to get reflection. * * @return \ReflectionClass Reflection instance. * * @throws \InvalidArgumentException */ protected static function getReflection($class) { if (is_object($class)) { $class = get_class($class); } if (!is_string($class)) { throw new \InvalidArgumentException('ReflectionClass need string name or object.'); } $class = StringNormalise::toClassNamespace($class); if (empty(static::$refs[$class])) { static::$refs[$class] = new \ReflectionClass($class); } return static::$refs[$class]; }
/** * instance * * @param string $selector * @param array $data * @param array $properties * * @return void */ public static function instance($selector, array $data = [], array $properties = []) { static::core(); JQueryScript::core(); $var = lcfirst(StringNormalise::toCamelCase(trim($selector, '.#[]'))); $instance = ['el' => $selector, 'data' => $data]; $instance = static::getJSObject($instance, $properties); static::internalJS(<<<JS jQuery(function(\$) { window.vueInstances = window.vueInstances || {}; window.vueInstances.{$var} = new Vue({$instance}); }); JS ); }
/** * doExecute * * @return mixed * * @throws \DomainException * @throws \InvalidArgumentException */ protected function doExecute() { $task = $this->input->get('task', 'Update'); if (!$task) { throw new \InvalidArgumentException('Task of: ' . __CLASS__ . ' should not be empty.'); } $resolver = $this->getPackage()->getMvcResolver(); $class = $resolver->resolveController($this->package, $this->getName() . '\\Batch\\' . $task . 'Controller'); if (!$class) { throw new \DomainException(StringNormalise::toClassNamespace($this->getName() . '\\Batch\\' . $task) . 'Controller not found'); } /** @var AbstractController $controller */ $controller = new $class(); $controller->setName($this->getName()); $controller->config->set('item_name', $this->config['item_name']); $controller->config->set('list_name', $this->config['list_name']); $this->hmvc($controller, $this->input->compact(array('id' => 'array', 'batch' => 'var', 'ordering' => 'var'))); return true; }
/** * Method to test StringNormalise::toKey(). * * @param string $expected The expected value from the method. * @param string $input The input value for the method. * * @return void * * @covers Windwalker\String\StringNormalise::toKey * @dataProvider seedTestToKey * @since 1.0 */ public function testToKey($expected, $input) { $this->assertEquals($expected, StringNormalise::toKey($input)); }
/** * call * * @param callable $callable * * @return mixed|null * * @throws InvalidArgumentException */ public function call($callable) { if (!is_array($callable)) { $callable = explode('::', $callable); } if (count($callable) < 2) { throw new \InvalidArgumentException(implode('::', $callable) . ' is not callable.'); } $class = \Windwalker\String\StringNormalise::toClassNamespace($callable[0]); $classname = 'MyEzset_' . trim(str_replace('\\', '_', $class), '\\'); if (!is_callable(array($classname, $callable[1]))) { $classname = 'MyEzset\\' . trim($class, '\\'); } if (!is_callable(array($classname, $callable[1]))) { $classname = 'Ezset\\' . trim($class, '\\'); } if (!is_callable(array($classname, $callable[1]))) { return null; } $args = func_get_args(); array_shift($args); return call_user_func_array(array($classname, $callable[1]), $args); }
/** * Remove alias. * * @param string $class The class to remove. * * @return ControllerDelegator Return self to support chaining. */ public function removeAlias($class) { $class = StringNormalise::toClassNamespace($class); if (!empty($this->aliases[$class])) { unset($this->aliases[$class]); } return $this; }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $package = $this->io->getArgument(0, new ValidatePrompter('Enter package name: ')); $class = $this->io->getArgument(1, new ValidatePrompter('Enter class name: ')); $class = StringNormalise::toClassNamespace($class); $target = $this->io->getArgument(2, $package . '\\' . $class . 'Test'); $target = StringNormalise::toClassNamespace($target); $package = ucfirst($package); if (!class_exists($class)) { $class = 'Windwalker\\' . $package . '\\' . $class; } if (!class_exists($class)) { $this->out('Class not exists: ' . $class); exit; } $replace = $this->replace; $ref = new \ReflectionClass($class); $replace['origin.class.dir'] = dirname($ref->getFileName()); $replace['origin.class.file'] = $ref->getFileName(); $replace['origin.class.name'] = $ref->getName(); $replace['origin.class.shortname'] = $ref->getShortName(); $replace['origin.class.namespace'] = $ref->getNamespaceName(); $replace['test.dir'] = WINDWALKER_ROOT . DIRECTORY_SEPARATOR . 'test'; $replace['test.class.name'] = 'Windwalker\\Test\\' . $target; $replace['test.class.file'] = Path::clean($replace['test.dir'] . DIRECTORY_SEPARATOR . $target . '.php'); $replace['test.class.dir'] = dirname($replace['test.class.file']); $replace['test.class.shortname'] = $this->getShortname(StringNormalise::toClassNamespace($replace['test.class.name'])); $replace['test.class.namespace'] = $this->getNamespace($replace['test.class.name']); $this->replace = $replace; $config = new Registry(); // Set replace to config. foreach ($this->replace as $key => $val) { $config->set('replace.' . $key, $val); } $methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC); $methodTmpl = file_get_contents(GENERATOR_BUNDLE_PATH . '/Template/test/testMethod.php'); $methodCodes = array(); foreach ($methods as $method) { $config['replace.origin.method'] = $method->getName(); $config['replace.test.method'] = ucfirst($method->getName()); $methodCodes[] = StringHelper::parseVariable($methodTmpl, $config->get('replace')); } $config['replace.test.methods'] = implode("", $methodCodes); $this->replace = $config->get('replace'); $this->config = $config; $this->doAction(new GenClassAction()); $this->out('Generate test class: ' . $replace['test.class.name'] . ' to file: ' . $replace['test.class.file'])->out(); return true; }
/** * Method to get the view name * * The model name by default parsed using the classname, or it can be set * by passing a $config['name'] in the class constructor * * @return string The name of the model * * @throws \Exception */ public function getName() { if (empty($this->name)) { $classname = get_class($this); $viewpos = strpos($classname, 'View'); if ($viewpos === false) { throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_VIEW_GET_NAME'), 500); } $lastPart = substr($classname, $viewpos + 4); $pathParts = explode(' ', StringNormalise::fromCamelCase($lastPart)); if (!empty($pathParts[1])) { $this->name = strtolower($pathParts[0]); } else { $this->name = strtolower($lastPart); } } return $this->name; }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @since 12.1 * @throws \LogicException * @throws \RuntimeException */ public function execute() { $config = array(); $this->out()->out('Start generating...')->out(); // Prepare basic data. $command = $this->command; $name = $command->getArgument(0); list($type, $task) = explode('.', $this->getTask(), 2); $this->name = $config['name'] = $name; $this->type = $config['type'] = $type; $this->template = $config['template'] = $this->command->getOption('t'); $this->template = $config['table'] = $this->command->getOption('table'); $config['tagVariables'] = (array) $this->tagVariables; $config['migrate'] = $this->command->getOption('migrate'); $config['seed'] = $this->command->getOption('seed'); // Get Handler $task = StringNormalise::toClassNamespace(str_replace('.', '\\', $task)); $class = sprintf(__NAMESPACE__ . '\\%s\\%sController', ucfirst($this->type), $task); if (!class_exists($class)) { throw new \RuntimeException(sprintf('Task %s of type "%s" not support.', $this->getTask(), $this->type)); } /** @var AbstractTaskController $controller */ $controller = new $class($this->container, $this->io, new Structure($config)); $controller->execute(); $this->out()->out('Template generated.'); }