action.php (2033B)
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 * Action class 12 */ 13 class Action { 14 private $id; 15 private $route; 16 private $method = 'index'; 17 18 /** 19 * Constructor 20 * 21 * @param string $route 22 */ 23 public function __construct($route) { 24 $this->id = $route; 25 26 $parts = explode('/', preg_replace('/[^a-zA-Z0-9_\/]/', '', (string)$route)); 27 28 // Break apart the route 29 while ($parts) { 30 $file = DIR_APPLICATION . 'controller/' . implode('/', $parts) . '.php'; 31 32 if (is_file($file)) { 33 $this->route = implode('/', $parts); 34 35 break; 36 } else { 37 $this->method = array_pop($parts); 38 } 39 } 40 } 41 42 /** 43 * 44 * 45 * @return string 46 * 47 */ 48 public function getId() { 49 return $this->id; 50 } 51 52 /** 53 * 54 * 55 * @param object $registry 56 * @param array $args 57 */ 58 public function execute($registry, array $args = array()) { 59 // Stop any magical methods being called 60 if (substr($this->method, 0, 2) == '__') { 61 return new \Exception('Error: Calls to magic methods are not allowed!'); 62 } 63 64 $file = DIR_APPLICATION . 'controller/' . $this->route . '.php'; 65 $class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->route); 66 67 // Initialize the class 68 if (is_file($file)) { 69 include_once($file); 70 71 $controller = new $class($registry); 72 } else { 73 return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!'); 74 } 75 76 $reflection = new ReflectionClass($class); 77 78 if ($reflection->hasMethod($this->method) && $reflection->getMethod($this->method)->getNumberOfRequiredParameters() <= count($args)) { 79 return call_user_func_array(array($controller, $this->method), $args); 80 } else { 81 return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!'); 82 } 83 } 84 }