Exemplo n.º 1
0
 public function __get($key)
 {
     if ($this->framework->hasKit($key) === false) {
         $helperClassName = '\\Sohoa\\Framework\\Kit\\' . ucfirst($key);
         if (!class_exists($helperClassName, true)) {
             $helperClassName = '\\Application\\Controller\\Kit\\' . ucfirst($key);
         }
         return $this->framework->kit($key, dnew($helperClassName));
     }
     return $this->framework->kit($key);
 }
Exemplo n.º 2
0
 /**
  * Resolve the dispatch call.
  *
  * @param   array                $rule      Rule.
  * @param   \Hoa\Router          $router    Router.
  * @param   \Hoa\View\Viewable   $view      View.
  * @return  mixed
  * @throws  \Hoa\Dispatcher\Exception
  */
 protected function resolve(array $rule, Router $router, View\Viewable $view = null)
 {
     $called = null;
     $variables =& $rule[Router::RULE_VARIABLES];
     $call = isset($variables['_call']) ? $variables['_call'] : $rule[Router::RULE_CALL];
     $able = isset($variables['_able']) ? $variables['_able'] : $rule[Router::RULE_ABLE];
     $rtv = [$router, $this, $view];
     $arguments = [];
     $reflection = null;
     $async = $router->isAsynchronous();
     $class = $call;
     $method = $able;
     if (false === $async) {
         $_class = 'synchronous.call';
         $_method = 'synchronous.able';
     } else {
         $_class = 'asynchronous.call';
         $_method = 'asynchronous.able';
     }
     $this->_parameters->setKeyword('call', $class);
     $this->_parameters->setKeyword('able', $method);
     $class = $this->_parameters->getFormattedParameter($_class);
     $method = $this->_parameters->getFormattedParameter($_method);
     try {
         $class = dnew($class, $rtv);
     } catch (\Exception $e) {
         throw new Exception('Class %s is not found ' . '(method: %s, asynchronous: %s).', 0, [$class, strtoupper($variables['_method']), true === $async ? 'true' : 'false'], $e);
     }
     $kitname = $this->getKitName();
     if (!empty($kitname) && !isset($variables['_this']) || !(isset($variables['_this']) && $variables['_this'] instanceof $kitname)) {
         $variables['_this'] = dnew($kitname, $rtv);
         $variables['_this']->construct();
     }
     if (!method_exists($class, $method)) {
         throw new Exception('Method %s does not exist on the class %s ' . '(method: %s, asynchronous: %s).', 1, [$method, get_class($class), strtoupper($variables['_method']), true === $async ? 'true' : 'false']);
     }
     $called = $class;
     $reflection = new \ReflectionMethod($class, $method);
     foreach ($reflection->getParameters() as $parameter) {
         $name = strtolower($parameter->getName());
         if (true === array_key_exists($name, $variables)) {
             $arguments[$name] = $variables[$name];
             continue;
         }
         if (false === $parameter->isOptional()) {
             throw new Exception('The method %s on the class %s needs a value for ' . 'the parameter $%s and this value does not exist.', 2, [$method, get_class($class), $name]);
         }
     }
     return $reflection->invokeArgs($called, $arguments);
 }
Exemplo n.º 3
0
 /**
  * The entry method.
  *
  * @return  int
  */
 public function main()
 {
     $visitor = null;
     $tokenSequence = false;
     $trace = false;
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 'v':
                 switch (strtolower($v)) {
                     case 'dump':
                         $visitor = 'Hoa\\Compiler\\Visitor\\Dump';
                         break;
                     default:
                         return $this->usage();
                 }
                 break;
             case 'c':
                 $visitor = str_replace('.', '\\', $v);
                 break;
             case 's':
                 $tokenSequence = true;
                 break;
             case 't':
                 $trace = true;
                 break;
             case '__ambiguous':
                 $this->resolveOptionAmbiguity($v);
                 break;
             case 'h':
             case '?':
             default:
                 return $this->usage();
         }
     }
     $this->parser->listInputs($grammar, $language);
     if (empty($grammar) || empty($language) && '0' !== $language) {
         return $this->usage();
     }
     $compiler = Compiler\Llk::load(new File\Read($grammar));
     $data = new File\Read($language);
     try {
         $ast = $compiler->parse($data->readAll());
     } catch (Compiler\Exception $e) {
         if (true === $tokenSequence) {
             $this->printTokenSequence($compiler);
             echo "\n\n";
         }
         throw $e;
         return 1;
     }
     if (true === $tokenSequence) {
         $this->printTokenSequence($compiler);
         echo "\n\n";
     }
     if (true === $trace) {
         $this->printTrace($compiler);
         echo "\n\n";
     }
     if (null !== $visitor) {
         $visitor = dnew($visitor);
         echo $visitor->visit($ast);
     }
     return;
 }
Exemplo n.º 4
0
 /**
  * Make an instance of a specific graph.
  *
  * @param   string  $type    Type of graph needed.
  * @return  \Hoa\Graph
  * @throws  \Hoa\Graph\Exception
  */
 public static function getInstance($type = self::TYPE_ADJACENCYLIST)
 {
     if ($type != self::TYPE_ADJACENCYLIST) {
         throw new Exception('Type %s is not supported. Only self:TYPE_ADJACENCYLIST is ' . 'supported.', 0, $type);
     }
     $arguments = func_get_args();
     array_shift($arguments);
     return dnew('Hoa\\Graph\\' . $type, $arguments);
 }
Exemplo n.º 5
0
 /**
  * Create a DAL instance, representing a connection to a database.
  * The constructor is private to make a multiton.
  *
  * @param   string  $dalName          The database abstract layer name.
  * @param   string  $dsn              The DSN of database.
  * @param   string  $username         The username to connect to database.
  * @param   string  $password         The password to connect to database.
  * @param   array   $driverOptions    The driver options.
  * @return  void
  * @throws  \Hoa\Database\Exception
  */
 private function __construct($dalName, $dsn, $username, $password, array $driverOptions = [])
 {
     // Please see https://bugs.php.net/55154.
     if (0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) {
         $dsn = 'sqlite:' . resolve($matches[1]);
     }
     $id = $this->__id = self::$_id;
     $event = 'hoa://Event/Database/' . $id;
     Core\Event::register($event . ':opened', $this);
     Core\Event::register($event . ':closed', $this);
     $this->setDal(dnew('\\Hoa\\Database\\Layer\\' . $dalName, [$dsn, $username, $password, $driverOptions]));
     Core\Event::notify($event . ':opened', $this, new Core\Event\Bucket(['id' => $id, 'dsn' => $dsn, 'username' => $username, 'driverOptions' => $driverOptions]));
     return;
 }
Exemplo n.º 6
0
 /**
  * Open the stream and return the associated resource.
  *
  * @param   string               $streamName    Socket URI.
  * @param   \Hoa\Stream\Context  $context       Context.
  * @return  resource
  * @throws  \Hoa\Socket\Exception
  */
 protected function &_open($streamName, Stream\Context $context = null)
 {
     if (null === $context) {
         $connection = @stream_socket_client($streamName, $errno, $errstr, $this->getTimeout(), $this->getFlag());
     } else {
         $connection = @stream_socket_client($streamName, $errno, $errstr, $this->getTimeout(), $this->getFlag(), $context->getContext());
     }
     if (false === $connection) {
         if ($errno == 0) {
             throw new Exception('Client cannot join %s.', 0, $streamName);
         } else {
             throw new Exception('Client returns an error (number %d): %s while trying ' . 'to join %s.', 1, [$errno, $errstr, $streamName]);
         }
     }
     $this->_stack[] = $connection;
     $id = $this->getNodeId($connection);
     $this->_node = dnew($this->getNodeName(), [$id, $connection, $this]);
     $this->_nodes[$id] = $this->_node;
     return $connection;
 }
Exemplo n.º 7
0
 /**
  * Resolve the dispatch call.
  *
  * @param   array                $rule      Rule.
  * @param   \Hoa\Router          $router    Router.
  * @param   \Hoa\View\Viewable   $view      View.
  * @return  mixed
  * @throws  \Hoa\Dispatcher\Exception
  */
 protected function resolve(array $rule, Router $router, View\Viewable $view = null)
 {
     $called = null;
     $variables =& $rule[Router::RULE_VARIABLES];
     $call = isset($variables['controller']) ? $variables['controller'] : (isset($variables['_call']) ? $variables['_call'] : $rule[Router::RULE_CALL]);
     $able = isset($variables['action']) ? $variables['action'] : (isset($variables['_able']) ? $variables['_able'] : $rule[Router::RULE_ABLE]);
     $rtv = [$router, $this, $view];
     $arguments = [];
     $reflection = null;
     if ($call instanceof \Closure) {
         $kitname = $this->getKitName();
         if (!empty($kitname)) {
             $kit = dnew($this->getKitName(), $rtv);
             if (!$kit instanceof Kit) {
                 throw new Exception('Your kit %s must extend Hoa\\Dispatcher\\Kit.', 0, $kitname);
             }
             $variables['_this'] = $kit;
         }
         $called = $call;
         $reflection = new \ReflectionMethod($call, '__invoke');
         foreach ($reflection->getParameters() as $parameter) {
             $name = strtolower($parameter->getName());
             if (true === array_key_exists($name, $variables)) {
                 $arguments[$name] = $variables[$name];
                 continue;
             }
             if (false === $parameter->isOptional()) {
                 throw new Exception('The closured action for the rule with pattern %s needs ' . 'a value for the parameter $%s and this value does not ' . 'exist.', 1, [$rule[Router::RULE_PATTERN], $name]);
             }
         }
     } elseif (is_string($call) && null === $able) {
         $kitname = $this->getKitName();
         if (!empty($kitname)) {
             $kit = dnew($this->getKitName(), $rtv);
             if (!$kit instanceof Kit) {
                 throw new Exception('Your kit %s must extend Hoa\\Dispatcher\\Kit.', 2, $kitname);
             }
             $variables['_this'] = $kit;
         }
         $reflection = new \ReflectionFunction($call);
         foreach ($reflection->getParameters() as $parameter) {
             $name = strtolower($parameter->getName());
             if (true === array_key_exists($name, $variables)) {
                 $arguments[$name] = $variables[$name];
                 continue;
             }
             if (false === $parameter->isOptional()) {
                 throw new Exception('The functional action for the rule with pattern %s needs ' . 'a value for the parameter $%s and this value does not ' . 'exist.', 3, [$rule[Router::RULE_PATTERN], $name]);
             }
         }
     } else {
         $async = $router->isAsynchronous();
         $controller = $call;
         $action = $able;
         if (!is_object($call)) {
             if (false === $async) {
                 $_controller = 'synchronous.call';
                 $_action = 'synchronous.able';
             } else {
                 $_controller = 'asynchronous.call';
                 $_action = 'asynchronous.able';
             }
             $this->_parameters->setKeyword('call', $controller);
             $this->_parameters->setKeyword('able', $action);
             $controller = $this->_parameters->getFormattedParameter($_controller);
             $action = $this->_parameters->getFormattedParameter($_action);
             try {
                 $controller = dnew($controller, $rtv);
             } catch (\Exception $e) {
                 throw new Exception('Controller %s is not found ' . '(method: %s, asynchronous: %s).', 4, [$controller, strtoupper($router->getMethod()), true === $async ? 'true' : 'false'], $e);
             }
             $kitname = $this->getKitName();
             if (!empty($kitname)) {
                 $variables['_this'] = dnew($kitname, $rtv);
             }
             if (method_exists($controller, 'construct')) {
                 $controller->construct();
             }
         }
         if (!method_exists($controller, $action)) {
             throw new Exception('Action %s does not exist on the controller %s ' . '(method: %s, asynchronous: %s).', 5, [$action, get_class($controller), strtoupper($router->getMethod()), true === $async ? 'true' : 'false']);
         }
         $called = $controller;
         $reflection = new \ReflectionMethod($controller, $action);
         foreach ($reflection->getParameters() as $parameter) {
             $name = strtolower($parameter->getName());
             if (true === array_key_exists($name, $variables)) {
                 $arguments[$name] = $variables[$name];
                 continue;
             }
             if (false === $parameter->isOptional()) {
                 throw new Exception('The action %s on the controller %s needs a value for ' . 'the parameter $%s and this value does not exist.', 6, [$action, get_class($controller), $name]);
             }
         }
     }
     if ($reflection instanceof \ReflectionFunction) {
         $return = $reflection->invokeArgs($arguments);
     } elseif ($reflection instanceof \ReflectionMethod) {
         $return = $reflection->invokeArgs($called, $arguments);
     }
     return $return;
 }
Exemplo n.º 8
0
 public function view($name, $classname)
 {
     if (array_key_exists($name, $this->_helpers)) {
         return $this->_helpers[$name];
     }
     if (is_string($classname)) {
         $classname = dnew($classname);
     }
     if ($classname instanceof Helper) {
         $this->_helpers[$name] = $classname;
         $this->_helpers[$name]->setView($this);
     }
     return $this->_helpers[$name];
 }
Exemplo n.º 9
0
 /**
  * The entry method.
  *
  * @access  public
  * @return  int
  */
 public function main()
 {
     $file = null;
     $debug = false;
     $dry = false;
     $directory = null;
     $output = 'out/';
     $style_formatter = 'Cli';
     while (false !== ($c = $this->getOption($v))) {
         switch ($c) {
             case 'f':
                 $file = $v;
                 break;
             case 'd':
                 $directory = $v;
                 break;
             case 'o':
                 $output = $v;
                 break;
             case 's':
                 $style_formatter = $v;
                 break;
             case 'v':
                 $debug = true;
                 break;
             case 'p':
                 $dry = true;
                 break;
             case 'h':
             case '?':
             default:
                 return $this->usage();
                 break;
         }
     }
     echo \Hoa\Console\Chrome\Text::colorize('Sohapi', 'fg(yellow)'), "\n\n";
     $root = realpath(__DIR__ . '/../../../../');
     $out = realpath($root . '/' . $output);
     // TODO : Detect relative avec absolute path !
     $formatter = '\\Sohapi\\Formatter\\' . ucfirst($style_formatter);
     if ($file !== null) {
         $file = realpath($root . '/' . $file);
     }
     if ($directory !== null) {
         $directory = realpath($root . '/' . $directory);
     }
     if ($out === false) {
         $out = $root;
     }
     if ($debug === true) {
         //\Sohapi\Parser\Ast::enableDebug();
         $a = [['ROOT', var_export($root, true)], ['Debug', var_export($debug, true)], ['Dry', var_export($dry, true)], ['Formatter', var_export($formatter, true)], ['Output', var_export($out, true)], ['File', var_export($file, true)], ['Directory', var_export($directory, true)]];
         echo \Hoa\Console\Chrome\Text::columnize($a);
     }
     $files = array();
     $this->searchLocalConfig($root, $files);
     if ($file !== null and !in_array($file, $files)) {
         $files = array($file);
     }
     if ($directory !== null) {
         if ($this->searchLocalConfig($directory, $files) === false) {
             $finder = new \Hoa\File\Finder();
             $finder->in($directory)->files()->name('#\\.php$#');
             foreach ($finder as $f) {
                 if (!in_array($this->clean($f->getPathName()), $files)) {
                     $files[] = $this->clean($f->getPathName());
                 }
             }
         }
     }
     echo 'Found ' . count($files) . ' Files' . "\n";
     foreach ($files as $i => $file) {
         echo 'Parsing : [' . ($i + 1) . '/' . count($files) . '] ' . $file . "\n";
         if ($dry === false) {
             (new \Sohapi\Parser\Reader(file_get_contents($file)))->build();
         }
     }
     $options = [];
     dnew($formatter, ['options' => ['output' => $out, 'debug' => $debug, 'dry' => $dry, 'options' => $options]])->render();
     return;
 }
Exemplo n.º 10
0
 /**
  * Select connections.
  *
  * @return  \Hoa\Socket\Server
  * @throws  \Hoa\Socket\Exception
  */
 public function select()
 {
     $read = $this->_stack;
     $write = null;
     $except = null;
     @stream_select($read, $write, $except, $this->getTimeout(), 0);
     foreach ($read as $socket) {
         if (true === in_array($socket, $this->_masters, true)) {
             $client = @stream_socket_accept($socket);
             if (false === $client) {
                 throw new Exception('Operation timed out (nothing to accept).', 3);
             }
             $m = array_search($socket, $this->_masters, true);
             $server = $this->_servers[$m];
             $id = $this->getNodeId($client);
             $node = dnew($server->getNodeName(), [$id, $client, $server]);
             $this->_nodes[$id] = $node;
             $this->_stack[] = $client;
         } else {
             $this->_iterator[] = $socket;
         }
     }
     return $this;
 }
Exemplo n.º 11
0
 /**
  * Declare a realistic domain: $disjunction->realdomName(arg1, arg2…).
  * About constants: use for example: $disjunction->const(true).
  *
  * @param   string  $name         Realistic domain name.
  * @param   array   $arguments    Arguments.
  * @return  \Hoa\Realdom\Disjunction
  * @throws  \Hoa\Realdom\Exception
  */
 public function __call($name, array $arguments)
 {
     $name = ucfirst(strtolower($name));
     if ('Const' === $name) {
         $handle = $arguments;
         Realdom::autoBoxing($handle);
         $handle = $handle[0];
         $arguments = [];
     } elseif ('Variable' === $name) {
         $handle = new Crate\Variable($arguments[0]);
     } else {
         if (Core\Consistency::isKeyword($name)) {
             $name = 'Realdom' . $name;
         }
         try {
             $handle = dnew('(Hoathis or Hoa)\\Realdom\\' . $name, $arguments);
         } catch (Exception $e) {
             throw $e;
         } catch (Core\Exception $e) {
             throw new Exception('Realistic domain %s() does not exist (or something ' . 'wrong happened).', 0, strtolower($name), $e);
         }
     }
     $this->offsetSet(null, $handle);
     return $this;
 }