PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/static/testsuite/Utility/Files.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 384 lines | 218 code | 22 blank | 144 comment | 20 complexity | f55c0e1522b09505b7225382ec89d2b7 MD5 | raw file
  1. <?php
  2. /**
  3. * A helper to gather specific kinds if files in Magento application
  4. *
  5. * Magento
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * This source file is subject to the Open Software License (OSL 3.0)
  10. * that is bundled with this package in the file LICENSE.txt.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://opensource.org/licenses/osl-3.0.php
  13. * If you did not receive a copy of the license and are unable to
  14. * obtain it through the world-wide-web, please send an email
  15. * to license@magentocommerce.com so we can send you a copy immediately.
  16. *
  17. * DISCLAIMER
  18. *
  19. * Do not edit or add to this file if you wish to upgrade Magento to newer
  20. * versions in the future. If you wish to customize Magento for your
  21. * needs please refer to http://www.magentocommerce.com for more information.
  22. *
  23. * @category tests
  24. * @package static
  25. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. */
  28. class Utility_Files
  29. {
  30. /**
  31. * @var Utility_Files
  32. */
  33. protected static $_instance = null;
  34. /**
  35. * In-memory cache for the data sets
  36. *
  37. * @var array
  38. */
  39. protected static $_cache = array();
  40. /**
  41. * @var string
  42. */
  43. protected $_path = '';
  44. /**
  45. * Setter/Getter for an instance of self
  46. *
  47. * @param Utility_Files $instance
  48. * @return Utility_Files
  49. * @throws Exception when there is no instance set
  50. */
  51. public static function init(Utility_Files $instance = null)
  52. {
  53. if ($instance) {
  54. self::$_instance = $instance;
  55. }
  56. if (!self::$_instance) {
  57. throw new Exception('Instance is not set yet.');
  58. }
  59. return self::$_instance;
  60. }
  61. /**
  62. * Compose PHPUnit's data sets that contain each file as the first argument
  63. *
  64. * @param array $files
  65. * @return array
  66. */
  67. public static function composeDataSets(array $files)
  68. {
  69. $result = array();
  70. foreach ($files as $file) {
  71. /* Use filename as a data set name to not include it to every assertion message */
  72. $result[$file] = array($file);
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Set path to source code
  78. *
  79. * @param string $pathToSource
  80. */
  81. public function __construct($pathToSource)
  82. {
  83. $this->_path = $pathToSource;
  84. }
  85. /**
  86. * Getter for _path
  87. *
  88. * @return string
  89. */
  90. public function getPathToSource()
  91. {
  92. return $this->_path;
  93. }
  94. /**
  95. * Returns array of PHP-files, that use or declare Magento application classes and Magento libs
  96. *
  97. * @param bool $appCode application PHP-code
  98. * @param bool $otherCode non-application PHP-code (doesn't include "dev" directory)
  99. * @param bool $templates application PHTML-code
  100. * @param bool $asDataSet
  101. * @return array
  102. */
  103. public function getPhpFiles($appCode = true, $otherCode = true, $templates = true, $asDataSet = true)
  104. {
  105. $key = __METHOD__ . "/{$this->_path}/{$appCode}/{$otherCode}/{$templates}";
  106. if (!isset(self::$_cache[$key])) {
  107. $pool = $namespace = $module = $area = $package = $theme = '*';
  108. $files = array();
  109. if ($appCode) {
  110. $files = array_merge(
  111. glob($this->_path . '/app/*.php', GLOB_NOSORT),
  112. self::_getFiles(array("{$this->_path}/app/code/{$pool}/{$namespace}/{$module}"), '*.php')
  113. );
  114. }
  115. if ($otherCode) {
  116. $files = array_merge($files,
  117. glob($this->_path . '/*.php', GLOB_NOSORT),
  118. glob($this->_path . '/pub/*.php', GLOB_NOSORT),
  119. self::_getFiles(array("{$this->_path}/downloader"), '*.php'),
  120. self::_getFiles(array("{$this->_path}/lib/{Mage,Magento,Varien}"), '*.php')
  121. );
  122. }
  123. if ($templates) {
  124. $files = array_merge($files,
  125. self::_getFiles(array("{$this->_path}/app/code/{$pool}/{$namespace}/{$module}"), '*.phtml'),
  126. self::_getFiles(
  127. array("{$this->_path}/app/design/{$area}/{$package}/{$theme}/{$namespace}_{$module}"), '*.phtml'
  128. )
  129. );
  130. }
  131. self::$_cache[$key] = $files;
  132. }
  133. if ($asDataSet) {
  134. return self::composeDataSets(self::$_cache[$key]);
  135. }
  136. return self::$_cache[$key];
  137. }
  138. /**
  139. * Returns list of xml files, used by Magento application
  140. *
  141. * @return array
  142. */
  143. public function getXmlFiles()
  144. {
  145. return array_merge(
  146. self::getConfigFiles(),
  147. self::getLayoutFiles()
  148. );
  149. }
  150. /**
  151. * Returns list of configuration files, used by Magento application
  152. *
  153. * @param string $fileNamePattern
  154. * @param array $excludedFileNames
  155. * @param bool $asDataSet
  156. * @return array
  157. */
  158. public function getConfigFiles(
  159. $fileNamePattern = '*.xml', $excludedFileNames = array('wsdl.xml', 'wsdl2.xml', 'wsi.xml'), $asDataSet = true
  160. ) {
  161. $cacheKey = __METHOD__ . '|' . $this->_path . '|' . serialize(func_get_args());
  162. if (!isset(self::$_cache[$cacheKey])) {
  163. $files = glob($this->_path . "/app/code/*/*/*/etc/$fileNamePattern", GLOB_NOSORT | GLOB_BRACE);
  164. $files = array_filter($files, function ($file) use ($excludedFileNames) {
  165. return !in_array(basename($file), $excludedFileNames);
  166. });
  167. self::$_cache[$cacheKey] = $files;
  168. }
  169. if ($asDataSet) {
  170. return self::composeDataSets(self::$_cache[$cacheKey]);
  171. }
  172. return self::$_cache[$cacheKey];
  173. }
  174. /**
  175. * Returns list of layout files, used by Magento application modules
  176. *
  177. * An incoming array can contain the following items
  178. * array (
  179. * 'pool' => 'pool_name',
  180. * 'namespace' => 'namespace_name',
  181. * 'module' => 'module_name',
  182. * 'area' => 'area_name',
  183. * 'package' => 'package_name',
  184. * 'theme' => 'theme_name',
  185. * 'include_code' => true|false,
  186. * 'include_design' => true|false,
  187. * )
  188. *
  189. * @param array $incomingParams
  190. * @param bool $asDataSet
  191. * @return array
  192. */
  193. public function getLayoutFiles($incomingParams = array(), $asDataSet = true)
  194. {
  195. $params = array(
  196. 'pool' => '*',
  197. 'namespace' => '*',
  198. 'module' => '*',
  199. 'area' => '*',
  200. 'package' => '*',
  201. 'theme' => '*',
  202. 'include_code' => true,
  203. 'include_design' => true
  204. );
  205. foreach (array_keys($params) as $key) {
  206. if (isset($incomingParams[$key])) {
  207. $params[$key] = $incomingParams[$key];
  208. }
  209. }
  210. $cacheKey = md5($this->_path . '|' . implode('|', $params));
  211. if (!isset(self::$_cache[__METHOD__][$cacheKey])) {
  212. $files = array();
  213. if ($params['include_code']) {
  214. $files = self::_getFiles(
  215. array("{$this->_path}/app/code/{$params['pool']}/{$params['namespace']}/{$params['module']}"
  216. . "/view/{$params['area']}"),
  217. '*.xml'
  218. );
  219. }
  220. if ($params['include_design']) {
  221. $files = array_merge(
  222. $files,
  223. self::_getFiles(
  224. array("{$this->_path}/app/design/{$params['area']}/{$params['package']}/{$params['theme']}"
  225. . "/{$params['namespace']}_{$params['module']}"),
  226. '*.xml'
  227. ),
  228. glob(
  229. "{$this->_path}/app/design/{$params['area']}/{$params['package']}/{$params['theme']}/local.xml",
  230. GLOB_NOSORT
  231. )
  232. );
  233. }
  234. self::$_cache[__METHOD__][$cacheKey] = $files;
  235. }
  236. if ($asDataSet) {
  237. return self::composeDataSets(self::$_cache[__METHOD__][$cacheKey]);
  238. }
  239. return self::$_cache[__METHOD__][$cacheKey];
  240. }
  241. /**
  242. * Returns list of Javascript files in Magento
  243. *
  244. * @return array
  245. */
  246. public function getJsFiles()
  247. {
  248. $key = __METHOD__ . $this->_path;
  249. if (isset(self::$_cache[$key])) {
  250. return self::$_cache[$key];
  251. }
  252. $pool = $namespace = $module = $area = $package = $theme = $skin = '*';
  253. $files = self::_getFiles(
  254. array(
  255. "{$this->_path}/app/code/{$pool}/{$namespace}/{$module}/view/{$area}",
  256. "{$this->_path}/app/design/{$area}/{$package}/{$theme}/skin/{$skin}",
  257. "{$this->_path}/pub/lib/{mage,varien}"
  258. ),
  259. '*.js'
  260. );
  261. $result = self::composeDataSets($files);
  262. self::$_cache[$key] = $result;
  263. return $result;
  264. }
  265. /**
  266. * Returns list of email template files
  267. *
  268. * @return array
  269. */
  270. public function getEmailTemplates()
  271. {
  272. $key = __METHOD__ . $this->_path;
  273. if (isset(self::$_cache[$key])) {
  274. return self::$_cache[$key];
  275. }
  276. $files = self::_getFiles(array($this->_path . '/app/code/*/*/*/view/email'), '*.html');
  277. $result = self::composeDataSets($files);
  278. self::$_cache[$key] = $result;
  279. return $result;
  280. }
  281. /**
  282. * Return list of all files. The list excludes tool-specific files
  283. * (e.g. Git, IDE) or temp files (e.g. in "var/").
  284. *
  285. * @return array
  286. */
  287. public function getAllFiles()
  288. {
  289. $key = __METHOD__ . $this->_path;
  290. if (isset(self::$_cache[$key])) {
  291. return self::$_cache[$key];
  292. }
  293. $subFiles = self::_getFiles(
  294. array(
  295. $this->_path . '/app',
  296. $this->_path . '/dev',
  297. $this->_path . '/downloader',
  298. $this->_path . '/lib',
  299. $this->_path . '/pub',
  300. ),
  301. '*'
  302. );
  303. $rootFiles = glob($this->_path . '/*', GLOB_NOSORT);
  304. $rootFiles = array_filter(
  305. $rootFiles,
  306. function ($file) {
  307. return is_file($file);
  308. }
  309. );
  310. $result = array_merge($rootFiles, $subFiles);
  311. $result = self::composeDataSets($result);
  312. self::$_cache[$key] = $result;
  313. return $result;
  314. }
  315. /**
  316. * Retrieve all files in folders and sub-folders that match pattern (glob syntax)
  317. *
  318. * @param array $dirPatterns
  319. * @param string $fileNamePattern
  320. * @return array
  321. */
  322. protected static function _getFiles(array $dirPatterns, $fileNamePattern)
  323. {
  324. $result = array();
  325. foreach ($dirPatterns as $oneDirPattern) {
  326. $entriesInDir = glob("$oneDirPattern/$fileNamePattern", GLOB_NOSORT | GLOB_BRACE);
  327. $subDirs = glob("$oneDirPattern/*", GLOB_ONLYDIR | GLOB_NOSORT | GLOB_BRACE);
  328. $filesInDir = array_diff($entriesInDir, $subDirs);
  329. $filesInSubDir = self::_getFiles($subDirs, $fileNamePattern);
  330. $result = array_merge($result, $filesInDir, $filesInSubDir);
  331. }
  332. return $result;
  333. }
  334. /**
  335. * Check if specified class exists within code pools
  336. *
  337. * @param string $class
  338. * @param string &$path
  339. * @return bool
  340. */
  341. public function codePoolClassFileExists($class, &$path = '')
  342. {
  343. $path = implode(DIRECTORY_SEPARATOR, explode('_', $class)) . '.php';
  344. $directories = array('/app/code/core/', '/app/code/community/', '/app/code/local/', '/lib/');
  345. foreach ($directories as $dir) {
  346. $fullPath = str_replace('/', DIRECTORY_SEPARATOR, $this->_path . $dir . $path);
  347. /**
  348. * Use realpath() instead of file_exists() to avoid incorrect work on Windows because of case insensitivity
  349. * of file names
  350. */
  351. if (realpath($fullPath) == $fullPath) {
  352. $fileContent = file_get_contents($fullPath);
  353. if (strpos($fileContent, 'class ' . $class) !== false) {
  354. return true;
  355. }
  356. }
  357. }
  358. return false;
  359. }
  360. }