Base for a static organization website

IncludePanel.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. App::uses('DebugPanel', 'DebugKit.Lib');
  14. /**
  15. * Provides a list of included files for the current request
  16. *
  17. */
  18. class IncludePanel extends DebugPanel {
  19. /**
  20. * The list of plugins within the application
  21. *
  22. * @var <type>
  23. */
  24. protected $_pluginPaths = array();
  25. /**
  26. * File Types
  27. *
  28. * @var array
  29. */
  30. protected $_fileTypes = array(
  31. 'Cache', 'Config', 'Configure', 'Console', 'Component', 'Controller',
  32. 'Behavior', 'Datasource', 'Model', 'Plugin', 'Test', 'View', 'Utility',
  33. 'Network', 'Routing', 'I18n', 'Log', 'Error'
  34. );
  35. /**
  36. * Get a list of plugins on construct for later use
  37. */
  38. public function __construct() {
  39. foreach (CakePlugin::loaded() as $plugin) {
  40. $this->_pluginPaths[$plugin] = CakePlugin::path($plugin);
  41. }
  42. parent::__construct();
  43. }
  44. /**
  45. * Get a list of files that were included and split them out into the various parts of the app
  46. *
  47. * @param Controller $controller
  48. * @return array
  49. */
  50. public function beforeRender(Controller $controller) {
  51. $return = array('core' => array(), 'app' => array(), 'plugins' => array());
  52. foreach (get_included_files() as $file) {
  53. $pluginName = $this->_isPluginFile($file);
  54. if ($pluginName) {
  55. $return['plugins'][$pluginName][$this->_getFileType($file)][] = $this->_niceFileName($file, $pluginName);
  56. } elseif ($this->_isAppFile($file)) {
  57. $return['app'][$this->_getFileType($file)][] = $this->_niceFileName($file, 'app');
  58. } elseif ($this->_isCoreFile($file)) {
  59. $return['core'][$this->_getFileType($file)][] = $this->_niceFileName($file, 'core');
  60. }
  61. }
  62. $return['paths'] = $this->_includePaths();
  63. ksort($return['core']);
  64. ksort($return['plugins']);
  65. ksort($return['app']);
  66. return $return;
  67. }
  68. /**
  69. * Get the possible include paths
  70. * @return array
  71. */
  72. protected function _includePaths() {
  73. $paths = array_flip(array_merge(explode(PATH_SEPARATOR, get_include_path()), array(CAKE)));
  74. unset($paths['.']);
  75. return array_flip($paths);
  76. }
  77. /**
  78. * Check if a path is part of cake core
  79. * @param string $file
  80. * @return boolean
  81. */
  82. protected function _isCoreFile($file) {
  83. return strstr($file, CAKE);
  84. }
  85. /**
  86. * Check if a path is from APP but not a plugin
  87. * @param string $file
  88. * @return boolean
  89. */
  90. protected function _isAppFile($file) {
  91. return strstr($file, APP);
  92. }
  93. /**
  94. * Check if a path is from a plugin
  95. * @param string $file
  96. * @return boolean
  97. */
  98. protected function _isPluginFile($file) {
  99. foreach ($this->_pluginPaths as $plugin => $path) {
  100. if (strstr($file, $path)) {
  101. return $plugin;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Replace the path with APP, CORE or the plugin name
  108. * @param string $file
  109. * @param string
  110. * - app for app files
  111. * - core for core files
  112. * - PluginName for the name of a plugin
  113. * @return boolean
  114. */
  115. protected function _niceFileName($file, $type) {
  116. switch ($type) {
  117. case 'app':
  118. return str_replace(APP, 'APP/', $file);
  119. case 'core':
  120. return str_replace(CAKE, 'CORE/', $file);
  121. default:
  122. return str_replace($this->_pluginPaths[$type], $type . '/', $file);
  123. }
  124. }
  125. /**
  126. * Get the type of file (model, controller etc)
  127. * @param string $file
  128. * @return string
  129. */
  130. protected function _getFileType($file) {
  131. foreach ($this->_fileTypes as $type) {
  132. if (stripos($file, '/' . $type . '/') !== false) {
  133. return $type;
  134. }
  135. }
  136. return 'Other';
  137. }
  138. }