/** * 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 = Consistency\Autoloader::dnew($this->getNodeName(), [$id, $connection, $this]); $this->_nodes[$id] = $this->_node; return $connection; }
/** * 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 = Consistency\Autoloader::dnew($visitor); echo $visitor->visit($ast); } return; }
public function case_dnew_unknown_class() { $this->given($this->function->spl_autoload_call = null)->exception(function () { SUT::dnew('Foo'); })->isInstanceOf('ReflectionException'); }
/** * 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 = Consistency\Autoloader::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'] = Consistency\Autoloader::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); }
/** * 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 = Consistency\Autoloader::dnew($server->getNodeName(), [$id, $client, $server]); $this->_nodes[$id] = $node; $this->_stack[] = $client; } else { $this->_iterator[] = $socket; } } return $this; }
/** * 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 = Consistency\Autoloader::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 = Consistency\Autoloader::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 = Consistency\Autoloader::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'] = Consistency\Autoloader::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; }