Exemplo n.º 1
0
 /**
  * Gets the table class name.
  *
  * @param string $alias The alias name you want to get.
  * @param array $options Table options array.
  * @return string
  */
 protected function _getClassName($alias, array $options = [])
 {
     if (empty($options['className'])) {
         $options['className'] = Inflector::camelize($alias);
     }
     return Core::className($options['className'], 'Model/Table', 'Table');
 }
Exemplo n.º 2
0
 /**
  * Add a provider
  *
  * @param string $name The provider's name.
  * @return $this
  */
 public function useProvider(string $name)
 {
     $className = Core::className($name, "", "Provider");
     if ($className !== false and !isset($this->_providers[$className])) {
         $object = new $className();
         $this->_providers[$className] = $object;
     }
     return $this;
 }
Exemplo n.º 3
0
 public static function getEngine()
 {
     $engineName = Configuration::getInstance()->get("Cache/engine", "File");
     $className = Core::className($engineName, "Cache/Engine", "CacheEngine");
     if (!$className) {
         throw new \RuntimeException(sprintf('Cache Engine class "%s" was not found.', $engineName));
     }
     if (static::$_engine instanceof $className) {
         return static::$_engine;
     }
     return static::_build($className);
 }
Exemplo n.º 4
0
 public function __construct(Console $console = null)
 {
     parent::__construct($console);
     foreach ($this->tasks as $task) {
         if (!($className = Core::className($task, 'Shell/Tasks', 'Task'))) {
             if (!($className = Core::className($task, 'Console/Tasks', 'Task'))) {
                 throw new MissingTaskException(['class' => $task . 'Task']);
             }
         }
         $this->_loadedTasks[$task] = new $className($console);
     }
 }
Exemplo n.º 5
0
 /**
  * Renders the response for the exception.
  *
  * @return Response The response to be sent.
  */
 public function render($exception)
 {
     $exception = $exception instanceof PHP7ErrorException ? $exception->getError() : $exception;
     list(, $baseClass) = namespaceSplit(get_class($exception));
     if (substr($baseClass, -9) === 'Exception') {
         $baseClass = substr($baseClass, 0, -9);
     }
     $action = (new Text($baseClass))->camelBackize() ?: 'error500';
     $class = Core::className('Error', 'Controller', 'Controller');
     $controller = new $class();
     if (!in_array($action, get_class_methods($controller))) {
         $action = "processError";
     }
     return $controller->callAction($action, ["exception" => $exception, "request" => Router::getInstance()->request()]);
 }
Exemplo n.º 6
0
 /**
  * Returns the class used to hydrate rows for this table or sets
  * a new one
  *
  * @param string|null $name the name of the class to use
  * @throws \Cake\ORM\Exception\MissingEntityException when the entity class cannot be found
  * @return string
  */
 public function entityClass($name = null)
 {
     if ($name === null && !$this->_entityClass) {
         $default = '\\Cake\\ORM\\Entity';
         $self = get_called_class();
         $parts = explode('\\', $self);
         if ($self === __CLASS__ || count($parts) < 3) {
             return $this->_entityClass = $default;
         }
         $alias = Inflector::singularize(substr(array_pop($parts), 0, -5));
         $name = implode('\\', array_slice($parts, 0, -1)) . '\\Entity\\' . $alias;
         if (!class_exists($name)) {
             return $this->_entityClass = $default;
         }
     }
     if ($name !== null) {
         $class = Core::className($name, 'Model/Entity');
         $this->_entityClass = $class;
     }
     if (!$this->_entityClass) {
         throw new MissingEntityException([$name]);
     }
     return $this->_entityClass;
 }
Exemplo n.º 7
0
 /**
  * Get the crypto implementation based on the loaded extensions.
  *
  * You can use this method to forcibly decide between mcrypt/openssl/custom implementations.
  *
  * @param AbstractEncryptEngine|string|null $engine The crypto engine to use.
  * @return AbstractEncryptEngine Crypto instance.
  * @throws \InvalidArgumentException When no compatible crypto extension is available.
  */
 public static function engine($engine = null)
 {
     if ($engine === null && static::$_engine === null) {
         if (extension_loaded('openssl')) {
             $engine = new OpenSslEngine();
         } elseif (extension_loaded('mcrypt')) {
             $engine = new MCryptEngine();
         }
     }
     if (is_string($engine)) {
         $engine = Core::className($engine, "Util/CrypTyson", "Engine");
         $eng = new $engine();
         if ($eng instanceof AbstractEncryptEngine) {
             static::$_engine = $eng;
         }
     }
     if ($engine instanceof AbstractEncryptEngine) {
         static::$_engine = $engine;
     }
     if (isset(static::$_engine)) {
         return static::$_engine;
     }
     throw new InvalidArgumentException('No compatible crypto engine available. ' . 'Load either the openssl or mcrypt extensions');
 }
Exemplo n.º 8
0
 protected function _engine() : AbstractRenderingEngine
 {
     if ($this->_engine) {
         return $this->_engine;
     }
     $className = Configuration::getInstance()->get("View/renderingEngine", "Php");
     $className = Core::className($className, "View/RenderingEngine", "RenderingEngine");
     if (!$className) {
         throw new \RuntimeException('Rendering Engine class was not found.');
     }
     $engine = new $className();
     if (!$engine instanceof AbstractRenderingEngine) {
         throw new \RuntimeException('Rendering engine must extend AbstractRenderingEngine class.');
     }
     return $this->_engine = $engine;
 }
 /**
  * Renders the given cell.
  *
  * Example:
  *
  * ```
  * // App\View\Cell\TagCloudCell::smallList()
  * $cell = $this->cell('TagCloud::smallList', ['limit' => 10]);
  * ```
  *
  * The `run` action will be used by default when no action is provided:
  *
  * ```
  * // App\Cell\TagCloudCell::run()
  * $cell = $this->cell('TagCloud');
  * ```
  *
  * Cells are not rendered until they are echoed.
  *
  * @param string $cell You must indicate cell name, and optionally a cell action. e.g.: `TagCloud::smallList`
  * will invoke `View\Cell\TagCloudCell::smallList()`, `display` action will be invoked by default when none is provided.
  * @param array $data Additional arguments for cell method. e.g.:
  *    `cell('TagCloud::smallList', ['a1' => 'v1', 'a2' => 'v2'])` maps to `View\Cell\TagCloud::smallList(v1, v2)`
  * @return AbstractCell The cell instance
  * @throws MissingCellException If Cell class was not found.
  * @throws BadMethodCallException If Cell class does not specified cell action.
  */
 public function cell(string $cell, $data = []) : AbstractCell
 {
     $className = Core::className($cell, 'Cell', 'Cell');
     if (!$className) {
         throw new MissingCellException(['className' => $cell . 'Cell']);
     }
     $cell = new $className();
     $cell->helpers += $this->getLoadedHelpers();
     if (!method_exists($cell, "run")) {
         throw new BadMethodCallException(sprintf('Class %s does not have a run() method.', $className));
     }
     if ($data instanceof ArrayObject) {
         $data = $data->toStandardArray();
     }
     call_user_func_array([$cell, "run"], $data);
     return $cell;
 }
Exemplo n.º 10
0
 /**
  * Initializes the components and models a controller will be using.
  * Triggers the controller action and invokes the rendering if Controller::$autoRender
  * is true. If a response object is returned by controller action that is returned
  * else controller's $response property is returned.
  *
  * @param Request $request Controller to invoke
  * @return Response The resulting response object
  * @throws MissingControllerException
  */
 public function exec(Request $request) : Response
 {
     $response = null;
     if (is_string($this->_callable)) {
         $params = explode('#', $this->_callable);
         $controller = Core::className($params[0], "Controller", "Controller");
         if (!$controller) {
             throw new MissingControllerException([$params[0] . "Controller"]);
         }
         $controller = new $controller();
         if (!isset($params[1]) or empty($params[1])) {
             $params[1] = "index";
         }
         $request->params['controller'] = $controller;
         $request->params['action'] = $params[1];
         $request->params['arguments'] = $this->_params;
         $controller->setUp();
         $response = $controller->dispatch($request);
         $controller->tearDown();
     } elseif (is_callable($this->_callable)) {
         $params = [];
         foreach ($this->_params as $parameter) {
             $params[] = $parameter["value"];
         }
         $response = call_user_func_array($this->_callable, $params);
     }
     return $response;
 }
Exemplo n.º 11
0
 /**
  * Displays an exception response body.
  *
  * @param \Exception $exception The exception to display
  * @return void
  * @throws \Exception When the chosen exception renderer is invalid.
  */
 protected function _displayException($exception)
 {
     $config = Configuration::getInstance();
     $renderer = Core::className($config->get("Error/exceptionRenderer", 'CoreTyson\\Error\\ExceptionRenderer'), 'Error');
     try {
         if (!$renderer) {
             throw new Exception("{$renderer} is an invalid class.");
         }
         $error = new $renderer($exception);
         $response = $error->render();
         $this->_clearOutput();
         $this->_sendResponse($response);
     } catch (Exception $e) {
         // Disable trace for internal errors.
         $config->set("Error/printTrace", "false");
         $message = sprintf("[%s] %s\n%s", get_class($e), $e->getMessage(), $e->getTraceAsString());
         trigger_error($message, E_USER_ERROR);
     }
 }
Exemplo n.º 12
0
 /**
  * Add a middleware to the controller's registry.
  *
  * This method will also set the middleware to a property.
  * For example:
  *
  * ```
  * $this->loadMiddleware('Auth');
  * ```
  *
  * Will result in a `Auth` property being set.
  *
  * @param string $name The name of the middleware to load.
  * @return \CoreTyson\Controller\Middleware\AbstractMiddleware
  */
 public function loadMiddleware(string $name) : AbstractMiddleware
 {
     if (isset($this->_middlewareRegistry[$name])) {
         return $this->_middlewareRegistry[$name];
     }
     Core::className($name, "Controller/Middleware", "Middleware");
     $className = Core::className($name, "Controller/Middleware", "Middleware");
     $middleware = new $className();
     $this->_middlewareRegistry[$name] = $middleware;
     $this->{$name} = $middleware;
     return $this->{$name};
 }
Exemplo n.º 13
0
 /**
  * Sets the driver instance. If a string is passed it will be treated
  * as a class name and will be instantiated.
  *
  * If no params are passed it will return the current driver instance.
  *
  * @param string|null $driver The driver instance to use.
  * @param array $config Either config for a new driver or null.
  * @throws \CoreTyson\Database\Exception\MissingDriverException When a driver class is missing.
  * @throws \CoreTyson\Database\Exception\MissingExtensionException When a driver's PHP extension is missing.
  * @return \CoreTyson\Database\Driver\AbstractDriver
  */
 public function driver($driver = null, array $config = [])
 {
     if ($driver === null) {
         return $this->_driver;
     }
     if (is_string($driver)) {
         $driver = Core::className($driver, 'Database\\Driver', 'Driver');
         if (!class_exists($driver)) {
             throw new MissingDriverException([$driver]);
         }
         $driver = new $driver($config);
     } else {
         return null;
     }
     if (!$driver->enabled()) {
         throw new MissingExtensionException(get_class($driver));
     }
     return $this->_driver = $driver;
 }