Bitrix-D7  20.0.0
main/lib/application.php
См. документацию.
1 <?php
2 /**
3  * Bitrix Framework
4  * @package bitrix
5  * @subpackage main
6  * @copyright 2001-2012 Bitrix
7  */
8 namespace Bitrix\Main;
9 
12 use Bitrix\Main\IO;
13 
14 /**
15  * Base class for any application.
16  */
17 abstract class Application
18 {
19  /**
20  * @var Application
21  */
22  protected static $instance = null;
23 
24  protected $isBasicKernelInitialized = false;
25  protected $isExtendedKernelInitialized = false;
26 
27  /**
28  * Execution context.
29  *
30  * @var Context
31  */
32  protected $context;
33 
34  /**
35  * Pool of database connections.
36  *
37  * @var Data\ConnectionPool
38  */
39  protected $connectionPool;
40 
41  /**
42  * Managed cache instance.
43  *
44  * @var \Bitrix\Main\Data\ManagedCache
45  */
46  protected $managedCache;
47 
48  /**
49  * Tagged cache instance.
50  *
51  * @var \Bitrix\Main\Data\TaggedCache
52  */
53  protected $taggedCache;
54 
55  /**
56  * LRU cache instance.
57  *
58  * @var \Bitrix\Main\Data\LruCache
59  */
60  protected $lruCache;
61 
62  /**
63  * @var \Bitrix\Main\Diag\ExceptionHandler
64  */
65  protected $exceptionHandler = null;
66 
67  /**
68  * @var Dispatcher
69  */
70  private $dispatcher = null;
71 
72  /**
73  * Creates new application instance.
74  */
75  protected function __construct()
76  {
77 
78  }
79 
80  /**
81  * Returns current instance of the Application.
82  *
83  * @return Application
84  * @throws SystemException
85  */
86  public static function getInstance()
87  {
88  if (!isset(static::$instance))
89  static::$instance = new static();
90 
91  return static::$instance;
92  }
93 
94  /**
95  * Does minimally possible kernel initialization
96  *
97  * @throws SystemException
98  */
99  public function initializeBasicKernel()
100  {
101  if ($this->isBasicKernelInitialized)
102  return;
103  $this->isBasicKernelInitialized = true;
104 
106  $this->initializeCache();
107  $this->createDatabaseConnection();
108  }
109 
110  /**
111  * Does full kernel initialization. Should be called somewhere after initializeBasicKernel()
112  *
113  * @param array $params Parameters of the current request (depends on application type)
114  * @throws SystemException
115  */
116  public function initializeExtendedKernel(array $params)
117  {
118  if ($this->isExtendedKernelInitialized)
119  return;
120  $this->isExtendedKernelInitialized = true;
121 
122  $this->initializeContext($params);
123 
124  //$this->initializeDispatcher();
125  }
126 
127  final public function getDispatcher()
128  {
129  if (is_null($this->dispatcher))
130  throw new NotSupportedException();
131  if (!($this->dispatcher instanceof Dispatcher))
132  throw new NotSupportedException();
133 
134  return clone $this->dispatcher;
135  }
136 
137  /**
138  * Initializes context of the current request.
139  * Should be implemented in subclass.
140  */
141  abstract protected function initializeContext(array $params);
142 
143  /**
144  * Starts request execution. Should be called after initialize.
145  * Should be implemented in subclass.
146  */
147  abstract public function start();
148 
149  /**
150  * Runs controller and its action and sends response to the output.
151  *
152  * It's a stub method and we can't mark it as abstract because there is compatibility.
153  * @return void
154  */
155  public function run()
156  {}
157 
158  /**
159  * Ends work of application.
160  * Sends response and then terminates execution.
161  * If there is no $response the method will use Context::$response.
162  *
163  * @param int $status
164  * @param Response|null $response
165  *
166  * @return void
167  * @throws SystemException
168  */
169  public function end($status = 0, Response $response = null)
170  {
171  $response = $response?: $this->context->getResponse();
172  $response->send();
173 
174  $this->terminate($status);
175  }
176 
177  /**
178  * Terminates application by invoking exit().
179  * It's the right way to finish application @see \CMain::finalActions().
180  *
181  * @param int $status
182  * @return void
183  */
184  public function terminate($status = 0)
185  {
186  /** @noinspection PhpUndefinedClassInspection */
187  \CMain::runFinalActionsInternal();
188  exit($status);
189  }
190 
191  /**
192  * Exception handler can be initialized through the Config\Configuration (.settings.php file).
193  *
194  * 'exception_handling' => array(
195  * 'value' => array(
196  * 'debug' => true, // output exception on screen
197  * 'handled_errors_types' => E_ALL & ~E_STRICT & ~E_NOTICE, // catchable error types, printed to log
198  * 'exception_errors_types' => E_ALL & ~E_NOTICE & ~E_STRICT, // error types from catchable which throws exceptions
199  * 'ignore_silence' => false, // ignore @
200  * 'assertion_throws_exception' => true, // assertion throws exception
201  * 'assertion_error_type' => 256,
202  * 'log' => array(
203  * 'class_name' => 'MyLog', // custom log class, must extends ExceptionHandlerLog; can be omited, in this case default Diag\FileExceptionHandlerLog will be used
204  * 'extension' => 'MyLogExt', // php extension, is used only with 'class_name'
205  * 'required_file' => 'modules/mylog.module/mylog.php' // included file, is used only with 'class_name'
206  * 'settings' => array( // any settings for 'class_name'
207  * 'file' => 'bitrix/modules/error.log',
208  * 'log_size' => 1000000,
209  * ),
210  * ),
211  * ),
212  * 'readonly' => false,
213  * ),
214  *
215  */
216  protected function initializeExceptionHandler()
217  {
219 
220  $exceptionHandling = Config\Configuration::getValue("exception_handling");
221  if ($exceptionHandling == null)
222  $exceptionHandling = array();
223 
224  if (!isset($exceptionHandling["debug"]) || !is_bool($exceptionHandling["debug"]))
225  $exceptionHandling["debug"] = false;
226  $exceptionHandler->setDebugMode($exceptionHandling["debug"]);
227 
228  if (isset($exceptionHandling["handled_errors_types"]) && is_int($exceptionHandling["handled_errors_types"]))
229  $exceptionHandler->setHandledErrorsTypes($exceptionHandling["handled_errors_types"]);
230 
231  if (isset($exceptionHandling["exception_errors_types"]) && is_int($exceptionHandling["exception_errors_types"]))
232  $exceptionHandler->setExceptionErrorsTypes($exceptionHandling["exception_errors_types"]);
233 
234  if (isset($exceptionHandling["ignore_silence"]) && is_bool($exceptionHandling["ignore_silence"]))
235  $exceptionHandler->setIgnoreSilence($exceptionHandling["ignore_silence"]);
236 
237  if (isset($exceptionHandling["assertion_throws_exception"]) && is_bool($exceptionHandling["assertion_throws_exception"]))
238  $exceptionHandler->setAssertionThrowsException($exceptionHandling["assertion_throws_exception"]);
239 
240  if (isset($exceptionHandling["assertion_error_type"]) && is_int($exceptionHandling["assertion_error_type"]))
241  $exceptionHandler->setAssertionErrorType($exceptionHandling["assertion_error_type"]);
242 
243  $exceptionHandler->initialize(
244  array($this, "createExceptionHandlerOutput"),
245  array($this, "createExceptionHandlerLog")
246  );
247 
248  $this->exceptionHandler = $exceptionHandler;
249  }
250 
251  public function createExceptionHandlerLog()
252  {
253  $exceptionHandling = Config\Configuration::getValue("exception_handling");
254  if ($exceptionHandling === null || !is_array($exceptionHandling) || !isset($exceptionHandling["log"]) || !is_array($exceptionHandling["log"]))
255  return null;
256 
257  $options = $exceptionHandling["log"];
258 
259  $log = null;
260 
261  if (isset($options["class_name"]) && !empty($options["class_name"]))
262  {
263  if (isset($options["extension"]) && !empty($options["extension"]) && !extension_loaded($options["extension"]))
264  return null;
265 
266  if (isset($options["required_file"]) && !empty($options["required_file"]) && ($requiredFile = Loader::getLocal($options["required_file"])) !== false)
267  require_once($requiredFile);
268 
269  $className = $options["class_name"];
270  if (!class_exists($className))
271  return null;
272 
273  $log = new $className();
274  }
275  elseif (isset($options["settings"]) && is_array($options["settings"]))
276  {
277  $log = new Diag\FileExceptionHandlerLog();
278  }
279  else
280  {
281  return null;
282  }
283 
284  $log->initialize(
285  isset($options["settings"]) && is_array($options["settings"]) ? $options["settings"] : array()
286  );
287 
288  return $log;
289  }
290 
292  {
293  return new Diag\ExceptionHandlerOutput();
294  }
295 
296  /**
297  * Creates database connection pool.
298  */
299  protected function createDatabaseConnection()
300  {
301  $this->connectionPool = new Data\ConnectionPool();
302  }
303 
304  protected function initializeCache()
305  {
306  //TODO: Should be transfered to where GET parameter is defined in future
307  //magic parameters: show cache usage statistics
308  $show_cache_stat = "";
309  if (isset($_GET["show_cache_stat"]))
310  {
311  $show_cache_stat = (strtoupper($_GET["show_cache_stat"]) == "Y" ? "Y" : "");
312  @setcookie("show_cache_stat", $show_cache_stat, false, "/");
313  }
314  elseif (isset($_COOKIE["show_cache_stat"]))
315  {
316  $show_cache_stat = $_COOKIE["show_cache_stat"];
317  }
318  Data\Cache::setShowCacheStat($show_cache_stat === "Y");
319 
320  if (isset($_GET["clear_cache_session"]))
321  Data\Cache::setClearCacheSession($_GET["clear_cache_session"] === 'Y');
322  if (isset($_GET["clear_cache"]))
323  Data\Cache::setClearCache($_GET["clear_cache"] === 'Y');
324  }
325 
326  /*
327  final private function initializeDispatcher()
328  {
329  $dispatcher = new Dispatcher();
330  $dispatcher->initialize();
331  $this->dispatcher = $dispatcher;
332  }
333  */
334 
335  /**
336  * @return \Bitrix\Main\Diag\ExceptionHandler
337  */
338  public function getExceptionHandler()
339  {
341  }
342 
343  /**
344  * Returns database connections pool object.
345  *
346  * @return Data\ConnectionPool
347  */
348  public function getConnectionPool()
349  {
350  return $this->connectionPool;
351  }
352 
353  /**
354  * Returns context of the current request.
355  *
356  * @return Context
357  */
358  public function getContext()
359  {
360  return $this->context;
361  }
362 
363  /**
364  * Modifies context of the current request.
365  *
366  * @param Context $context
367  */
368  public function setContext(Context $context)
369  {
370  $this->context = $context;
371  }
372 
373  /**
374  * Static method returns database connection for the specified name.
375  * If name is empty - default connection is returned.
376  *
377  * @static
378  * @param string $name Name of database connection. If empty - default connection.
379  * @return DB\Connection
380  */
381  public static function getConnection($name = "")
382  {
383  $pool = Application::getInstance()->getConnectionPool();
384  return $pool->getConnection($name);
385  }
386 
387  /**
388  * Returns new instance of the Cache object.
389  *
390  * @return Data\Cache
391  */
392  public function getCache()
393  {
395  }
396 
397  /**
398  * Returns manager of the managed cache.
399  *
400  * @return Data\ManagedCache
401  */
402  public function getManagedCache()
403  {
404  if ($this->managedCache == null)
405  {
406  $this->managedCache = new Data\ManagedCache();
407  }
408 
409  return $this->managedCache;
410  }
411 
412  /**
413  * Returns manager of the managed cache.
414  *
415  * @return Data\TaggedCache
416  */
417  public function getTaggedCache()
418  {
419  if ($this->taggedCache == null)
420  {
421  $this->taggedCache = new Data\TaggedCache();
422  }
423 
424  return $this->taggedCache;
425  }
426 
427  /**
428  * Returns UF manager.
429  *
430  * @return \CUserTypeManager
431  */
432  public static function getUserTypeManager()
433  {
434  global $USER_FIELD_MANAGER;
435  return $USER_FIELD_MANAGER;
436  }
437 
438  /**
439  * Returns true id server is in utf-8 mode. False - otherwise.
440  *
441  * @return bool
442  */
443  public static function isUtfMode()
444  {
445  static $isUtfMode = null;
446  if ($isUtfMode === null)
447  {
448  $isUtfMode = Config\Configuration::getValue("utf_mode");
449  if ($isUtfMode === null)
450  $isUtfMode = false;
451  }
452  return $isUtfMode;
453  }
454 
455  /**
456  * Returns server document root.
457  *
458  * @return null|string
459  */
460  public static function getDocumentRoot()
461  {
462  static $documentRoot = null;
463  if ($documentRoot != null)
464  return $documentRoot;
465 
466  $context = Application::getInstance()->getContext();
467  if ($context != null)
468  {
469  $server = $context->getServer();
470  if ($server != null)
471  return $documentRoot = $server->getDocumentRoot();
472  }
473 
474  return Loader::getDocumentRoot();
475  }
476 
477  /**
478  * Returns personal root directory (relative to document root)
479  *
480  * @return null|string
481  */
482  public static function getPersonalRoot()
483  {
484  static $personalRoot = null;
485  if ($personalRoot != null)
486  return $personalRoot;
487 
488  $context = Application::getInstance()->getContext();
489  if ($context != null)
490  {
491  $server = $context->getServer();
492  if ($server != null)
493  return $personalRoot = $server->getPersonalRoot();
494  }
495 
496  return isset($_SERVER["BX_PERSONAL_ROOT"]) ? $_SERVER["BX_PERSONAL_ROOT"] : "/bitrix";
497  }
498 
499  /**
500  * Resets accelerator if any.
501  */
502  public static function resetAccelerator()
503  {
504  if (defined("BX_NO_ACCELERATOR_RESET"))
505  return;
506 
507  $fl = Config\Configuration::getValue("no_accelerator_reset");
508  if ($fl)
509  return;
510 
511  if (function_exists("accelerator_reset"))
512  accelerator_reset();
513  elseif (function_exists("wincache_refresh_if_changed"))
514  wincache_refresh_if_changed();
515  }
516 }
Bitrix\Main\Application
Base class for any application.
Definition: main/lib/application.php:17
Bitrix\Main\Application\getManagedCache
getManagedCache()
Returns manager of the managed cache.
Definition: main/lib/application.php:402
Bitrix\Main\Data\ConnectionPool
Connection pool is a connections holder.
Definition: connectionpool.php:17
Bitrix\Main\Application\$context
$context
Definition: main/lib/application.php:32
Bitrix\Main\Application\createExceptionHandlerLog
createExceptionHandlerLog()
Definition: main/lib/application.php:251
Bitrix\Main\Application\$instance
static $instance
Definition: main/lib/application.php:22
Bitrix\Main\Data
Definition: aliases.php:105
Bitrix\Main\Application\getUserTypeManager
static getUserTypeManager()
Returns UF manager.
Definition: main/lib/application.php:432
Bitrix\Main
Bitrix\Main\Application\initializeBasicKernel
initializeBasicKernel()
Does minimally possible kernel initialization.
Definition: main/lib/application.php:99
Bitrix\Main\Response
Definition: main/lib/response.php:4
Bitrix\Main\Application\resetAccelerator
static resetAccelerator()
Resets accelerator if any.
Definition: main/lib/application.php:502
Bitrix\Main\Application\getContext
getContext()
Returns context of the current request.
Definition: main/lib/application.php:358
Bitrix\Main\Application\$managedCache
$managedCache
Definition: main/lib/application.php:46
Bitrix\Main\Application\initializeExtendedKernel
initializeExtendedKernel(array $params)
Does full kernel initialization.
Definition: main/lib/application.php:116
Bitrix\Main\Data\Cache\setClearCache
static setClearCache($clearCache)
A privileged user wants to skip cache on this hit.
Definition: cache.php:178
Bitrix\Main\Application\run
run()
Runs controller and its action and sends response to the output.
Definition: main/lib/application.php:155
Bitrix\Main\Context
Context of current request.
Definition: main/lib/context.php:13
Bitrix\Main\Data\TaggedCache
Definition: taggedcache.php:13
Bitrix\Main\Application\$isExtendedKernelInitialized
$isExtendedKernelInitialized
Definition: main/lib/application.php:25
Bitrix\Main\Application\$connectionPool
$connectionPool
Definition: main/lib/application.php:39
Bitrix\Main\Application\createDatabaseConnection
createDatabaseConnection()
Creates database connection pool.
Definition: main/lib/application.php:299
Bitrix\Main\Application\getConnection
static getConnection($name="")
Static method returns database connection for the specified name.
Definition: main/lib/application.php:381
Bitrix\Main\Diag\ExceptionHandlerOutput
Definition: exceptionhandleroutput.php:8
Bitrix\Main\Data\Cache\setShowCacheStat
static setShowCacheStat($showCacheStat)
Definition: cache.php:164
Bitrix\Main\Application\getExceptionHandler
getExceptionHandler()
Definition: main/lib/application.php:338
Bitrix\Main\NotSupportedException
Exception is thrown when operation is not supported.
Definition: main/lib/exception.php:158
Bitrix\Main\Application\__construct
__construct()
Creates new application instance.
Definition: main/lib/application.php:75
Bitrix\Main\Application\$exceptionHandler
$exceptionHandler
Definition: main/lib/application.php:65
Bitrix\Main\Application\isUtfMode
static isUtfMode()
Returns true id server is in utf-8 mode.
Definition: main/lib/application.php:443
Bitrix\Main\Diag\FileExceptionHandlerLog
Definition: fileexceptionhandlerlog.php:6
Bitrix\Main\Application\getInstance
static getInstance()
Returns current instance of the Application.
Definition: main/lib/application.php:86
Bitrix\Main\Data\Cache\setClearCacheSession
static setClearCacheSession($clearCacheSession)
A privileged user wants to skip cache on this session.
Definition: cache.php:187
Bitrix\Main\Application\getCache
getCache()
Returns new instance of the Cache object.
Definition: main/lib/application.php:392
Bitrix\Main\Application\getDocumentRoot
static getDocumentRoot()
Returns server document root.
Definition: main/lib/application.php:460
Bitrix\Main\IO
Definition: main/lib/io/directory.php:2
Bitrix\Main\Application\createExceptionHandlerOutput
createExceptionHandlerOutput()
Definition: main/lib/application.php:291
Bitrix\Main\Application\initializeExceptionHandler
initializeExceptionHandler()
Exception handler can be initialized through the Config\Configuration (.settings.php file).
Definition: main/lib/application.php:216
Bitrix\Main\Application\initializeCache
initializeCache()
Definition: main/lib/application.php:304
Bitrix\Main\Application\$lruCache
$lruCache
Definition: main/lib/application.php:60
Bitrix\Main\Application\terminate
terminate($status=0)
Terminates application by invoking exit().
Definition: main/lib/application.php:184
Bitrix\Main\Application\$taggedCache
$taggedCache
Definition: main/lib/application.php:53
Bitrix\Main\Diag
Definition: cachetracker.php:2
Bitrix\Main\Data\ManagedCache
Definition: managedcache.php:13
Bitrix\Main\Application\end
end($status=0, Response $response=null)
Ends work of application.
Definition: main/lib/application.php:169
Bitrix\Main\Loader\getDocumentRoot
static getDocumentRoot()
Returns document root.
Definition: main/lib/loader.php:198
Bitrix\Main\Application\getConnectionPool
getConnectionPool()
Returns database connections pool object.
Definition: main/lib/application.php:348
Bitrix\Main\Loader\getLocal
static getLocal($path, $root=null)
Checks if file exists in /local or /bitrix directories.
Definition: main/lib/loader.php:439
Bitrix\Main\Application\start
start()
Starts request execution.
Bitrix\Main\Application\setContext
setContext(Context $context)
Modifies context of the current request.
Definition: main/lib/application.php:368
Bitrix\Main\Application\$isBasicKernelInitialized
$isBasicKernelInitialized
Definition: main/lib/application.php:24
Bitrix\Main\Application\getPersonalRoot
static getPersonalRoot()
Returns personal root directory (relative to document root)
Definition: main/lib/application.php:482
Bitrix\Main\Diag\ExceptionHandler
Definition: exceptionhandler.php:6
Bitrix\Main\Application\getDispatcher
getDispatcher()
Definition: main/lib/application.php:127
Bitrix\Main\Application\getTaggedCache
getTaggedCache()
Returns manager of the managed cache.
Definition: main/lib/application.php:417
Bitrix\Main\Data\Cache\createInstance
static createInstance($params=[])
Definition: cache.php:153
Bitrix\Main\Application\initializeContext
initializeContext(array $params)
Initializes context of the current request.
Bitrix\Main\Dispatcher
Definition: dispatcher.php:6