PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/private/rest/classes/debug/debug.php

http://github.com/ezsystems/ezpublish
PHP | 137 lines | 77 code | 18 blank | 42 comment | 9 complexity | 809080ed940757408c07431ccff1c228 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing ezpRestDebug class
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. final class ezpRestDebug
  11. {
  12. private static $instance;
  13. /**
  14. * @var eZINI
  15. */
  16. private $restINI;
  17. /**
  18. * eZDebug instance
  19. * @var eZDebug
  20. */
  21. private $eZDebug;
  22. /**
  23. * @var ezcDebug
  24. */
  25. private $debug;
  26. /**
  27. * @var bool
  28. */
  29. private static $isDebugEnabled;
  30. /**
  31. * Private constructor
  32. */
  33. private function __construct()
  34. {
  35. $this->restINI = eZINI::instance( 'rest.ini' );
  36. $this->eZDebug = eZDebug::instance();
  37. $this->debug = ezcDebug::getInstance();
  38. $this->debug->setOutputFormatter( new ezpRestDebugPHPFormatter() );
  39. }
  40. /**
  41. * Singleton. Get instance of the class
  42. */
  43. public static function getInstance()
  44. {
  45. if ( !self::$instance instanceof ezpRestDebugHandler)
  46. {
  47. self::$instance = new self();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Checks if debug is enabled (locally in rest.ini and globally in site.ini)
  53. * @return bool
  54. */
  55. public static function isDebugEnabled()
  56. {
  57. if( self::$isDebugEnabled === null )
  58. {
  59. $isEnabled = false;
  60. $globalDebugEnabled = eZINI::instance()->variable( 'DebugSettings', 'DebugOutput' ) === 'enabled';
  61. $localDebugEnabled = eZINI::instance( 'rest.ini' )->variable( 'DebugSettings', 'Debug' ) === 'enabled';
  62. if( $globalDebugEnabled && $localDebugEnabled )
  63. $isEnabled = true;
  64. self::$isDebugEnabled = $isEnabled;
  65. }
  66. return self::$isDebugEnabled;
  67. }
  68. /**
  69. * Returns debug report
  70. */
  71. public function getReport()
  72. {
  73. $report = array();
  74. $report['restDebug'] = $this->debug->generateOutput();
  75. $reportEZDebug = $this->eZDebug->printReportInternal( false );
  76. $report['eZDebug'] = explode( "\n", $reportEZDebug );
  77. return $report;
  78. }
  79. /**
  80. * Initializes/updates debug settings, system wide
  81. */
  82. public function updateDebugSettings()
  83. {
  84. $ini = eZINI::instance();
  85. $debugSettings = array();
  86. $debugSettings['debug-enabled'] = ( $ini->variable( 'DebugSettings', 'DebugOutput' ) == 'enabled' and
  87. $this->restINI->variable( 'DebugSettings', 'Debug' ) == 'enabled' );
  88. $debugSettings['debug-by-ip'] = $ini->variable( 'DebugSettings', 'DebugByIP' ) == 'enabled';
  89. $debugSettings['debug-ip-list'] = $ini->variable( 'DebugSettings', 'DebugIPList' );
  90. $logList = $ini->variable( 'DebugSettings', 'AlwaysLog' );
  91. $logMap = array( 'notice' => eZDebug::LEVEL_NOTICE,
  92. 'warning' => eZDebug::LEVEL_WARNING,
  93. 'error' => eZDebug::LEVEL_ERROR,
  94. 'debug' => eZDebug::LEVEL_DEBUG,
  95. 'strict' => eZDebug::LEVEL_STRICT );
  96. $debugSettings['always-log'] = array();
  97. foreach ( $logMap as $name => $level )
  98. {
  99. $debugSettings['always-log'][$level] = in_array( $name, $logList );
  100. }
  101. eZDebug::updateSettings( $debugSettings );
  102. }
  103. /**
  104. * Generic safe way to access ezcDebug public methods
  105. * @param $method
  106. * @param $arguments
  107. */
  108. public function __call( $method, $arguments )
  109. {
  110. if( self::isDebugEnabled() )
  111. {
  112. if ( method_exists( $this->debug, $method ) )
  113. return call_user_func_array( array( $this->debug, $method ), $arguments );
  114. else
  115. throw new ezcBasePropertyNotFoundException( 'ezcDebug::'.$method.'()' );
  116. }
  117. }
  118. }
  119. ?>