PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wire/core/WireDebugInfo.php

http://github.com/ryancramerdesign/ProcessWire
PHP | 158 lines | 111 code | 16 blank | 31 comment | 33 complexity | 415c6fd4ceb251103950ebc11c70e87a MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Helper class for PHP 5.6+ __debugInfo() methods in Wire classes
  4. *
  5. * Class WireDebugInfo
  6. *
  7. * ProcessWire 2.x
  8. * Copyright 2015 by Ryan Cramer
  9. * This file licensed under Mozilla Public License v2.0 http://mozilla.org/MPL/2.0/
  10. *
  11. */
  12. class WireDebugInfo extends Wire {
  13. /**
  14. * Get all debug info for the given Wire object
  15. *
  16. * @param Wire $obj
  17. * @return array
  18. *
  19. */
  20. public function getDebugInfo(Wire $obj) {
  21. $className = $obj->className();
  22. $info = array();
  23. if(method_exists($this, $className)) {
  24. $info = array_merge($info, $this->$className($obj));
  25. }
  26. $changes = $obj->getChanges();
  27. if(count($changes)) $info['changes'] = $changes;
  28. $hooks = $this->getHooksInfo($obj);
  29. if(count($hooks)) $info['hooks'] = $hooks;
  30. return $info;
  31. }
  32. /**
  33. * Get hooks debug info for the given Wire object
  34. *
  35. * @param Wire $obj
  36. * @return array
  37. *
  38. */
  39. public function getHooksInfo(Wire $obj) {
  40. $hooks = array();
  41. foreach($obj->getHooks() as $hook) {
  42. list($class, $priority) = explode(':', $hook['id']);
  43. $key = '';
  44. $value = '';
  45. if($hook['options']['before']) $key .= "before ";
  46. if($hook['options']['type'] == 'property') {
  47. $key .= "property ";
  48. } else if($hook['options']['after']) {
  49. if(method_exists($class, $hook['method']) || method_exists($class, '___' . $hook['method'])) {
  50. $key .= "after ";
  51. }
  52. }
  53. if($hook['options']['type'] == 'property' || !$hook['options']['allInstances']) {
  54. $key .= "$class" . '->' . "$hook[method]";
  55. } else {
  56. $key .= "$class::$hook[method]";
  57. }
  58. $filename = '';
  59. if(!empty($hook['toObject'])) {
  60. $value .= $hook['toObject']->className() . "->";
  61. $ref = new ReflectionClass($hook['toObject']);
  62. $filename = $ref->getFileName();
  63. }
  64. if(!empty($hook['toMethod'])) {
  65. if(is_string($hook['toMethod'])) {
  66. $value .= "$hook[toMethod]()";
  67. } else if(is_callable($hook['toMethod'])) {
  68. $ref = new ReflectionFunction($hook['toMethod']);
  69. $filename = $ref->getFileName();
  70. $value = "anonymous function()";
  71. }
  72. }
  73. if($filename) $value .= " in " . basename($filename);
  74. if(!isset($hooks[$key])) {
  75. $hooks[$key] = $value;
  76. } else {
  77. if(!is_array($hooks[$key])) $hooks[$key] = array($hooks[$key]);
  78. $hooks[$key][] = $value;
  79. }
  80. }
  81. return $hooks;
  82. }
  83. /**
  84. * Debug info specific to Page objects
  85. *
  86. * @param Page $page
  87. * @return array
  88. *
  89. */
  90. protected function Page(Page $page) {
  91. $info = array(
  92. 'instanceID' => $page->instanceID,
  93. 'id' => $page->id,
  94. 'name' => $page->name,
  95. 'namePrevious' => '',
  96. 'path' => $page->path(),
  97. 'status' => implode(', ', $page->status(true)),
  98. 'statusPrevious' => 0,
  99. 'template' => $page->template ? $page->template->name : '',
  100. 'templatePrevious' => '',
  101. 'parent' => $page->parent ? $page->parent->path : '',
  102. 'parentPrevious' => '',
  103. 'numChildren' => $page->numChildren(),
  104. 'sort' => $page->sort,
  105. 'sortfield' => $page->sortfield,
  106. 'created' => $page->created,
  107. 'modified' => $page->modified,
  108. 'published' => $page->published,
  109. 'createdUser' => $page->createdUser ? $page->createdUser->name : $page->created_users_id,
  110. 'modifiedUser' => $page->modifiedUser ? $page->modifiedUser->name : $page->modified_users_id,
  111. );
  112. if($page->namePrevious) {
  113. $info['namePrevious'] = $page->namePrevious;
  114. } else {
  115. unset($info['namePrevious']);
  116. }
  117. if($page->statusPrevious !== null) {
  118. $info['statusPrevious'] = implode(', ', $page->status(true, $page->statusPrevious));
  119. } else {
  120. unset($info['statusPrevious']);
  121. }
  122. if($page->templatePrevious) {
  123. $info['templatePrevious'] = $page->templatePrevious->name;
  124. } else {
  125. unset($info['templatePrevious']);
  126. }
  127. if($page->parentPrevious) {
  128. $info['parentPrevious'] = $page->parentPrevious->path();
  129. } else {
  130. unset($info['parentPrevious']);
  131. }
  132. if($page->isNew) $info['isNew'] = 1;
  133. $info['isLoaded'] = (int) $page->isLoaded();
  134. $info['outputFormatting'] = (int) $page->outputFormatting();
  135. if($page->quietMode) $info['quietMode'] = 1;
  136. foreach(array('created', 'modified', 'published') as $key) {
  137. $info[$key] = wireDate($this->wire('config')->dateFormat, $info[$key]) . " " .
  138. "(" . wireDate('relative', $info[$key]) . ")";
  139. }
  140. return $info;
  141. }
  142. }