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  }
99 
100  public function serve()
101  {
102  $this->router = new Router($this->server, $this);
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  }
124 
128  public function __get($key)
129  {
130  switch ($key)
131  {
132  case "router": return $this->router; break;
133  case "cart": return $this->cart; break;
134  case "user": return $this->user; break;
135  case "moduleManager": return $this->moduleManager; break;
136  case "hookManager": return $this->hookManager; break;
137  case "ip": return $this->ip; break;
138  case "controller": return $this->controller; break;
139  }
140  throw new \Exception("Cannot access attribute {$key}");
141  }
142 
146  public function isTestingEnvironment()
147  {
148  return isset($this->server["phpUnit"]) && $this->server["phpUnit"] == true;
149  }
150 
155  public static function getContext()
156  {
157  return self::$instance;
158  }
159 }
160 
static getContext()
Definition: Context.php:155
__construct($server=null)
Definition: Context.php:90
static $instance
Definition: Context.php:23
isTestingEnvironment()
Definition: Context.php:146
__get($key)
Definition: Context.php:128