Resolvers IX - Updating Files

Using resolvers in a transport package to update template variables.

By Bob Ray  |  August 19, 2025  |  2 min read
Resolvers IX - Updating Files

Fixing Files

Some of the earlier articles in this series had fairly complex code. I thought it would be a nice change to discuss a much simpler example. In this article, we'll look at modifying files in the update section of a resolver.

The Code

As a reminder, here's the code from the top of the resolver file that does the search-and-replace operations:

function checkContent(array $terms, string $content) {

    foreach ($terms as $term) {
        if (strpos($content, $term) !== false) {
            return true;
        }
    }

    return false;
}

$base = MODX_CORE_PATH . 'components/classextender/model/';

$prefix = $modx->getVersionData()['version'] >= 3
    ? 'MODX\Revolution\\'
    : '';

$uSearch = 'UserData';
$uReplace = 'userData';
$rSearch = 'ResourceData';
$rReplace = 'resourceData';

$searchArray = array(
    $uSearch,
    $rSearch,
);
$replaceArray = array(
    $uReplace,
    $rReplace,
);

Here's the code for correcting files that would go in the Update section of the resolver:

/* Check Files */
$files = array(
    /* User files */
    'extendeduser/metadata.mysql.php',
    'extendeduser/mysql/userdata.class.php',
    'extendeduser/mysql/userdata.map.inc.php',
    'extendeduser/userdata.class.php',
    'schema/extendeduser.mysql.schema.xml',

    /* Resource Files */
    'extendedresource/metadata.mysql.php',
    'extendedresource/mysql/resourcedata.class.php',
    'extendedresource/mysql/resourcedata.map.inc.php',
    'extendedresource/resourcedata.class.php',
    'schema/extendedresource.mysql.schema.xml',
);

$count = 0;
$fixedFileCount = 0;

foreach ($files as $file) {
    $file = $base . $file;
    if (file_exists($file)) {
        $count++;
        $content = file_get_contents($file);
        if (checkContent($searchArray, $content)) {
            $fixedFileCount++;
            $content = str_replace($searchArray, $replaceArray, $content);

            file_put_contents($file, $content);
        }
    }
}

/* Optional Report */
    $msg = 'Files found: ' . $count;
    $modx->log(modX::LOG_LEVEL_INFO, $msg);

    $msg = 'Files altered: ' . $fixedFileCount;
    $modx->log(modX::LOG_LEVEL_INFO, $msg);

In the code above, we get each file, check for the search terms, replace them if necessary, and save the file.

Coming Up

In my next article, the final one in this series, we'll move all the code from the previous articles into a single PHP class in a separate file.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.