mediasource - дополнение к staticSaver

Скрипт для разработчиков кто пользуется StaticSaver

  • скачивает и устанавливаем StaticSaver
  • создает категорию system
  • создает источники файлов для сниппетов, чанков, плагинов, шаблонов
  • для настроек StaticSaver устанавливает вновь созданные источники файлов

Использование

создать в корне файл к примеру test.php и добавить в него код. либо через консоль

если StaticSaver уже установлен то его необходимо удалить чтобы скрипт исполнился.


<?php
ini_set('display_errors', 1);
ini_set("max_execution_time", 50);
define('MODX_API_MODE', true);
require 'index.php';

$response = installPackage('StaticSaver');

$category_id = false;
$category_name = '';
if($modCategories = $modx->getObject('modCategory' , array('category' => 'system'))){
    $category_id = $modCategories->get('id');
    $category_name = $modCategories->get('category');
} else {
    $object = $modx->newObject('modCategory');
    $object->set('category','system');
    $object->save();
    $category_id = $object->get('id');
    $category_name = $object->get('category');
}

if($category_id) {

    /* @var modMediaSource $StaticTemplates */
    /* @var modMediaSource $StaticSnippets */
    /* @var modMediaSource $StaticSnippets */
    /* @var modMediaSource $StaticPlugins */
    /* @var modMediaSource $StaticChunks */

    // StaticTemplates
    if(!$StaticTemplates = $modx->getObject('modMediaSource', array('name' => 'StaticTemplates'))){
        $StaticTemplates = $modx->newObject('modMediaSource');
        $StaticTemplates->set('name','StaticTemplates');
        $StaticTemplates->set('description','статичных шаблонов');
        $StaticTemplates->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:18:"/static/templates/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticTemplates->save();
    }

    // StaticSnippets
    if(!$StaticSnippets = $modx->getObject('modMediaSource', array('name' => 'StaticSnippets'))){
        $StaticSnippets = $modx->newObject('modMediaSource');
        $StaticSnippets->set('name','StaticSnippets');
        $StaticSnippets->set('description','статичных сниппетов');
        $StaticSnippets->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:16:"static/snippets/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticSnippets->save();
    }

    // StaticPlugins
    if(!$StaticPlugins = $modx->getObject('modMediaSource', array('name' => 'StaticPlugins'))){
        $StaticPlugins = $modx->newObject('modMediaSource');
        $StaticPlugins->set('name','StaticPlugins');
        $StaticPlugins->set('description','статичных плагинов');
        $StaticPlugins->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:15:"static/plugins/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticPlugins->save();
    }
    // StaticChunks
    if(!$StaticChunks = $modx->getObject('modMediaSource', array('name' => 'StaticChunks'))){
        $StaticChunks = $modx->newObject('modMediaSource');
        $StaticChunks->set('name','StaticChunks');
        $StaticChunks->set('description','статичных чанков');
        $StaticChunks->set('properties',unserialize('a:1:{s:8:"basePath";a:6:{s:4:"name";s:8:"basePath";s:4:"desc";s:23:"prop_file.basePath_desc";s:4:"type";s:9:"textfield";s:7:"options";a:0:{}s:5:"value";s:14:"static/chunks/";s:7:"lexicon";s:11:"core:source";}}'));
        $StaticChunks->save();
    }

    $default_template = 1;
    if($modTemplate = $modx->getObject('modTemplate', array('templatename' => 'page'))){
        $default_template = $modTemplate->get('id');
    }

    // Установка настроек для StaticSaver
    $options = array(
        'staticsaver.include_category' => true,
        'staticsaver.static_chunk_file_extension' => 'tpl',
        'staticsaver.static_chunk_media_source' => $StaticChunks->get('id'),
        'staticsaver.static_plugin_file_extension' => 'php',
        'staticsaver.static_plugin_media_source' => $StaticPlugins->get('id'),
        'staticsaver.static_snippet_file_extension' => 'php',
        'staticsaver.static_snippet_media_source' => $StaticSnippets->get('id'),
        'staticsaver.static_template_file_extension' => 'tpl',
        'staticsaver.static_template_media_source' => $StaticTemplates->get('id'),
        'default_template' => $default_template, // Установка шаблона по умолчанию
        'pdotools_fenom_default' => 1,
        'pdotools_fenom_modx' => 1,
        'pdotools_fenom_parser' => 1,
        'pdotools_fenom_php' => 1,
        'cache_default' => 0,
    );

    foreach ($options as $key => $value) {
        if($tmTender = $modx->getObject('modSystemSetting', $key)){
            $tmTender->set('value',$value);
            $tmTender->save();
        }
    }

    /* @var modChunk $modChunk */
    $source_id = $StaticChunks->get('id');
    $Properties = $StaticChunks->getProperties();
    $q = $modx->newQuery('modChunk');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modChunk', $q)) {
        foreach ($objectList as $modChunk) {
            $name_chunk = $modChunk->get('name');
            $path = $category_name.'/'.$name_chunk.'.tpl';
            $modChunk->set('source', $source_id);
            $modChunk->set('static', true);
            $modChunk->set('static_file', $path);
            $modChunk->save();
        }
    }

    /* @var modSnippet $modSnippet */
    $source_id = $StaticSnippets->get('id');
    $Properties = $StaticSnippets->getProperties();
    $q = $modx->newQuery('modSnippet');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modSnippet', $q)) {
        foreach ($objectList as $modSnippet) {
            $name_chunk = $modSnippet->get('name');
            $path = $category_name.'/'.$name_chunk.'.php';
            $modSnippet->set('source', $source_id);
            $modSnippet->set('static', true);
            $modSnippet->set('static_file', $path);
            $modSnippet->save();
        }
    }

    /* @var modTemplate $modTemplate */
    $source_id = $StaticTemplates->get('id');
    $Properties = $StaticTemplates->getProperties();
    $q = $modx->newQuery('modTemplate');
    $q->where(array(
        'category' => $category_id,
        'static' => false
    ));
    if($objectList = $modx->getCollection('modSnippet', $q)) {
        foreach ($objectList as $modTemplate) {
            $name_chunk = $modTemplate->get('templatename');
            $path = $category_name.'/'.$name_chunk.'.tpl';
            $modTemplate->set('source', $source_id);
            $modTemplate->set('static', true);
            $modTemplate->set('static_file', $path);
            $modTemplate->save();
        }
    }
}

echo 'Установка завершена';
exit();

function installPackage($packageName) {
    global $modx;

    /* @var modTransportProvider $provider */
    if (!$provider = $modx->getObject('transport.modTransportProvider', array('service_url:LIKE' => '%rest.modx.com%'))) {
        $provider = $modx->getObject('transport.modTransportProvider', 1);
    }

    $modx->getVersionData();
    $productVersion = $modx->version['code_name'] . '-' . $modx->version['full_version'];

    $response = $provider->request('package', 'GET', array(
        'supports' => $productVersion,
        'query' => $packageName
    ));

    if (!empty($response)) {
        $foundPackages = simplexml_load_string($response->response);
        foreach ($foundPackages as $foundPackage) {
            /* @var modTransportPackage $foundPackage */
            if ($foundPackage->name == $packageName) {
                $sig = explode('-', $foundPackage->signature);
                $versionSignature = explode('.', $sig[1]);
                $url = $foundPackage->location;

                if (!downloadPackage($url, $modx->getOption('core_path') . 'packages/' . $foundPackage->signature . '.transport.zip')) {
                    return array(
                        'success' => 0,
                        'message' => "Could not download package <b>{$packageName}</b>.",
                    );
                }

                /* add in the package as an object so it can be upgraded */
                /** @var modTransportPackage $package */
                $package = $modx->newObject('transport.modTransportPackage');
                $package->set('signature', $foundPackage->signature);
                $package->fromArray(array(
                    'created' => date('Y-m-d h:i:s'),
                    'updated' => null,
                    'state' => 1,
                    'workspace' => 1,
                    'provider' => $provider->id,
                    'source' => $foundPackage->signature . '.transport.zip',
                    'package_name' => $packageName,
                    'version_major' => $versionSignature[0],
                    'version_minor' => !empty($versionSignature[1]) ? $versionSignature[1] : 0,
                    'version_patch' => !empty($versionSignature[2]) ? $versionSignature[2] : 0,
                ));

                if (!empty($sig[2])) {
                    $r = preg_split('/([0-9]+)/', $sig[2], -1, PREG_SPLIT_DELIM_CAPTURE);
                    if (is_array($r) && !empty($r)) {
                        $package->set('release', $r[0]);
                        $package->set('release_index', (isset($r[1]) ? $r[1] : '0'));
                    }
                    else {
                        $package->set('release', $sig[2]);
                    }
                }

                if ($package->save() && $package->install()) {
                    return array(
                        'success' => 1,
                        'message' => "<b>{$packageName}</b> was successfully installed",
                    );
                }
                else {
                    return array(
                        'success' => 0,
                        'message' => "Could not save package <b>{$packageName}</b>",
                    );
                }
                break;
            }
        }
    }
    else {
        return array(
            'success' => 0,
            'message' => "Could not find <b>{$packageName}</b> in MODX repository",
        );
    }
    return true;
}

function downloadPackage($src, $dst) {
    if (ini_get('allow_url_fopen')) {
        $file = @file_get_contents($src);
    }
    else {
        if (function_exists('curl_init')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $src);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 180);
            $safeMode = @ini_get('safe_mode');
            $openBasedir = @ini_get('open_basedir');
            if (empty($safeMode) && empty($openBasedir)) {
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            }

            $file = curl_exec($ch);
            curl_close($ch);
        }
        else {
            return false;
        }
    }
    file_put_contents($dst, $file);

    return file_exists($dst);
}
12 июня 2017, 12:48    129



Наверх