PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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