public function testRequestObserver() { $config = new Collection(); $config->set('socket', '\\Juriya\\Controller'); $request = new Request($config); $request->attach(new DummyObserver()); ob_start(); $request->execute(); $this->assertEquals('Dummy observer now receive ' . get_class($request), ob_get_clean()); }
/** * Request intantiation process * * @param Collection Juriya\Collection * @throws object RuntimeException */ public function __construct(Collection $config) { // Initialize the request if ($config->valid()) { // Get the request environment runtime setting $this->path = $config->get('path', array()); $this->socket = $config->get('socket', NULL); $this->arguments = $config->get('arguments', array()); $this->tunnel = $config->get('tunnel', 'HMVC'); } else { throw new \RuntimeException(I18n::translate('class_cannot_start', __CLASS__)); } }
/** * Process arguments from $_SERVER * * @param array Server arguments * @return stream */ public static function processArguments(array $arguments) { array_shift($arguments); if (empty($arguments)) { self::showInfo(); } else { $availableCommands = array('-h', '--help', '-v', '--version', '-a', '--app', '-m', '--module', '-t', '--test'); $commands = new Collection($arguments); while ($commands->valid()) { $command = $commands->current(); if (!in_array($command, $availableCommands)) { self::out(I18n::translate('command_not_implemented', $command)); } else { if ($command == '-h' || $command == '--help') { self::showInfo(); $commands->end(); } elseif ($command == '-v' || $command == '--version') { self::showVersion(); $commands->end(); } elseif ($command == '-a' || $command == '--app') { self::generate(); $commands->end(); } elseif ($command == '-m' || $command == '--module') { $namespace = $commands->next(); if (!$namespace) { self::out(I18n::translate('cannot_register_empty_namespace')); self::out(I18n::translate('command_generate_fail')); } else { self::generate($namespace); } $commands->end(); } elseif ($command == '-t' || $command == '--test') { $target = $commands->next(); self::runTest($target); $commands->end(); } } $commands->next(); } } }
/** * I18n main translation API * * @return string */ public static function translate() { // Get the key $arguments = func_get_args(); $key = array_shift($arguments); // Load locale array and translate the key $localeArray = new Collection(self::getInstance()->lang->get(static::$locale, array())); $format = $localeArray->get($key, $key); // Re-arrange the arguments array_unshift($arguments, $format); return call_user_func_array('sprintf', $arguments); }
public function testRequestObserver() { $config = new Collection(); $config->set('socket', '\\Juriya\\Controller'); $config->set('tunnel', 'HMVC'); $request = new Request($config); $observer = new DummyObserver(); $launcher = Juriya::getInstance(); $launcher->attach($observer); ob_start(); $launcher->execute($request); $this->assertEquals('Dummy observer now receive ' . get_class($launcher), ob_get_clean()); // Detach Dummy Observer from Launcher Juriya::getInstance()->detach(new DummyObserver()); }
/** * Dispatch Request instance path information based given routes rule * * @param Collection Juriya Routes * @param array Path information * @return Collection Dispatch result */ public static function dispatchRequestPathWithRoutes(Collection $routes, array $path) { $found = FALSE; // Guess work, first dispatch from application routes foreach ($routes->get(Juriya::APPLICATION, array()) as $key => $value) { $routeConfig = new Collection($value); $routeArguments = $routeConfig->get('arguments', array()); $routeController = $routeConfig->get('controller', '\\stdClass'); $matcherResult = self::routesMatcher($path, $routeArguments, $routeController); if ($matcherResult->get('found') === TRUE) { return $matcherResult; } } // Try dispatch from registered modules routes if (!$found) { foreach ($routes->get(Juriya::MODULES, array()) as $module => $moduleRoutes) { $hasDefaultRoutes = array_key_exists('default', $moduleRoutes); if ($hasDefaultRoutes && count($path) == 1 && strtolower(current($path)) === strtolower($module)) { // Path information match with module name // without given any arguments. If module has default routes, // then we are done. $defaultModuleRoutes = new Collection($moduleRoutes['default']); $controllerClass = $defaultModuleRoutes->get('controller'); $arguments = array(); $found = TRUE; break; } else { // Traverse the module routes and search match pattern foreach ($moduleRoutes as $key => $value) { $routeConfig = new Collection($value); $routeArguments = $routeConfig->get('arguments', array()); $routeController = $routeConfig->get('controller', '\\stdClass'); $matcherResult = self::routesMatcher($path, $routeArguments, $routeController); if ($matcherResult->get('found') === TRUE) { return $matcherResult; } } } } } return new Collection(compact('found', 'controllerClass', 'arguments')); }
public function testDataImplementor() { $collection = new Collection(); $this->assertEmpty($collection->get()); $this->assertEquals('defaultValue', $collection->get('undefined', 'defaultValue')); $collection->set(1, array('First value')); $this->assertEquals(array('First value'), $collection->get('1')); $collection->set('1.Something', array()); $collection->set('1.Something.NotSoSpecial', 'First sub value'); $collection->set('1.Something.Special', array()); $collection->set('1.Something.Special.And', array()); $collection->set('1.Something.Special.And.Awesome', 'Yeah'); $this->assertEquals('Yeah', $collection->get('1.Something.Special.And.Awesome')); $this->assertNull($collection->get('1.Something.Special.And.Awesome.But.Undefined')); }