Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.29 KB | None | 0 0
  1. <?php
  2. /**
  3.  * DeliciousPHP
  4.  *
  5.  * An open source application development framework for PHP 5.3 or newer
  6.  *
  7.  * NOTICE OF LICENSE
  8.  *
  9.  * Licensed under the Open Software License version 3.0
  10.  *
  11.  * This source file is subject to the Open Software License (OSL 3.0) that is
  12.  * bundled with this package in the files license.txt / license.rst.  It is
  13.  * also available through the world wide web at this URL:
  14.  * http://opensource.org/licenses/OSL-3.0
  15.  *
  16.  * @package DeliciousPHP
  17.  * @author Michael Beers <michael@michaelbeers.nl>
  18.  * @copyright (c) 2012 - 2013, Michael Beers | Online Media en Design
  19.  * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  20.  * @link http://michaelbeers.nl
  21.  * @since Version 1.0
  22.  * @filesource
  23.  */
  24.  
  25. namespace DeliciousPHP;
  26.  
  27. /**
  28.  * The core of the framework and it's dependency container. It holds references
  29.  * to all framework wide instances, like configs, sessions, debugging etc.
  30.  * Instead of calling a class constructor call a wrapping function of this class
  31.  * to construct the object for you. You can extend this class adding properties
  32.  * that you want to be accessible all around your app.
  33.  *
  34.  * Do not use this class directly. Instead, use its child class [[\App\Delicious]]
  35.  * where you can customize the methods of this class.
  36.  *
  37.  * @package DeliciousPHP
  38.  * @author Michael Beers <michael@michaelbeers.nl>
  39.  * @copyright (c) 2012 - 2013, Michael Beers | Online Media en Design
  40.  * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  41.  * @link http://michaelbeers.nl
  42.  * @since Version 1.0
  43.  *
  44.  * @property-read \DeliciousPHP\Config $config Description
  45.  * @property-read \DeliciousPHP\Cookie $cookie Description
  46.  * @property-read \DeliciousPHP\Debug $debug Error/Debugging handler.
  47.  * @property-read \DeliciousPHP\Router $router Description
  48.  * @property-read \DeliciousPHP\Session $session Description
  49.  */
  50. class Delicious {
  51.  
  52.     /**
  53.      * List with framework modules.
  54.      *
  55.      * @var array
  56.      */
  57.     protected $modules = array();
  58.  
  59.     /**
  60.      * List with framework instances.
  61.      *
  62.      * @var array
  63.      */
  64.     protected $instances = array();
  65.  
  66.     /**
  67.      * List with resources that will be used in the framework.
  68.      *
  69.      * @var array
  70.      */
  71.     protected $resources = array(
  72.         'config' => '\DeliciousPHP\Config',
  73.         'cookie' => '\DeliciousPHP\Cookie',
  74.         'debug' => '\DeliciousPHP\Debug',
  75.         'router' => '\DeliciousPHP\Router',
  76.         'session' => '\DeliciousPHP\Session'
  77.     );
  78.  
  79.     /**
  80.      * List with paths to data directories.
  81.      *
  82.      * @var type
  83.      */
  84.     protected $data_dirs = array();
  85.  
  86.     /**
  87.      * Base path of the application.
  88.      *
  89.      * @var string
  90.      */
  91.     public $basepath = '/';
  92.  
  93.     /**
  94.      * Stores the namespace of the application.
  95.      *
  96.      * @var string
  97.      */
  98.     public $app_namespace;
  99.  
  100.     // ----------------------------------------------------------------------
  101.  
  102.     /**
  103.      * Get a property by name. Returns defined class and module instances.
  104.      *
  105.      * @param string $name Property name.
  106.      * @return mixed Instance of defined class or module.
  107.      * @throws \Exception When a property is not found.
  108.      */
  109.     public function __get($name) {
  110.         if (isset($this->instances[$name])) {
  111.             return $this->instances[$name];
  112.         }
  113.  
  114.         if (isset($this->modules[$name])) {
  115.             return $this->instances[$name] = new $this->modules[$name]($this);
  116.         }
  117.  
  118.         if (isset($this->resources[$name])) {
  119.             return $this->instances[$name] = new $this->resources[$name]($this);
  120.         }
  121.  
  122.         throw new \Exception("Property {$name} not found in " . get_class($this));
  123.     }
  124.  
  125.     // ----------------------------------------------------------------------
  126.  
  127.     /**
  128.      * Bootstraps the project.
  129.      *
  130.      * @return \DeliciousPHP\Delicious
  131.      */
  132.     public function bootstrap() {
  133.         // Initialize application namespace.
  134.         if ($this->app_namespace === null) {
  135.             $class_name = get_class($this);
  136.             $this->app_namespace = substr($class_name, 0, strpos($class_name, "\\") + 1);
  137.         }
  138.  
  139.         // Add the root to data directories.
  140.         $this->data_dirs[] = BASE_PATH . 'data' . DS;
  141.  
  142.         // Initialize the error handler and debug logger.
  143.         $this->debug->init();
  144.  
  145.         // Load all modules.
  146.         foreach ($this->modules as $name => $class) {
  147.             $this->$name = new $class($this);
  148.         }
  149.  
  150.         // Add the public to data directories.
  151.         array_unshift($this->data_dirs, PUBLIC_PATH . 'data' . DS);
  152.  
  153.         // Add the routes.
  154.         foreach ($this->config->get('routes') as $name => $rule) {
  155.             $methods = $this->array_key(2, null, $rule);
  156.             $route = $this->route($name, $rule[0], $rule[1], $methods);
  157.             $this->router->add($route);
  158.         }
  159.  
  160.         $this->bootstrap_after();
  161.         return $this;
  162.     }
  163.  
  164.     /**
  165.      * Perform some initialization after the bootstrap is finished loading.
  166.      *
  167.      * Note: You should overwrite this method.
  168.      */
  169.     protected function bootstrap_after() {
  170.         // Does nothing.
  171.     }
  172.  
  173.     // ----------------------------------------------------------------------
  174.  
  175.     /**
  176.      * Creates a Request representing current HTTP request.
  177.      *
  178.      * @return \DeliciousPHP\Request
  179.      */
  180.     public function http_request() {
  181.         $uri = $_SERVER['REQUEST_URI'];
  182.         $uri = preg_replace("#^{$this->basepath}(?:index\.php/)?#i", '/', $uri);
  183.         $url_parts = parse_url($uri);
  184.         $route_data = $this->router->match($url_parts['path'], $_SERVER['REQUEST_METHOD']);
  185.         return $this->request($route_data['route'], $_SERVER['REQUEST_METHOD'], $_POST, $_GET, $route_data['params'], $_SERVER, $_COOKIE);
  186.     }
  187.  
  188.     /**
  189.      * Processes HTTP request, executes it and sends back the response.
  190.      */
  191.     public function handle_http_request() {
  192.         try {
  193.             $request = $this->http_request();
  194.             $response = $request->execute();
  195.             $response->send_headers()->send_body();
  196.         } catch (\Exception $e) {
  197.             $this->handle_exception($e);
  198.         }
  199.     }
  200.  
  201.     /**
  202.      * Exception handler. By default displays the error page.
  203.      * If you want your exceptions to be handled in a specific way
  204.      * you should override this method.
  205.      *
  206.      * @param \Exception $exception Exception to handle
  207.      */
  208.     public function handle_exception(\Exception $exception) {
  209.         $this->debug->render($exception);
  210.     }
  211.  
  212.     // ----------------------------------------------------------------------
  213.  
  214.     /**
  215.      * Constructs a new controller by class name.
  216.      *
  217.      * @param string $class
  218.      * @return \DeliciousPHP\Controller
  219.      */
  220.     public function controller($class) {
  221.         if (!class_exists($class)) {
  222.             throw new \DeliciousPHP\Exception\PageNotFound("Class {$class} doesn't exist");
  223.         }
  224.  
  225.         return new $class($this);
  226.     }
  227.  
  228.     /**
  229.      * Constructs a new request.
  230.      *
  231.      * @param \DeliciousPHP\Delicious $delicious DeliciousPHP dependency container.
  232.      * @param \DeliciousPHP\Route $route Route for this request.
  233.      * @param type $method HTTP method for the request (e.g. GET, POST)
  234.      * @param type $post Array of POST data
  235.      * @param type $get Array of GET data
  236.      * @param type $param Array of URL parameters
  237.      * @param type $server Array of SERVER data
  238.      * @param type $cookie Array of COOKIE data
  239.      * @return \DeliciousPHP\Request
  240.      */
  241.     public function request(\DeliciousPHP\Route $route, $method = "GET", $post = array(), $get = array(), $param = array(), $server = array(), $cookie = array()) {
  242.         return new \DeliciousPHP\Request($this, $route, $method, $post, $get, $param, $server, $cookie);
  243.     }
  244.  
  245.     /**
  246.      * Constructs a new response.
  247.      *
  248.      * @return \DeliciousPHP\Response
  249.      */
  250.     public function response() {
  251.         return new \DeliciousPHP\Response($this);
  252.     }
  253.  
  254.     /**
  255.      * Constructs a new route.
  256.      *
  257.      * @param string $name Name of this route.
  258.      * @param mixed $rule Rule for this route.
  259.      * @param array $defaults Default parameters for this route.
  260.      * @param mixed $methods Methods to restrict this route to.
  261.      * @return \DeliciousPHP\Route
  262.      */
  263.     public function route($name, $rule, $defaults, $methods = null) {
  264.         return new \DeliciousPHP\Route($name, $rule, $defaults, $methods);
  265.     }
  266.    
  267.     /**
  268.      * Constructs a view.
  269.      *
  270.      * @param string $name Name of the template to use.
  271.      * @return \DeliciousPHP\View
  272.      */
  273.     public function view($name) {
  274.         return new \DeliciousPHP\View($this, $this->view_helper(), $name);
  275.     }
  276.    
  277.     /**
  278.      * Constructs a view helper.
  279.      *
  280.      * @return \DeliciousPHP\View\Helper
  281.      */
  282.     public function view_helper() {
  283.         return new \DeliciousPHP\View\Helper($this);
  284.     }
  285.  
  286.     // ----------------------------------------------------------------------
  287.  
  288.     /**
  289.      * Fetch a key from an array, with default value support.
  290.      *
  291.      * @param type $key Value to check.
  292.      * @param type $default Default value on failure.
  293.      * @param array $array An array with keys to check.
  294.      * @return mixed An array value if it was found or default value if it is not.
  295.      */
  296.     public function array_key($key, $default, array $array) {
  297.         if (isset($array[$key])) {
  298.             return $array[$key];
  299.         }
  300.  
  301.         return $default;
  302.     }
  303.  
  304.     /**
  305.      * Finds full path to a specified file in the data directories.
  306.      *
  307.      * @param type $subfolder
  308.      * @param type $name
  309.      * @param type $return_all
  310.      * @return string|boolean
  311.      */
  312.     public function find_file($subfolder, $name, $return_all = false) {
  313.         $fname = $name . EXT;
  314.         $found_files = array();
  315.  
  316.         foreach ($this->data_dirs as $folder) {
  317.             $file = $folder . $subfolder . DS . $fname;
  318.             if (file_exists($file)) {
  319.                 if (!$return_all) {
  320.                     return $file;
  321.                 }
  322.  
  323.                 $found_files[] = $file;
  324.             }
  325.         }
  326.  
  327.         if (!empty($found_files)) {
  328.             return $found_files;
  329.         }
  330.  
  331.         return false;
  332.     }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement