Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. <?php
  2. /* This script generates @method annotations for magento collection classes.
  3. * Example:
  4. * cd [magento root]
  5. * php shell/collections.php
  6. *
  7. */
  8. // Init framework
  9. require __DIR__.'/../app/Mage.php';
  10. Mage::app();
  11. // Factory methods to search for
  12. $methods = array(
  13. 'Mage::getModel',
  14. );
  15. // Path to search for source files
  16. $projectPath = 'app';
  17. $sourceRegex = '/^.+\.(?:php|phtml)$/';
  18. // Collect class names
  19. $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectPath, FilesystemIterator::FOLLOW_SYMLINKS));
  20. $files = new RegexIterator($iterator, $sourceRegex, RecursiveRegexIterator::GET_MATCH);
  21. $classes = $collections = [];
  22. foreach($files as $file) {
  23. $code = file_get_contents($file[0]);
  24. if ($code === FALSE) die("Could not get contents of {$file[0]}\n");
  25. foreach($methods as $method) {
  26. if(preg_match_all('#'.preg_quote($method).'\s*\(\s*[\'"]([a-zA-Z0-9/_]+)[\'"]#', $code, $matches)) {
  27. if(empty($classes[$method])) $classes[$method] = array();
  28. foreach($matches[1] as $token) {
  29. if(isset($classes[$method][$token])) continue;
  30. $className = Mage::getConfig()->getModelClassName($token);
  31. if ($className && preg_match('/^Mage_/', $className) && ! preg_match('/_(Paypal|Api|Api2|Service|Config|Calculator|Exception|Import|Export|Uploader|Install|XmlConnect|Acl)/', $className)) {
  32. $object = Mage::getModel($token);
  33. if ($object && $object instanceof Mage_Core_Model_Abstract && method_exists($object, 'getCollection')) {
  34. try {
  35. $collection = $object->getCollection();
  36. if ($collection) {
  37. $collectionClass = get_class($collection);
  38. $collections[$collectionClass] = $className;
  39. } else {
  40. $collections[$collectionClass] = 'FAILED (getCollection)';
  41. }
  42. } catch (Exception $e) {
  43. $collections[$collectionClass] = 'FAILED (exception)';
  44. }
  45. } else {
  46. $collections[$collectionClass] = 'SKIPPED (instanceof)';
  47. }
  48. $classes[$method][$token] = $className;
  49. } else {
  50. $collections[$collectionClass] = 'SKIPPED (pattern)';
  51. }
  52. }
  53. }
  54. }
  55. }
  56. foreach ($collections as $collectionClass => $className) {
  57. if (preg_match('/^(SKIPPED|FAILED)/', $className)) {
  58. echo "$className $collectionClass\n";
  59. }
  60. $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $collectionClass))).'.php';
  61. $classFilePath = stream_resolve_include_path($classFile);
  62. if ($classFilePath) {
  63. $contents = file("$classFilePath");
  64. foreach ($contents as $index => $line) {
  65. if ($line == " * @category Mage\n") {
  66. $insertAfter = $index - 1;
  67. }
  68. }
  69. if ($insertAfter) {
  70. $newLines = [
  71. " *\n",
  72. " * @method {$className}[] getIterator()\n",
  73. " * @method {$className} getFirstItem()\n",
  74. " * @method {$className}[] getItems()\n",
  75. " * @method {$className} getItemById(\$value)\n",
  76. " * @method {$className}[] getItemsByColumnValue(\$column, \$value)\n",
  77. " * @method {$className} getItemByColumnValue(\$column, \$value)\n",
  78. ];
  79. array_splice($contents, $insertAfter, 0, $newLines);
  80. file_put_contents("$classFilePath", implode("", $contents));
  81. echo "$className::$classFilePath\n";
  82. } else {
  83. echo "No insertion point found for $className::$classFilePath";
  84. }
  85. #exit; // Testing
  86. } else {
  87. echo "Could not resolve file for $className::$collectionClass\n";
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement