Exemplo n.º 1
0
 public function offsetGet($offset)
 {
     if (isset($this->modules[$offset])) {
         return $this->modules[$offset];
     }
     Exception::toss('The module "%s" does not exist.', $offset);
 }
Exemplo n.º 2
0
 public function offsetGet($name)
 {
     if (isset($this->routes[$name])) {
         return $this->routes[$name];
     }
     Exception::toss('Cannot get route "%s" because it does not exist.', $name);
 }
Exemplo n.º 3
0
 public function get($name)
 {
     if (isset($this->cache[$name])) {
         return $this->cache[$name];
     }
     if (isset($this->loading[$name])) {
         throw new CircularReferenceException($name, array_keys($this->loading));
     }
     $this->loading[$name] = true;
     $class = call_user_func($this->filter, $name);
     if (!class_exists($class)) {
         Exception::toss('The class "%s" does not exist.', $class);
     }
     $args = [];
     foreach ($this->args as $instanceof => $instanceofArgs) {
         if ($this->is($class, $instanceof)) {
             $args = array_merge($args, $instanceofArgs());
         }
     }
     $class = new ClassReflector($class);
     $class = $class->newInstanceArgs($args);
     foreach ($this->callbacks as $instanceof => $callback) {
         if ($this->is($class, $instanceof)) {
             $callback($class);
         }
     }
     unset($this->loading[$name]);
     if ($this->transient) {
         return $class;
     }
     return $this->cache[$name] = $class;
 }
Exemplo n.º 4
0
 public function mergeNamedArgs(array $params, $caseSensitive = false)
 {
     $merged = [];
     foreach ($params as $name => $value) {
         if (is_numeric($name)) {
             $merged[(int) $name] = $value;
         } elseif (!$caseSensitive) {
             $params[strtolower($name)] = $value;
         }
     }
     foreach ($this->getParameters() as $param) {
         $pos = $param->getPosition();
         $name = $caseSensitive ? $param->getName() : strtolower($param->getName());
         if (array_key_exists($name, $params)) {
             $merged[$pos] = $params[$name];
         } elseif (array_key_exists($pos, $params)) {
             $merged[$pos] = $params[$pos];
         } elseif ($param->isOptional()) {
             $merged[$pos] = $param->getDefaultValue();
         } else {
             Exception::toss('The required parameter "%s" for "%s()" was not specified.', $param->getName(), $this->__toString());
         }
     }
     return $merged;
 }
Exemplo n.º 5
0
 public function get($name)
 {
     if (isset($this->routes[$name])) {
         return $this->routes[$name];
     }
     Exception::toss('The route "%s" does not exist.', $name);
 }
Exemplo n.º 6
0
 public function mergeNamedArgs(array $params, $caseSensitive = false, $throw = true)
 {
     // resulting merged parameters will be stored here
     $merged = array();
     // apply strict position parameters and case sensitivity
     foreach ($params as $name => $value) {
         if (is_numeric($name)) {
             $merged[(int) $name] = $value;
         } elseif (!$caseSensitive) {
             $params[strtolower($name)] = $value;
         }
     }
     // we check each parameter and set accordingly
     foreach ($this->getParameters() as $param) {
         $pos = $param->getPosition();
         $name = $caseSensitive ? $param->getName() : strtolower($param->getName());
         if (array_key_exists($name, $params)) {
             $merged[$pos] = $params[$name];
         } elseif (array_key_exists($pos, $params)) {
             $merged[$pos] = $params[$pos];
         } elseif ($param->isOptional()) {
             $merged[$pos] = $param->getDefaultValue();
         } elseif ($throw) {
             Exception::toss('The required parameter "%s" for "%s::%s()" was not specified.', $param->getName(), $this->getClass()->getName(), $this->getName());
         } else {
             $meged[$pos] = null;
         }
     }
     return $merged;
 }
Exemplo n.º 7
0
 public function redirect($uri = null, array $params = [])
 {
     if (headers_sent()) {
         Exception::toss('Cannot redirect to "%s" from the view because output has already started.', $uri);
     }
     (new RequestUri($this->format($uri, $params)))->redirect();
 }
Exemplo n.º 8
0
 public function __invoke($data)
 {
     $decoded = json_decode($data);
     if ($error = json_last_error()) {
         Exception::toss('The JSON string "%s" was unable to be parsed because: %s', $data, $this->errorMessages[$error]);
     }
     return $decoded;
 }
Exemplo n.º 9
0
 public function __invoke()
 {
     $decoded = json_decode(file_get_contents($this->file));
     if ($error = json_last_error()) {
         Exception::toss('The JSON file "%s" was unable to be parsed with message: ' . $this->errorMessages[$error], $this->file);
     }
     return $decoded;
 }
Exemplo n.º 10
0
 public function current()
 {
     $callable = current($this->callables);
     if (!is_callable($callable)) {
         Exception::toss('The item at offset "%s" must be callable. Type of "%s" supplied.', $this->key(), gettype($instance));
     }
     return $callable;
 }
Exemplo n.º 11
0
 public function get($name)
 {
     foreach ($this->injectors as $injector) {
         if ($injector->has($name)) {
             return $injector->get($name);
         }
     }
     Exception::toss('The service "%s" does not exist.', $name);
 }
Exemplo n.º 12
0
 public function __construct($file)
 {
     if (!function_exists('yaml_parse_file')) {
         Exception::toss('In order to use the YAML config adapter you must install the PECL YAML extension. See http://php.net/yaml for more information.');
     }
     if (!is_file($this->file = $file)) {
         Exception::toss('The YAML configuration file "%s" does not exist.', $file);
     }
 }
Exemplo n.º 13
0
 private function translateRequest(RequestInterface $request)
 {
     if ($request instanceof CliInterface) {
         return $request->getCommand();
     }
     if ($request instanceof HttpInterface) {
         return $request->getMethod() . ' ' . $request->getUri()->getRequest();
     }
     Exception::toss('Unable to translate request "%s".', get_class($request));
 }
Exemplo n.º 14
0
 public function addPath($path, $check = true)
 {
     $path = $this->root ? $this->root . '/' . $path : $path;
     if ($real = realpath($path)) {
         $this->paths[] = $real;
     } elseif ($check) {
         Exception::toss('The path "%s" does not exist.', $path);
     }
     return $this;
 }
Exemplo n.º 15
0
 public function current()
 {
     $instance = current($this->instances);
     if (!is_object($instance)) {
         Exception::toss('The item at offset "%s" must be an object instance. Type of "%s" supplied.', $this->key(), gettype($instance));
     }
     if (!$instance instanceof $this->instanceof) {
         Exception::toss('The instance at offset "%s" must be an instance of "%s". Instance of "%s" supplied.', $this->key(), $this->instanceof, get_class($instance));
     }
     return $instance;
 }
Exemplo n.º 16
0
 private function resolveController()
 {
     $this->events->trigger(self::EVENT_ROUTE, $this->controllers, $this->request, $this->router);
     if (!$this->router->route($this->request)) {
         Exception::toss('The router could not find a suitable controller for the request "%s".', $this->request);
     }
     $controller = $this->request->getParam($this->config['controller-param']);
     if (!$this->controllers->has($controller)) {
         Exception::toss('The controller "%s" could not be found.', $controller);
     }
     return $this->controllers->get($controller);
 }
Exemplo n.º 17
0
 private function validate(ModuleInterface $module)
 {
     foreach ($module->dependencies() as $name => $version) {
         if (!$this->has($name)) {
             Exception::toss('The module "%s" is required by the module "%s".', $name, $module->name());
         }
         $version = new SemVer($version);
         if (!$version->is($this->get($name)->version())) {
             Exception::toss('The module "%s", currently at version "%s", is required to be at version "%s" by the module "%s".', $name, $this->get($name)->version(), $version, $module->name());
         }
     }
 }
Exemplo n.º 18
0
 public function __construct(callable $callable)
 {
     if ($callable instanceof Closure || is_string($callable) && function_exists($callable)) {
         $this->reflector = new FunctionReflector($callable);
     } elseif (is_array($callable)) {
         $this->reflector = new MethodReflector($callable[0], $callable[1]);
     } elseif (is_object($callable)) {
         $this->reflector = new ClassReflector($callable);
     }
     if (!$this->reflector) {
         Exception::toss('The callable could not be reflected.');
     }
 }
Exemplo n.º 19
0
 public static function detect(callable $callable)
 {
     if ($callable instanceof \Closure || function_exists($callable)) {
         return new \ReflectionFunction($callable);
     }
     if (is_array($callable)) {
         return new MethodReflector($callable[0], $callable[1]);
     }
     if (is_object($callable)) {
         return new ClassReflector($callable);
     }
     Exception::toss('The callable could not be reflected.');
 }
Exemplo n.º 20
0
 private function getFiltersFor(ReflectorInterface $reflector)
 {
     $filters = [];
     foreach ($reflector->getDocBlock()->getTags(static::DOC_TAG_FILTER) as $filter) {
         $parts = explode(' ', $filter->value(), 2);
         $class = trim($parts[0]);
         $value = isset($parts[1]) ? trim($parts[1]) : '';
         if (!class_exists($class)) {
             Exception::toss('The filter "%s" specified for "%s" does not exist.', $class, $reflector->__toString());
         }
         $filters[] = new $class($value);
     }
     return $filters;
 }
Exemplo n.º 21
0
 public function __invoke(ControllerAbstract $controller, ClassReflector $class, MethodReflector $method, array &$context)
 {
     foreach ($method->getParameters() as $param) {
         if (!($type = $param->getClass())) {
             continue;
         }
         $type = $type->getName();
         $name = $param->getName();
         if (isset($context[$name])) {
             $value = $context[$name];
         } elseif ($param->isDefaultValueAvailable()) {
             $value = $param->getDefaultValue();
         } else {
             Exception::toss('Cannot type-hint "%s" in "%s" because the request does not contain the parameter and a default value was not specified.', $name, $method->__toString());
         }
         $context[$name] = new $type($value);
     }
 }
Exemplo n.º 22
0
 public function __invoke(ControllerAbstract $controller, $class, $method)
 {
     $request = $controller->request();
     foreach ($method->getParameters() as $param) {
         if (!($type = $param->getClass())) {
             continue;
         }
         $type = $type->getName();
         $name = $param->getName();
         if ($request->hasParam($name)) {
             $value = $request->getParam($name);
         } elseif ($param->isDefaultValueAvailable()) {
             $value = $param->getDefaultValue();
         } else {
             Exception::toss('Cannot type-hint "%s" in "%s::%s()" because the request does not contain the parameter and a default value was not specified.', $name, $class->getName(), $method->getName());
         }
         $request->setParam($name, new $type($value));
     }
 }
Exemplo n.º 23
0
 public function load($class)
 {
     if (class_exists($class, false)) {
         return true;
     }
     if (isset($this->included[$class])) {
         Exception::toss('The class "%s" was supposed to be found in "%s". A potential cause is when a class name does not match PSR-0 standards.', $class, $this->included[$class]);
     }
     $subject = str_replace($this->separators, DIRECTORY_SEPARATOR, $class) . $this->suffix;
     if (isset($this->map[$class])) {
         include $found = $this->map[$class];
     } elseif ($this->locator && ($found = $this->locator->locate($subject))) {
         include $found;
     } elseif (is_file($found = __DIR__ . '/../../' . $subject)) {
         include $found;
     }
     if ($found) {
         $this->map[$class] = $found;
         $this->included[$class] = $found;
         return true;
     }
     return false;
 }
Exemplo n.º 24
0
 public function extend($parent)
 {
     // the child is the current script
     $child = $this->getScript();
     // child views cannot extend themselves
     if ($parent === $child) {
         Exception::toss('Child view cannot extend itself.');
     }
     // if the child has already extended a parent, don't do anything
     if (in_array($child, $this->extendStack)) {
         return $this;
     }
     // the extend stack makes sure that extend doesn't trigger recursion
     $this->extendStack[] = $child;
     // set the parent
     $this->parentScript = $parent;
     return $this;
 }
Exemplo n.º 25
0
 private function validateFunctions()
 {
     foreach ($this->classConfig->requiredFunctions as $function) {
         if (!function_exists($function)) {
             Exception::toss('The function "%s" is required by the module "%s".', $function, $this->name);
         }
     }
 }
Exemplo n.º 26
0
 private function checkReadonly()
 {
     if ($this->readonly) {
         Exception::toss('Cannot modify the configuration because it is readonly.');
     }
 }
Exemplo n.º 27
0
 private function resolveAdapterFromFile($config)
 {
     $adapter = explode('.', $config);
     $adapter = end($adapter);
     $adapter = 'Europa\\Config\\Adapter\\From\\' . ucfirst($adapter);
     if (!class_exists($adapter)) {
         Exception::toss('The import adapter "%s" autodetected from "%s" cannot be found.', $config, $adapter);
     }
     return new $adapter();
 }
Exemplo n.º 28
0
 private function generateErrorHandler($data)
 {
     return function ($errno, $errstr, $errfile, $errline, $errcontext) use($data) {
         Exception::toss('Unable to parse YAML string "%s" with error: %s', $data, $errstr);
     };
 }
Exemplo n.º 29
0
 public function __construct($file)
 {
     if (!is_file($this->file = $file)) {
         Exception::toss('The INI config file "%s" does not exist.', $file);
     }
 }
Exemplo n.º 30
0
 public static function get($name = self::DEFAULT_INSTANCE)
 {
     if (!isset(self::$instances[$name])) {
         Exception::toss('Could not find application instance "%s".', $name);
     }
     return self::$instances[$name];
 }