/** * Set a registry value. * * @param string $path Registry Path (e.g. joomla.content.showauthor) * @param mixed $value Value of entry * @param string $separator The key separator * * @return mixed The value of the that has been set. * * @since 1.0 */ public function set($path, $value, $separator = '.') { $bak = $this->registry->getSeparator(); $this->registry->setSeparator($separator); $this->registry->set($path, $value); $this->registry->setSeparator($bak); return $this->registry->get($path); }
/** * Constructor. * * @param \Windwalker\DI\Container $container * @param \Muse\IO\IOInterface $io * @param Registry $config */ public function __construct(Container $container, IOInterface $io, Registry $config = null) { // Get item & list name $ctrl = $config['ctrl'] ?: $io->getArgument(1); $ctrl = explode('.', $ctrl); $inflector = \JStringInflector::getInstance(); if (empty($ctrl[0])) { $ctrl[0] = 'item'; } if (empty($ctrl[1])) { $ctrl[1] = $inflector->toPlural($ctrl[0]); } list($itemName, $listName) = $ctrl; $replace['extension.element.lower'] = strtolower($config['element']); $replace['extension.element.upper'] = strtoupper($config['element']); $replace['extension.element.cap'] = ucfirst($config['element']); $replace['extension.name.lower'] = strtolower($config['name']); $replace['extension.name.upper'] = strtoupper($config['name']); $replace['extension.name.cap'] = ucfirst($config['name']); $replace['controller.list.name.lower'] = strtolower($listName); $replace['controller.list.name.upper'] = strtoupper($listName); $replace['controller.list.name.cap'] = ucfirst($listName); $replace['controller.item.name.lower'] = strtolower($itemName); $replace['controller.item.name.upper'] = strtoupper($itemName); $replace['controller.item.name.cap'] = ucfirst($itemName); // Set replace to config. foreach ($replace as $key => $val) { $config->set('replace.' . $key, $val); } // Set copy dir. $config->set('dir.dest', PathHelper::get(strtolower($config['element']), $config['client'])); $config->set('dir.tmpl', GENERATOR_BUNDLE_PATH . '/Template/' . $config['extension'] . '/' . $config['template']); $config->set('dir.src', $config->get('dir.tmpl') . '/' . $config['client']); // 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, $replace); }
/** * Unset a property. * * @param mixed $offset Offset key to unset. * * @throws \InvalidArgumentException * @return void */ public function offsetUnset($offset) { $this->state->set($offset, null); }
/** * Set config. * * @param string $key Config key. * @param string $value Value you want to set. * * @return AbstractTaskController Return self to support chaining. */ public function set($key, $value) { $this->config->set($key, $value); return $this; }
/** * Method to test set(). * * @return void * * @covers Windwalker\Registry\Registry::set * @TODO Implement testSet(). */ public function testSet() { $this->instance->set('tree.bird', 'sleeping'); $this->assertEquals($this->instance->get('tree.bird'), 'sleeping'); }
/** * 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; }
/** * Modifies a property of the object, creating it if it does not already exist. * * @param string $key The name of the property. * @param mixed $value The value of the property to set (optional). * * @return mixed Previous value of the property * * @since 2.0 */ public function set($key, $value = null) { $previous = $this->config->get($key); $this->config->set($key, $value); return $previous; }
/** * prepareSystemPath * * @param Registry $config * * @return void */ public static function prepareSystemPath(Registry $config) { $config['path.root'] = WINDWALKER_ROOT; $config['path.bin'] = WINDWALKER_BIN; $config['path.cache'] = WINDWALKER_CACHE; $config['path.etc'] = WINDWALKER_ETC; $config['path.logs'] = WINDWALKER_LOGS; $config['path.resources'] = WINDWALKER_RESOURCES; $config['path.source'] = WINDWALKER_SOURCE; $config['path.temp'] = WINDWALKER_TEMP; $config['path.vendor'] = WINDWALKER_VENDOR; $config['path.public'] = WINDWALKER_PUBLIC; $config['path.migrations'] = WINDWALKER_MIGRATIONS; $config['path.seeders'] = WINDWALKER_SEEDERS; $config['path.languages'] = WINDWALKER_LANGUAGES; switch ($config['mode']) { case 'test': $root = WINDWALKER_ROOT; break; default: $env = new Environment(); $root = $env->server->getWorkingDirectory(); $root = $root ?: WINDWALKER_ROOT; break; } $data = $config->get('outer_project') ? $root . '/.vaseman' : $root; $config['path.templates'] = $data . '/layouts'; $config->set('project.path.root', $root); $config->set('project.path.data', $data); $config->set('project.path.entries', WINDWALKER_ROOT . '/entries'); $config->set('project.path.layouts', WINDWALKER_ROOT . '/layouts'); }
/** * Method to run this controller. * * @return mixed */ protected function doExecute() { // Init Variables $data = $this->input->get($this->input->get('formctrl'), array(), 'array'); $result = new Registry(); $result->set('Result', false); $model_name = $this->input->get('model_name'); $component = $this->input->get('component'); $extension = $this->input->get('extension'); // Include Needed Classes JLoader::registerPrefix(ucfirst($component), JPATH_BASE . "/components/com_{$component}"); JForm::addFormPath(JPATH_BASE . "/components/com_{$component}/models/forms"); JForm::addFieldPath(JPATH_BASE . "/components/com_{$component}/models/fields"); JTable::addIncludePath(JPATH_BASE . "/components/com_{$component}/tables"); LanguageHelper::loadLanguage($extension, null); // Get Model /** @var $model CrudModel */ $model = $this->getModel(ucfirst($model_name), ucfirst($component)); if (!$model instanceof CrudModel) { $result->set('errorMsg', 'Model need extends to \\Windwalker\\Model\\CrudModel.'); jexit($result); } // For WindWalker Component only if (method_exists($model, 'getFieldsName')) { $fields_name = $model->getFieldsName(); $data = ArrayHelper::pivotToTwoDimension($data, $fields_name); } // Check for validation errors. try { // Get Form if (method_exists($model, 'getForm')) { $form = $model->getForm($data, false); if (!$form) { $result->set('errorMsg', 'No form'); jexit($result); } // Test whether the data is valid. $validData = $model->validate($form, $data); } else { $validData = $data; } // Do Save $model->save($validData); } catch (ValidateFailException $e) { // Get the validation messages. $errors = $e->getErrors(); $errors = array_map(function ($error) { return (string) $error->getMessage(); }, $errors); $result->set('errorMsg', $errors); exit($result); } catch (\Throwable $e) { // Return Error Message. $result->set('errorMsg', \JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $e->getMessage())); $result->set('backtrace', BacktraceHelper::normalizeBacktraces($e->getTrace())); jexit($result); } catch (\Exception $e) { // Return Error Message. $result->set('errorMsg', \JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $e->getMessage())); $result->set('backtrace', BacktraceHelper::normalizeBacktraces($e->getTrace())); jexit($result); } // Set ID $data['id'] = $model->getState()->get($model_name . '.id'); // Set Result $result->set('Result', true); $result->set('data', $data); jexit($result); }
<?php require_once 'vendor/autoload.php'; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse as Response; use Windwalker\Registry\Registry; use League\Route\RouteCollection; //use Symfony\Component\HttpFoundation\RedirectResponse as Redirect; $router = new RouteCollection(); $request = Request::createFromGlobals(); $registry = new Registry(); $registry->set('config.posibile-hoteluri', ['perla', 'parc']); $registry->set('config.string_hotel_identifier_limit', 3); /** * Helpers */ $registry->set('helper.generator-hotel-item-body', function ($identifier) use($registry) { return ['identifier' => $identifier, 'name' => 'Hotel ' . ucwords($identifier) . ' ' . str_repeat('*', strlen($identifier)), 'mobile' => '0421.000.000', 'country' => 'Romania', 'geo' => ['lat' => '41.222', 'lon' => '26.222'], '@self' => '/hotel/' . $identifier, '@rooms' => '/rooms?hotel=' . $identifier]; }); $registry->set('helper.generator-room-number', function ($args) use($registry) { $charLimit = $registry->get('config.string_hotel_identifier_limit'); $prefix = strtoupper(substr(md5($args['hotelIdentifier']), 0, $charLimit)); $padding = str_pad($args['number'], 5, 0); return $prefix . $padding . $args['number']; }); $registry->set('helper.generator-room-item-body', function ($roomIdentifier, $hotelIdentifier) use($registry) { $charLimit = $registry->get('config.string_hotel_identifier_limit'); return ['identifier' => $roomIdentifier, 'name' => 'Room ' . substr($roomIdentifier, $charLimit + 1), '@self' => '/room/' . $roomIdentifier, '@hotel' => '/hotel/' . $hotelIdentifier]; }); $registry->set('helper.find-hotel-by-room-identifier', function ($roomIdentifier) use($registry) { $demoList = $registry->get('config.posibile-hoteluri');
/** * testUnshift * * @return void * * @covers Windwalker\Registry\Registry::unshift */ public function testUnshift() { $registry = new Registry(); $registry->set('foo', array('var1', 'var2', 'var3')); $registry->unshift('foo', 'var4'); $this->assertEquals('var4', $registry->get('foo.0')); $registry->unshift('foo', 'var5', 'var6'); $this->assertEquals('var5', $registry->get('foo.0')); $this->assertEquals('var6', $registry->get('foo.1')); $registry->setRaw('foo2', (object) array('var1', 'var2', 'var3')); $b = $registry->get('foo2'); $this->assertTrue(is_object($b)); $registry->unshift('foo2', 'var4'); $b = $registry->get('foo2'); $this->assertTrue(is_array($b)); }