Skip to content

Instantly share code, notes, and snippets.

@Rud5G
Forked from colinmollenhour/makePhpstormMap.php
Created June 5, 2012 18:07
Show Gist options
  • Save Rud5G/2876622 to your computer and use it in GitHub Desktop.
Save Rud5G/2876622 to your computer and use it in GitHub Desktop.
Generate map of factory method arguments to resolved class names for Magento projects
<?php
// Init framework
require 'app/Mage.php';
Mage::app();
// Factory methods to search for
$methods = array(
'Mage::helper',
'Mage::getModel',
'Mage::getResourceModel',
'Mage::getSingleton',
'Mage::getResourceSingleton',
);
// Path to search for source files
$projectPath = 'app';
$sourceRegex = '/^.+\.(?:php|phtml)$/';
// Collect class names
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($projectPath, FilesystemIterator::FOLLOW_SYMLINKS));
$files = new RegexIterator($iterator, $sourceRegex, RecursiveRegexIterator::GET_MATCH);
$classes = array();
foreach($files as $file) {
$code = file_get_contents($file[0]) or die("Could not get contents of {$file[0]}\n");
foreach($methods as $method) {
if(preg_match_all('#'.preg_quote($method).'\s*\(\s*[\'"]([a-zA-Z0-9/_]+)[\'"]#', $code, $matches)) {
if(empty($classes[$method])) $classes[$method] = array();
foreach($matches[1] as $token) {
if(isset($classes[$method][$token])) continue;
try {
$instance = call_user_func($method, $token);
if($instance)
$classes[$method][$token] = get_class($instance);
} catch(Exception $e){}
}
}
}
}
echo json_encode($classes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment