DeprecationService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Use the elgg_* versions instead.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Deprecation
  12. * @since 1.11.0
  13. */
  14. class DeprecationService {
  15. /**
  16. * @var \ElggSession
  17. */
  18. protected $session;
  19. /**
  20. * @var Logger
  21. */
  22. protected $logger;
  23. /**
  24. * Constructor
  25. *
  26. * @param \ElggSession $session Session service
  27. * @param Logger $logger Logger service
  28. */
  29. public function __construct(\ElggSession $session, Logger $logger) {
  30. $this->session = $session;
  31. $this->logger = $logger;
  32. }
  33. /**
  34. * Sends a notice about deprecated use of a function, view, etc.
  35. *
  36. * @param string $msg Message to log
  37. * @param string $dep_version Human-readable *release* version: 1.7, 1.8, ...
  38. * @param int $backtrace_level How many levels back to display the backtrace.
  39. * Useful if calling from functions that are called
  40. * from other places (like elgg_view()). Set to -1
  41. * for a full backtrace.
  42. * @return bool
  43. */
  44. function sendNotice($msg, $dep_version, $backtrace_level = 1) {
  45. if (!$dep_version) {
  46. return false;
  47. }
  48. $elgg_version = elgg_get_version(true);
  49. $elgg_version_arr = explode('.', $elgg_version);
  50. $elgg_major_version = (int)$elgg_version_arr[0];
  51. $elgg_minor_version = (int)$elgg_version_arr[1];
  52. $dep_version_arr = explode('.', (string)$dep_version);
  53. $dep_major_version = (int)$dep_version_arr[0];
  54. $dep_minor_version = (int)$dep_version_arr[1];
  55. $msg = "Deprecated in $dep_major_version.$dep_minor_version: $msg Called from ";
  56. // Get a file and line number for the log. Skip over the function that
  57. // sent this notice and see who called the deprecated function itself.
  58. $stack = array();
  59. $backtrace = debug_backtrace();
  60. // never show this call.
  61. array_shift($backtrace);
  62. $i = count($backtrace);
  63. foreach ($backtrace as $trace) {
  64. $stack[] = "[#$i] {$trace['file']}:{$trace['line']}";
  65. $i--;
  66. if ($backtrace_level > 0) {
  67. if ($backtrace_level <= 1) {
  68. break;
  69. }
  70. $backtrace_level--;
  71. }
  72. }
  73. $msg .= implode("<br /> -> ", $stack);
  74. $this->logger->warn($msg);
  75. return true;
  76. }
  77. }