Skip to content

Instantly share code, notes, and snippets.

@barbazul
Last active January 3, 2016 15:19
Show Gist options
  • Save barbazul/8482036 to your computer and use it in GitHub Desktop.
Save barbazul/8482036 to your computer and use it in GitHub Desktop.
Detect conflicting rewrites in a Magento installation
<?php
function get_flag($value) {
$value = strtolower($value);
return $value === TRUE || ($value !== 'false' && !!$value);
}
$modules_dir = dir('app/etc/modules/');
$rewrites = array();
$conflicts = array();
while ($mod = $modules_dir->read()) {
if (in_array($mod, array('.', '..'))) continue;
try {
$config = new SimpleXMLElement(file_get_contents('app/etc/modules/' . $mod));
} catch (Exception $e) {
continue;
}
$modules = reset($config->modules);
foreach (get_object_vars($config->modules) as $name => $module) {
if (isset($module->active) && get_flag($module->active)) {
$pool = $module->codePool;
$configFile = 'app/code/' . $pool . '/' . implode('/', explode('_', $name, 2)) . '/etc/config.xml';
if (!file_exists($configFile)) {
continue;
}
$mod_config = new SimpleXMLElement(file_get_contents($configFile));
$types = array('models', 'blocks', 'helpers');
foreach ($types as $type) {
foreach($mod_config->xpath("//{$type}") as $type_node) {
foreach (get_object_vars($type_node) as $namespace => $data) {
foreach ($type_node->xpath($namespace . '//rewrite') as $rewrite) {
foreach (get_object_vars($rewrite) as $original => $new) {
if (empty($rewrites["{$type}/{$namespace}/{$original}"])) {
$rewrites["{$type}/{$namespace}/{$original}"] = $new;
} else {
if (empty($conflicts["{$type}/{$namespace}/{$original}"])) {
$conflicts["{$type}/{$namespace}/{$original}"] = array($rewrites["{$type}/{$namespace}/{$original}"]);
}
$conflicts["{$type}/{$namespace}/{$original}"][] = $new;
}
echo str_pad("Rewrite ({$mod}):", 44) . "{$type}/{$namespace}/$original => {$new}\n";
}
}
}
}
}
}
}
}
require_once "app/Mage.php";
if (!empty($conflicts)) {
echo "Conflicting rewrites:\n";
foreach ($conflicts as $resource => $conflictingRewrites) {
echo $resource . "\n";
foreach ($conflictingRewrites as $r) {
echo "\t* {$r}\n";
}
$resolved = TRUE;
$nConflicts = count($conflictingRewrites);
for ($i = 0; $i < $nConflicts && $resolved == TRUE; $i++) {
for ($j = 0; $j < $nConflicts - 1; $j++) {
if (is_subclass_of($conflictingRewrites[$j + 1], $conflictingRewrites[$j])) {
$temp = $conflictingRewrites[$j + 1];
$conflictingRewrites[$j + 1] = $conflictingRewrites[$j];
$conflictingRewrites[$j] = $temp;
} else {
// Verifico si el arbol de herencia esta cortado
if (!is_subclass_of($conflictingRewrites[$j], $conflictingRewrites[$j + 1])) {
$resolved = FALSE;
break;
}
}
}
}
if ($resolved) {
echo "\t--- RESOLVED ---\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment