modx - move chunks, snippets, templates to static files

initialize('mgr');

function slugify($text)
{
    $replacement = '-';
    $lowercase = true;

    $options = 'Any-Latin; Latin-ASCII; [\u0100-\u7fff] remove;';
    $text = transliterator_transliterate($options, $text);

    $parts = explode($replacement, $text);
    $replaced = array_map(function ($element) use ($replacement) {
        $element = preg_replace('/[^a-zA-Z0-9=\s—–]+/u', '', $element);
        return preg_replace('/[=\s—–]+/u', $replacement, $element);
    }, $parts);
    $string = trim(implode($replacement, $replaced), $replacement);
    return $lowercase ? strtolower($string) : $string;
}

function setConfig($var, $value)
{
    global $modx;

    $setting = $modx->getObject('modSystemSetting', $var);

    if ($setting == null) {
        $setting = $modx->newObject('modSystemSetting');
    }

    $setting->set('value', $value);
    $setting->save();
}

function handle_objects($type, $objects)
{
    global $modx;

    $fileType = 'html';
    if ($type == 'snippets') {
        $fileType = 'php';
    } else if ($fileType == 'templates') {
        $fileType = 'tpl';
    }

    foreach ($objects as $one) {

        $content = $one->getContent();

        if ($type == 'templates') {
            $name = slugify($one->get('templatename'));
        } else {
            $name = $one->get('name');
        }

        $category = "SELECT category FROM `categories` WHERE id = " . $one->get('category') . "";
        $query = $modx->query($category);
        $aCategory = $query->fetch(PDO::FETCH_ASSOC);

        $directory = __DIR__ . "/assets/{$type}/{$aCategory['category']}";
        $file = $directory . '/' . $name . '.' . $fileType;

        $static_file = "assets/{$type}/{$aCategory['category']}/{$name}.".$fileType;
        $static_file = preg_replace('#/+#', '/', $static_file);

        mkdir($directory, 0755, true);
        file_put_contents($file, $content);

        echo "$static_file\n";

        $one->source = 1;
        $one->static = 1;
        $one->static_file = $static_file;
        $one->save();
    }
}

if ($argv[1] == 'movestatic') {
    $chunks = $modx->getCollection('modChunk');
    $snippets = $modx->getCollection('modSnippet');
    $templates = $modx->getCollection('modTemplate');

    handle_objects('chunks', $chunks);
    handle_objects('snippets', $snippets);
    handle_objects('templates', $templates);
} elseif ('cache') {
//    $cache = $modx->getOption('cache_scripts');
    $setting = $modx->getObject('modSystemSetting', 'cache_scripts');

    echo "cache: $cache\n";

    if ($setting->value == 1) {

        setConfig('cache_default', 0);
        setConfig('cache_db', 0);
        setConfig('cache_scripts', 0);
        setConfig('cache_resource', 0);
        setConfig('cache_disabled', 1);
        echo "cache disabled\n";

    } else {
        setConfig('cache_default', 1);
        setConfig('cache_db', 1);
        setConfig('cache_scripts', 1);
        setConfig('cache_resource', 1);
        setConfig('cache_disabled', 0);
        echo "cache enabled\n";
    }

} else {
    echo "enter command pls\n";
}