Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * DeliciousPHP
- *
- * An open source application development framework for PHP 5.3 or newer
- *
- * NOTICE OF LICENSE
- *
- * Licensed under the Open Software License version 3.0
- *
- * This source file is subject to the Open Software License (OSL 3.0) that is
- * bundled with this package in the files license.txt / license.rst. It is
- * also available through the world wide web at this URL:
- * http://opensource.org/licenses/OSL-3.0
- *
- * @package DeliciousPHP
- * @author Michael Beers <michael@michaelbeers.nl>
- * @copyright (c) 2012 - 2013, Michael Beers | Online Media en Design
- * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
- * @link http://michaelbeers.nl
- * @since Version 1.0
- * @filesource
- */
- namespace DeliciousPHP;
- /**
- * The core of the framework and it's dependency container. It holds references
- * to all framework wide instances, like configs, sessions, debugging etc.
- * Instead of calling a class constructor call a wrapping function of this class
- * to construct the object for you. You can extend this class adding properties
- * that you want to be accessible all around your app.
- *
- * Do not use this class directly. Instead, use its child class [[\App\Delicious]]
- * where you can customize the methods of this class.
- *
- * @package DeliciousPHP
- * @author Michael Beers <michael@michaelbeers.nl>
- * @copyright (c) 2012 - 2013, Michael Beers | Online Media en Design
- * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
- * @link http://michaelbeers.nl
- * @since Version 1.0
- *
- * @property-read \DeliciousPHP\Config $config Description
- * @property-read \DeliciousPHP\Cookie $cookie Description
- * @property-read \DeliciousPHP\Debug $debug Error/Debugging handler.
- * @property-read \DeliciousPHP\Router $router Description
- * @property-read \DeliciousPHP\Session $session Description
- */
- class Delicious {
- /**
- * List with framework modules.
- *
- * @var array
- */
- protected $modules = array();
- /**
- * List with framework instances.
- *
- * @var array
- */
- protected $instances = array();
- /**
- * List with resources that will be used in the framework.
- *
- * @var array
- */
- protected $resources = array(
- 'config' => '\DeliciousPHP\Config',
- 'cookie' => '\DeliciousPHP\Cookie',
- 'debug' => '\DeliciousPHP\Debug',
- 'router' => '\DeliciousPHP\Router',
- 'session' => '\DeliciousPHP\Session'
- );
- /**
- * List with paths to data directories.
- *
- * @var type
- */
- protected $data_dirs = array();
- /**
- * Base path of the application.
- *
- * @var string
- */
- public $basepath = '/';
- /**
- * Stores the namespace of the application.
- *
- * @var string
- */
- public $app_namespace;
- // ----------------------------------------------------------------------
- /**
- * Get a property by name. Returns defined class and module instances.
- *
- * @param string $name Property name.
- * @return mixed Instance of defined class or module.
- * @throws \Exception When a property is not found.
- */
- public function __get($name) {
- if (isset($this->instances[$name])) {
- return $this->instances[$name];
- }
- if (isset($this->modules[$name])) {
- return $this->instances[$name] = new $this->modules[$name]($this);
- }
- if (isset($this->resources[$name])) {
- return $this->instances[$name] = new $this->resources[$name]($this);
- }
- throw new \Exception("Property {$name} not found in " . get_class($this));
- }
- // ----------------------------------------------------------------------
- /**
- * Bootstraps the project.
- *
- * @return \DeliciousPHP\Delicious
- */
- public function bootstrap() {
- // Initialize application namespace.
- if ($this->app_namespace === null) {
- $class_name = get_class($this);
- $this->app_namespace = substr($class_name, 0, strpos($class_name, "\\") + 1);
- }
- // Add the root to data directories.
- $this->data_dirs[] = BASE_PATH . 'data' . DS;
- // Initialize the error handler and debug logger.
- $this->debug->init();
- // Load all modules.
- foreach ($this->modules as $name => $class) {
- $this->$name = new $class($this);
- }
- // Add the public to data directories.
- array_unshift($this->data_dirs, PUBLIC_PATH . 'data' . DS);
- // Add the routes.
- foreach ($this->config->get('routes') as $name => $rule) {
- $methods = $this->array_key(2, null, $rule);
- $route = $this->route($name, $rule[0], $rule[1], $methods);
- $this->router->add($route);
- }
- $this->bootstrap_after();
- return $this;
- }
- /**
- * Perform some initialization after the bootstrap is finished loading.
- *
- * Note: You should overwrite this method.
- */
- protected function bootstrap_after() {
- // Does nothing.
- }
- // ----------------------------------------------------------------------
- /**
- * Creates a Request representing current HTTP request.
- *
- * @return \DeliciousPHP\Request
- */
- public function http_request() {
- $uri = $_SERVER['REQUEST_URI'];
- $uri = preg_replace("#^{$this->basepath}(?:index\.php/)?#i", '/', $uri);
- $url_parts = parse_url($uri);
- $route_data = $this->router->match($url_parts['path'], $_SERVER['REQUEST_METHOD']);
- return $this->request($route_data['route'], $_SERVER['REQUEST_METHOD'], $_POST, $_GET, $route_data['params'], $_SERVER, $_COOKIE);
- }
- /**
- * Processes HTTP request, executes it and sends back the response.
- */
- public function handle_http_request() {
- try {
- $request = $this->http_request();
- $response = $request->execute();
- $response->send_headers()->send_body();
- } catch (\Exception $e) {
- $this->handle_exception($e);
- }
- }
- /**
- * Exception handler. By default displays the error page.
- * If you want your exceptions to be handled in a specific way
- * you should override this method.
- *
- * @param \Exception $exception Exception to handle
- */
- public function handle_exception(\Exception $exception) {
- $this->debug->render($exception);
- }
- // ----------------------------------------------------------------------
- /**
- * Constructs a new controller by class name.
- *
- * @param string $class
- * @return \DeliciousPHP\Controller
- */
- public function controller($class) {
- if (!class_exists($class)) {
- throw new \DeliciousPHP\Exception\PageNotFound("Class {$class} doesn't exist");
- }
- return new $class($this);
- }
- /**
- * Constructs a new request.
- *
- * @param \DeliciousPHP\Delicious $delicious DeliciousPHP dependency container.
- * @param \DeliciousPHP\Route $route Route for this request.
- * @param type $method HTTP method for the request (e.g. GET, POST)
- * @param type $post Array of POST data
- * @param type $get Array of GET data
- * @param type $param Array of URL parameters
- * @param type $server Array of SERVER data
- * @param type $cookie Array of COOKIE data
- * @return \DeliciousPHP\Request
- */
- public function request(\DeliciousPHP\Route $route, $method = "GET", $post = array(), $get = array(), $param = array(), $server = array(), $cookie = array()) {
- return new \DeliciousPHP\Request($this, $route, $method, $post, $get, $param, $server, $cookie);
- }
- /**
- * Constructs a new response.
- *
- * @return \DeliciousPHP\Response
- */
- public function response() {
- return new \DeliciousPHP\Response($this);
- }
- /**
- * Constructs a new route.
- *
- * @param string $name Name of this route.
- * @param mixed $rule Rule for this route.
- * @param array $defaults Default parameters for this route.
- * @param mixed $methods Methods to restrict this route to.
- * @return \DeliciousPHP\Route
- */
- public function route($name, $rule, $defaults, $methods = null) {
- return new \DeliciousPHP\Route($name, $rule, $defaults, $methods);
- }
- /**
- * Constructs a view.
- *
- * @param string $name Name of the template to use.
- * @return \DeliciousPHP\View
- */
- public function view($name) {
- return new \DeliciousPHP\View($this, $this->view_helper(), $name);
- }
- /**
- * Constructs a view helper.
- *
- * @return \DeliciousPHP\View\Helper
- */
- public function view_helper() {
- return new \DeliciousPHP\View\Helper($this);
- }
- // ----------------------------------------------------------------------
- /**
- * Fetch a key from an array, with default value support.
- *
- * @param type $key Value to check.
- * @param type $default Default value on failure.
- * @param array $array An array with keys to check.
- * @return mixed An array value if it was found or default value if it is not.
- */
- public function array_key($key, $default, array $array) {
- if (isset($array[$key])) {
- return $array[$key];
- }
- return $default;
- }
- /**
- * Finds full path to a specified file in the data directories.
- *
- * @param type $subfolder
- * @param type $name
- * @param type $return_all
- * @return string|boolean
- */
- public function find_file($subfolder, $name, $return_all = false) {
- $fname = $name . EXT;
- $found_files = array();
- foreach ($this->data_dirs as $folder) {
- $file = $folder . $subfolder . DS . $fname;
- if (file_exists($file)) {
- if (!$return_all) {
- return $file;
- }
- $found_files[] = $file;
- }
- }
- if (!empty($found_files)) {
- return $found_files;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement