DbRule.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Rule for searching DB dependency
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\TestFramework\Dependency;
  9. class DbRule implements \Magento\TestFramework\Dependency\RuleInterface
  10. {
  11. /**
  12. * Map of tables and modules
  13. *
  14. * @var array
  15. */
  16. protected $_moduleTableMap;
  17. /**
  18. * Constructor
  19. *
  20. * @param array $tables
  21. */
  22. public function __construct(array $tables)
  23. {
  24. $this->_moduleTableMap = $tables;
  25. }
  26. /**
  27. * Gets alien dependencies information for current module by analyzing file's contents
  28. *
  29. * @param string $currentModule
  30. * @param string $fileType
  31. * @param string $file
  32. * @param string $contents
  33. * @return array
  34. */
  35. public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
  36. {
  37. if ('php' != $fileType || !preg_match('#.*/(Setup|Resource)/.*\.php$#', $file)) {
  38. return [];
  39. }
  40. $dependenciesInfo = [];
  41. $unKnowTables = [];
  42. if (preg_match_all('#>gettable(name)?\([\'"]([^\'"]+)[\'"]\)#i', $contents, $matches)) {
  43. $tables = array_pop($matches);
  44. foreach ($tables as $table) {
  45. if (!isset($this->_moduleTableMap[$table])) {
  46. $unKnowTables[$file][$table] = $table;
  47. continue;
  48. }
  49. if (strtolower($currentModule) !== strtolower($this->_moduleTableMap[$table])) {
  50. $dependenciesInfo[] = [
  51. 'module' => $this->_moduleTableMap[$table],
  52. 'type' => \Magento\TestFramework\Dependency\RuleInterface::TYPE_HARD,
  53. 'source' => $table,
  54. ];
  55. }
  56. }
  57. }
  58. foreach ($unKnowTables as $tables) {
  59. foreach ($tables as $table) {
  60. $dependenciesInfo[] = ['module' => 'Unknown', 'source' => $table];
  61. }
  62. }
  63. return $dependenciesInfo;
  64. }
  65. }