ecom
E-commerce cms
 All Data Structures Namespaces Files Functions Variables
Context.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Tools;
4 
10 class Context
11 {
18  private $hookManager;
19 
23  private static $instance;
24 
31  private $moduleManager;
32 
40  private $router;
41 
49  private $cart;
50 
57  private $user;
58 
64  private $controller;
65 
71  private $ip;
72 
77  private $server;
78 
90  public function __construct($server = null)
91  {
92  self::$instance = $this;
93  if ($server == null)
94  $server = $_SERVER;
95  $this->ip = $server["REMOTE_ADDR"];
96  $this->hookManager = new Hooks($this);
97  $this->server = $server;
98  $this->router = new Router($this->server, $this);
99  }
100 
101  public function serve()
102  {
103  // @codeCoverageIgnoreStart
104  if (!\Entity\ModelBase::init())
105  {
106  @require_once(getcwd().'/core/setup/index.php');
107  die;
108  }
109  // @codeCoverageIgnoreEnd
110  $this->router->init($this->server);
111  $this->moduleManager = new ModuleManager($this);
112  $this->hookManager->trigger("routerSetup");
113  try
114  {
115  $this->controller = $this->router->serveUrl();
116  if (!$this->controller)
117  throw new \Exception\Error404();
118  }
119  catch (\Exception\Error404 $e)
120  {
121  $this->controller = new \Controller\Error404($this, array());
122  }
123  return $this->controller;
124  }
125 
129  public function run()
130  {
131  $this->controller->start();
132  }
133 
137  public function __get($key)
138  {
139  switch ($key)
140  {
141  case "router": return $this->router; break;
142  case "cart": return $this->cart; break;
143  case "user": return $this->user; break;
144  case "moduleManager": return $this->moduleManager; break;
145  case "hookManager": return $this->hookManager; break;
146  case "ip": return $this->ip; break;
147  case "controller": return $this->controller; break;
148  }
149  throw new \Exception("Cannot access attribute {$key}");
150  }
151 
155  public function isTestingEnvironment()
156  {
157  return isset($this->server["phpUnit"]) && $this->server["phpUnit"] == true;
158  }
159 
164  public static function getContext()
165  {
166  return self::$instance;
167  }
168 }
169 
static getContext()
Definition: Context.php:164
__construct($server=null)
Definition: Context.php:90
static $instance
Definition: Context.php:23
isTestingEnvironment()
Definition: Context.php:155
__get($key)
Definition: Context.php:137