/** * FileModel constructor. * * @param string $path The file path relative to the application root. * * @throws Exception If the path does not represent an existing file. */ public function __construct($path) { $this->_absolute = Tempest::get()->filesystem->absolute($path); $this->_relative = $path; if (!Tempest::get()->filesystem->exists($path)) { throw new Exception('File "' . $this->_absolute . '" does not exist and cannot be represented as a FileModel.'); } }
public function __get($prop) { if ($prop === 'uri') { return $this->memoize('uri', function () { if ($this->_definition[0] === '/' && !empty(Tempest::get()->public)) { return Tempest::get()->public; } return Tempest::get()->public . '/' . trim($this->_definition[0], '/'); }); } if ($prop === 'method') { return $this->memoize('method', function () { if ($this->format === self::FORMAT_INVALID || $this->format === self::FORMAT_URI_CONTROLLER) { return 'GET'; } else { return strtoupper($this->_definition[1]); } }); } if ($prop === 'middleware') { return $this->memoize('middleware', function () { if ($this->format === self::FORMAT_URI_METHOD_MIDDLEWARE_CONTROLLER) { return array_slice($this->_definition, 2, -1); } else { return array(); } }); } if ($prop === 'controller') { return $this->memoize('controller', function () { return end($this->_definition); }); } if ($prop === 'format') { return $this->memoize('format', function () { $dl = count($this->_definition); if ($dl < 2) { return self::FORMAT_INVALID; } if ($dl === 2) { return self::FORMAT_URI_CONTROLLER; } if ($dl === 3) { return self::FORMAT_URI_METHOD_CONTROLLER; } if ($dl > 3) { return self::FORMAT_URI_METHOD_MIDDLEWARE_CONTROLLER; } return self::FORMAT_INVALID; }); } return null; }
public function __construct() { $config = Tempest::get()->config->get('db'); if (!empty($config)) { $config = array_merge(array('driver' => 'mysql', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci'), $config); $connection = $this->parseConnectionString(ObjectUtil::getDeepValue($config, 'connection')); $connection = array_merge($config, $connection); $this->_capsule = new Manager(); $this->_capsule->addConnection($connection); $this->_capsule->setAsGlobal(); $this->_capsule->bootEloquent(); } else { throw new Exception('No database connection details were provided by the application.'); } }
/** * Save the uploaded file into the application directory. * * @param string $path The save destination relative to the application root. * * @return bool Whether or not the save was successful. */ public function save($path) { return move_uploaded_file($this->temp, Tempest::get()->filesystem->absolute($path)); }
/** * Adds a new location relative to the application root where templates can be searched for. * * @param string|array $path A path or array of paths to search for templates. * @param string $namespace An optional namespace to use for templates. * * @throws Twig_Error_Loader */ public function addTemplatePath($path, $namespace = Twig_Loader_Filesystem::MAIN_NAMESPACE) { if (is_array($path)) { foreach ($path as $p) { // Recursive path additions. $this->addTemplatePath($p, $namespace); } } else { $path = trim($path, '/'); $this->_loader->prependPath(Tempest::get()->filesystem->absolute($path), $namespace); } }
public function __get($prop) { if ($prop === 'method') { if (array_key_exists('x-http-method-override', $this->headers)) { return strtoupper($this->headers['x-http-method-override']); } return strtoupper($_SERVER['REQUEST_METHOD']); } if ($prop === 'contentType') { return strtolower($_SERVER['CONTENT_TYPE']); } if ($prop === 'ip') { return $_SERVER['REMOTE_ADDR']; } if ($prop === 'uri') { return $this->memoize('uri', function () { $base = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); if (empty($base) || $base === '/') { return '/'; } return rtrim($base, '/'); }); } if ($prop === 'headers') { return $this->memoize('headers', function () { $headers = array(); if (function_exists('getallheaders')) { foreach (getallheaders() as $header => $value) { $headers[strtolower($header)] = $value; } } return $headers; }); } if ($prop === 'user') { return $this->memoize('user', function () { $email = $this->header('x-tempest-user-email'); $password = $this->header('x-tempest-user-password'); if ($email && $password) { return Tempest::get()->users->findByCredentials($email, $password); } return null; }); } if ($prop === 'body') { return $this->memoize('body', function () { return file_get_contents('php://input'); }); } return null; }
/** * Creates an absolute link relative to {@link App::public public} path. * * @param string $value The link relative to the public site URL. * * @return string */ public function link($value) { return Tempest::get()->public . '/' . ltrim($value, '/'); }
public function createTable() { Tempest::get()->db->schema()->create('users', function (Blueprint $table) { $table->increments('id'); $table->string('email')->unique(); $table->string('password'); }); }
public function dispatch() { if (!$this->_dispatched) { $dispatcher = \FastRoute\simpleDispatcher(function (RouteCollector $collector) { foreach ($this->_routes as $route) { $collector->addRoute($route->method, $route->uri, $route); } }); $info = $dispatcher->dispatch($this->_request->method, $this->_request->uri); if ($info[0] === Dispatcher::FOUND) { // Successful route match. $this->_request->attachNamed($info[2]); $this->_matched = $info[1]; $respond = true; if (count($this->_matched->middleware) > 0) { foreach ($this->_matched->middleware as $middleware) { if (!$this->instantiateAndCall($this->baseMiddlewareNamespace . ltrim($middleware, '\\'), $this->_request, $this->_response)) { $respond = false; break; } } } if ($respond) { $this->_response->body = $this->instantiateAndCall($this->baseControllerNamespace . ltrim($this->_matched->controller, '\\'), $this->_request, $this->_response); } } if ($info[0] === Dispatcher::NOT_FOUND) { $useTemplate = false; if (!empty($this->_request->uri)) { // Attempt to load HTML file with the same name. // Hitting the root looks for index.html. $template = ($this->_request->uri === '/' ? 'index' : $this->_request->uri) . '.html'; if ($this->_request->method === 'GET' && Tempest::get()->twig->loader->exists($template)) { $useTemplate = true; foreach (explode('/', $template) as $part) { // Don't use templates if it or any ancestor directory begins with an underscore. if (strpos($part, '_') === 0) { $useTemplate = false; break; } } if ($useTemplate) { $this->_response->body = Tempest::get()->twig->render($template); } } } if (!$useTemplate) { $this->_response->status = Status::NOT_FOUND; if (Tempest::get()->twig->loader->exists('404.html')) { $this->_response->body = Tempest::get()->twig->render('404.html'); } else { $this->_response->body = Tempest::get()->twig->render('@tempest/_errors/404.html'); } } } if ($info[0] === Dispatcher::METHOD_NOT_ALLOWED) { $this->_response->status = Status::METHOD_NOT_ALLOWED; $this->_response->body = Tempest::get()->twig->render('@tempest/_errors/405.html'); } $this->_response->send(); } $this->_dispatched = true; }
/** * Creates an absolute filesystem link based on the application root. * * @param string $relative The relative path within the application directory. * * @return string */ public function absolute($relative) { return Tempest::get()->root . '/' . ltrim($relative, '/'); }