示例#1
0
 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());
 }
示例#2
0
 /**
  * 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__));
     }
 }
示例#3
0
 /**
  * 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();
         }
     }
 }
示例#4
0
文件: I18n.php 项目: nurcahyo/juriya
 /**
  * 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);
 }
示例#5
0
 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());
 }
示例#6
0
 /**
  * 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'));
 }
示例#7
0
 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'));
 }