shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

loader.php (7066B)


      1 <?php
      2 /**
      3  * @package		OpenCart
      4  * @author		Daniel Kerr
      5  * @copyright	Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/)
      6  * @license		https://opensource.org/licenses/GPL-3.0
      7  * @link		https://www.opencart.com
      8 */
      9 
     10 /**
     11 * Loader class
     12 */
     13 final class Loader {
     14 	protected $registry;
     15 
     16 	/**
     17 	 * Constructor
     18 	 *
     19 	 * @param	object	$registry
     20  	*/
     21 	public function __construct($registry) {
     22 		$this->registry = $registry;
     23 	}
     24 
     25 	/**
     26 	 * 
     27 	 *
     28 	 * @param	string	$route
     29 	 * @param	array	$data
     30 	 *
     31 	 * @return	mixed
     32  	*/	
     33 	public function controller($route, $data = array()) {
     34 		// Sanitize the call
     35 		$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
     36 		
     37 		// Keep the original trigger
     38 		$trigger = $route;
     39 		
     40 		// Trigger the pre events
     41 		$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/before', array(&$route, &$data));
     42 		
     43 		// Make sure its only the last event that returns an output if required.
     44 		if ($result != null && !$result instanceof Exception) {
     45 			$output = $result;
     46 		} else {
     47 			$action = new Action($route);
     48 			$output = $action->execute($this->registry, array(&$data));
     49 		}
     50 		
     51 		// Trigger the post events
     52 		$result = $this->registry->get('event')->trigger('controller/' . $trigger . '/after', array(&$route, &$data, &$output));
     53 		
     54 		if ($result && !$result instanceof Exception) {
     55 			$output = $result;
     56 		}
     57 
     58 		if (!$output instanceof Exception) {
     59 			return $output;
     60 		}
     61 	}
     62 
     63 	/**
     64 	 * 
     65 	 *
     66 	 * @param	string	$route
     67  	*/	
     68 	public function model($route) {
     69 		// Sanitize the call
     70 		$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
     71 		
     72 		if (!$this->registry->has('model_' . str_replace('/', '_', $route))) {
     73 			$file  = DIR_APPLICATION . 'model/' . $route . '.php';
     74 			$class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $route);
     75 			
     76 			if (is_file($file)) {
     77 				include_once($file);
     78 	
     79 				$proxy = new Proxy();
     80 				
     81 				// Overriding models is a little harder so we have to use PHP's magic methods
     82 				// In future version we can use runkit
     83 				foreach (get_class_methods($class) as $method) {
     84 					$proxy->{$method} = $this->callback($this->registry, $route . '/' . $method);
     85 				}
     86 				
     87 				$this->registry->set('model_' . str_replace('/', '_', (string)$route), $proxy);
     88 			} else {
     89 				throw new \Exception('Error: Could not load model ' . $route . '!');
     90 			}
     91 		}
     92 	}
     93 
     94 	/**
     95 	 * 
     96 	 *
     97 	 * @param	string	$route
     98 	 * @param	array	$data
     99 	 *
    100 	 * @return	string
    101  	*/
    102 	public function view($route, $data = array()) {
    103 		// Sanitize the call
    104 		$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
    105 		
    106 		// Keep the original trigger
    107 		$trigger = $route;
    108 		
    109 		// Template contents. Not the output!
    110 		$template = '';
    111 		
    112 		// Trigger the pre events
    113 		$result = $this->registry->get('event')->trigger('view/' . $trigger . '/before', array(&$route, &$data, &$template));
    114 		
    115 		// Make sure its only the last event that returns an output if required.
    116 		if ($result && !$result instanceof Exception) {
    117 			$output = $result;
    118 		} else {
    119 			$template = new Template($this->registry->get('config')->get('template_engine'));
    120 				
    121 			foreach ($data as $key => $value) {
    122 				$template->set($key, $value);
    123 			}
    124 
    125 			$output = $template->render($this->registry->get('config')->get('template_directory') . $route, $this->registry->get('config')->get('template_cache'));		
    126 		}
    127 		
    128 		// Trigger the post events
    129 		$result = $this->registry->get('event')->trigger('view/' . $trigger . '/after', array(&$route, &$data, &$output));
    130 		
    131 		if ($result && !$result instanceof Exception) {
    132 			$output = $result;
    133 		}
    134 		
    135 		return $output;
    136 	}
    137 
    138 	/**
    139 	 * 
    140 	 *
    141 	 * @param	string	$route
    142  	*/
    143 	public function library($route) {
    144 		// Sanitize the call
    145 		$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
    146 			
    147 		$file = DIR_SYSTEM . 'library/' . $route . '.php';
    148 		$class = str_replace('/', '\\', $route);
    149 
    150 		if (is_file($file)) {
    151 			include_once($file);
    152 
    153 			$this->registry->set(basename($route), new $class($this->registry));
    154 		} else {
    155 			throw new \Exception('Error: Could not load library ' . $route . '!');
    156 		}
    157 	}
    158 
    159 	/**
    160 	 * 
    161 	 *
    162 	 * @param	string	$route
    163  	*/	
    164 	public function helper($route) {
    165 		$file = DIR_SYSTEM . 'helper/' . preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route) . '.php';
    166 
    167 		if (is_file($file)) {
    168 			include_once($file);
    169 		} else {
    170 			throw new \Exception('Error: Could not load helper ' . $route . '!');
    171 		}
    172 	}
    173 
    174 	/**
    175 	 * 
    176 	 *
    177 	 * @param	string	$route
    178  	*/	
    179 	public function config($route) {
    180 		$this->registry->get('event')->trigger('config/' . $route . '/before', array(&$route));
    181 		
    182 		$this->registry->get('config')->load($route);
    183 		
    184 		$this->registry->get('event')->trigger('config/' . $route . '/after', array(&$route));
    185 	}
    186 
    187 	/**
    188 	 * 
    189 	 *
    190 	 * @param	string	$route
    191 	 * @param	string	$key
    192 	 *
    193 	 * @return	array
    194  	*/
    195 	public function language($route, $key = '') {
    196 		// Sanitize the call
    197 		$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
    198 		
    199 		// Keep the original trigger
    200 		$trigger = $route;
    201 				
    202 		$result = $this->registry->get('event')->trigger('language/' . $trigger . '/before', array(&$route, &$key));
    203 		
    204 		if ($result && !$result instanceof Exception) {
    205 			$output = $result;
    206 		} else {
    207 			$output = $this->registry->get('language')->load($route, $key);
    208 		}
    209 		
    210 		$result = $this->registry->get('event')->trigger('language/' . $trigger . '/after', array(&$route, &$key, &$output));
    211 		
    212 		if ($result && !$result instanceof Exception) {
    213 			$output = $result;
    214 		}
    215 				
    216 		return $output;
    217 	}
    218 	
    219 	protected function callback($registry, $route) {
    220 		return function($args) use($registry, $route) {
    221 			static $model;
    222 			
    223 			$route = preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route);
    224 
    225 			// Keep the original trigger
    226 			$trigger = $route;
    227 					
    228 			// Trigger the pre events
    229 			$result = $registry->get('event')->trigger('model/' . $trigger . '/before', array(&$route, &$args));
    230 			
    231 			if ($result && !$result instanceof Exception) {
    232 				$output = $result;
    233 			} else {
    234 				$class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', substr($route, 0, strrpos($route, '/')));
    235 				
    236 				// Store the model object
    237 				$key = substr($route, 0, strrpos($route, '/'));
    238 				
    239 				if (!isset($model[$key])) {
    240 					$model[$key] = new $class($registry);
    241 				}
    242 				
    243 				$method = substr($route, strrpos($route, '/') + 1);
    244 				
    245 				$callable = array($model[$key], $method);
    246 	
    247 				if (is_callable($callable)) {
    248 					$output = call_user_func_array($callable, $args);
    249 				} else {
    250 					throw new \Exception('Error: Could not call model/' . $route . '!');
    251 				}					
    252 			}
    253 			
    254 			// Trigger the post events
    255 			$result = $registry->get('event')->trigger('model/' . $trigger . '/after', array(&$route, &$args, &$output));
    256 			
    257 			if ($result && !$result instanceof Exception) {
    258 				$output = $result;
    259 			}
    260 						
    261 			return $output;
    262 		};
    263 	}	
    264 }