Esempio n. 1
0
function smarty_function_linkup_js($params, Smarty_Internal_Template &$smarty)
{
    $params = ArrayAdapter::inst($params);
    $dir = $params->str('dir');
    $name = $params->str('name');
    $di = DirManager::resources('scripts')->getDirItem($dir, $name, 'js');
    echo $di->isFile() ? PsHtml::linkJs($di) : '';
}
Esempio n. 2
0
function smarty_function_linkup_css($params, Smarty_Internal_Template &$smarty)
{
    $params = ArrayAdapter::inst($params);
    $dir = $params->str('dir');
    $name = $params->str('name');
    $media = $params->str('media');
    $di = DirManager::resources('css')->getDirItem($dir, $name, 'css');
    echo $di->isFile() ? PsHtml::linkCss($di, $media) : '';
}
 /**
  * Метод создаёт новый фолдинг
  */
 public function makeNewFolding(ArrayAdapter $adapter)
 {
     $ifaces = $adapter->arr('ifaces');
     $rtypes = $adapter->arr('rtypes');
     $type = lowertrim($adapter->str('FoldingType'));
     $subtype = lowertrim($adapter->str('FoldingSubType'));
     $group = $adapter->str('FoldingGroup');
     $classPrefix = $adapter->str('FoldingClassPrefix');
     check_condition($type, 'Не передан тип фолдинга');
     check_condition($group, 'Не передана группа для фолдинга');
     check_condition($classPrefix, 'Не передан префикс для классов фолдинга');
     //TODO - определять директорию для воздания менеджера фолдингов.
     die('Folding dir is not deffined.');
     $classesDi = DirItem::inst(array('sdk/kit', 'folded'), $group);
     check_condition(!$classesDi->isDir(), "Директория {$classesDi} уже существует");
     check_condition(!$classesDi->isFile(), "Элемент {$classesDi} не может быть файлом");
     $rtypes = array_intersect(array_keys(PsUtil::getClassConsts('FoldedResources', 'RTYPE_')), $rtypes);
     $rtypesArr = trim(implode(', self::', $rtypes));
     $rtypesArr = $rtypesArr ? "self::{$rtypesArr}" : '';
     $hasPhp = in_array('RTYPE_PHP', $rtypes);
     $classesDm = DirManager::inst($classesDi->getRelPath())->makePath();
     $resourcesPatternDmTo = DirManager::resources(array('folded', $group, FoldedResources::PATTERN_NAME));
     check_condition(!$resourcesPatternDmTo->isDir(), "Целевая директория {$resourcesPatternDmTo} существует");
     $resourcesPatternDmFrom = DirManager::resources(array('folded', FoldedResources::PATTERN_NAME));
     check_condition($resourcesPatternDmFrom->isDir(), "Некорректна директория-источник {$resourcesPatternDmFrom}");
     $interfaces = array();
     foreach ($this->getFoldedInterfaces($ifaces) as $name => $ifaceClass) {
         $ctt = $ifaceClass->getClassBody();
         if ($ctt) {
             $interfaces[] = "/****************\n\t * {$name}\n\t ****************/\n" . $ctt;
         }
     }
     $smParams = $adapter->getData();
     $smParams['rtypes'] = $rtypesArr;
     $smParams['funique'] = FoldedResources::unique($type, $subtype);
     $smParams['BaseClass'] = 'Base' . $classPrefix;
     $smParams['ManagerClass'] = $classPrefix . 'Manager';
     $smParams['ResourcesClass'] = $classPrefix . 'Resources';
     $smParams['implements'] = $ifaces ? 'implements ' . implode(', ', $ifaces) . ' ' : '';
     $smParams['interfaces'] = $interfaces ? implode("\n\n\t", $interfaces) : '';
     try {
         //КЛАССЫ
         //Resources
         $tpl = PSSmarty::template(DirItem::inst(array(__DIR__, 'tpls'), 'resources.tpl'), $smParams);
         $di = $classesDm->getDirItem(null, $smParams['ResourcesClass'], 'php');
         $di->writeToFile(trim($tpl->fetch()), true);
         //Manager
         $tpl = PSSmarty::template(DirItem::inst(array(__DIR__, 'tpls'), 'manager.tpl'), $smParams);
         $di = $classesDm->getDirItem(null, $smParams['ManagerClass'], 'php');
         $di->writeToFile(trim($tpl->fetch()), true);
         if ($hasPhp) {
             //BaseClass
             $tpl = PSSmarty::template(DirItem::inst(array(__DIR__, 'tpls'), 'baseclass.tpl'), $smParams);
             $di = $classesDm->getDirItem(null, $smParams['BaseClass'], 'php');
             $di->writeToFile(trim($tpl->fetch()), true);
         }
         //ШАБЛОН ДЛЯ СУЩНОСТЕЙ
         $resourcesPatternDmTo->makePath();
         foreach ($rtypes as $rtype) {
             $ext = FoldedResources::resourceTypeToExt(PsUtil::newReflectionClass('FoldedResources')->getConstant($rtype));
             $diTo = $resourcesPatternDmTo->getDirItem(null, FoldedResources::PATTERN_NAME, $ext);
             $diFrom = $resourcesPatternDmFrom->getDirItem(null, FoldedResources::PATTERN_NAME, $ext);
             if ($ext == PsConst::EXT_PHP) {
                 $diTo->writeToFile(str_replace('eclassnamebase', $smParams['BaseClass'], $diFrom->getFileContents()));
                 continue;
             }
             $diFrom->copyTo($diTo);
         }
     } catch (Exception $ex) {
         $classesDm->clearDir(null, true);
         $resourcesPatternDmTo->clearDir(null, true);
         throw $ex;
     }
 }
Esempio n. 4
0
 public function buildContent()
 {
     $res = AdminPostsProcessor::inst()->saveCommentsFormules2Zip(DirManager::resources('sprites')->getDirItem());
     echo $this->getFoldedEntity()->fetchTpl(array('data' => $res));
 }
Esempio n. 5
0
 protected function doFinalize($html)
 {
     /*
      * ИНИЦИАЛИЗАЦИЯ
      */
     $this->OBFUSCATABLE[] = DirManager::resources()->relDirPath('folded');
     $this->OBFUSCATABLE[] = DirManager::resources()->relDirPath('scripts/ps');
     //Расширим для предотвращения PREG_BACKTRACK_LIMIT_ERROR
     ini_set('pcre.backtrack_limit', 10 * 1000 * 1000);
     /*
      * НАЧАЛО РЫБОТЫ
      */
     /* Удалим комментарии */
     $pattern = "/<!--(.*?)-->/si";
     $html = preg_replace($pattern, '', $html);
     $resources = '';
     /*
      * JAVASCRIPT
      * <script ...>...</script>
      */
     $linked = array();
     $matches = array();
     $pattern = "/<script ([^>]*)>(.*?)<\\/script>/si";
     $cnt = preg_match_all($pattern, $html, $matches, PREG_PATTERN_ORDER);
     $attributes = $matches[1];
     $contents = $matches[2];
     for ($index = 0; $index < $cnt; $index++) {
         $attrs = $this->parseAttributesString($attributes[$index]);
         $content = trim($contents[$index]);
         if ($content) {
             //Есть содержимое, этот тег включаем сразу
             $tmp = PsHtml::linkJs(null, $content, $attrs);
             $resources .= $tmp . "\n";
             $this->LOGGER->info($tmp);
             continue;
         }
         $src = array_get_value('src', $attrs);
         if (!$src || in_array($src, $linked)) {
             //Нет пути или путь уже включён - пропускаем
             continue;
         }
         $linked[] = $src;
         $newSrc = $this->tryReplaceResource($src);
         if (!$newSrc) {
             if ($this->LOGGER->isEnabled()) {
                 $tmp = PsHtml::linkJs(null, null, $attrs);
                 $this->LOGGER->info($tmp . '  [EXCLUDED]');
             }
             continue;
         }
         $replaced = $src != $newSrc;
         if ($replaced) {
             $linked[] = $newSrc;
             $attrs['src'] = $newSrc;
         }
         $tmp = PsHtml::linkJs(null, null, $attrs);
         $resources .= $tmp . "\n";
         if ($this->LOGGER->isEnabled()) {
             $attrs['src'] = $src;
             $this->LOGGER->info(($replaced ? PsHtml::linkJs(null, null, $attrs) . '  [REPLACED]  ' : '') . $tmp);
         }
     }
     $html = preg_replace($pattern, '', $html);
     /*
      * FAVICON, CSS
      * <link .../>
      */
     /* Вырежем css и другие "линки" */
     $matches = array();
     $pattern = "/<link ([^>]*)\\/>/si";
     $cnt = preg_match_all($pattern, $html, $matches, PREG_PATTERN_ORDER);
     $attributes = $matches[1];
     for ($index = 0; $index < $cnt; $index++) {
         $attrs = $this->parseAttributesString($attributes[$index]);
         $src = array_get_value('href', $attrs);
         if (!$src || in_array($src, $linked)) {
             //Нет пути или путь уже включён - пропускаем
             continue;
         }
         $linked[] = $src;
         $newSrc = $this->tryReplaceResource($src);
         if (!$newSrc) {
             if ($this->LOGGER->isEnabled()) {
                 $tmp = PsHtml::linkLink($attrs);
                 $this->LOGGER->info($tmp . '  [EXCLUDED]');
             }
             continue;
         }
         $replaced = $src != $newSrc;
         if ($replaced) {
             $linked[] = $newSrc;
             $attrs['href'] = $newSrc;
         }
         $tmp = PsHtml::linkLink($attrs);
         $resources .= $tmp . "\n";
         if ($this->LOGGER->isEnabled()) {
             $attrs['href'] = $src;
             $this->LOGGER->info(($replaced ? PsHtml::linkLink($attrs) . '  [REPLACED]  ' : '') . $tmp);
         }
     }
     $html = preg_replace($pattern, '', $html);
     $resources = "\n" . trim($resources) . "\n";
     /*
      * Удалим пробелы
      */
     $matches = array();
     $cnt = preg_match("/<head>(.*?)<\\/head>/si", $html, $matches);
     if ($cnt == 1) {
         /**
          * $headOld - ресурсы страницы, находящиеся в блоке <head ...>...</head> и 
          * оставшиеся после вырезания .js и .css. Обычно там остаётся два блока:
          * <meta...>...</meta> и <title>...</title>
          */
         $headOld = $matches[1];
         $headNew = normalize_string($headOld);
         $headNew = "{$headNew} {$resources}";
         $html = str_replace_first($headOld, $headNew, $html);
     } else {
         //Вставляем ресурсы в <head>
         $html = str_replace_first('</head>', $resources . '</head>', $html);
     }
     $this->LOGGER->infoBox('PAGE FINALISED', $html);
     return $html;
 }
Esempio n. 6
0
function executeProcess(array $argv)
{
    $CLASS_PATTERN = file_get_contents(file_path(__DIR__, 'class.txt'));
    $METHOD_PATTERN = file_get_contents(file_path(__DIR__, 'method.txt'));
    /*
     * Название тестового файла.
     * Для проверки мы сначала удалим класс для него, если он был, потом проверим, что за класс был сгенерирован.
     */
    $TEST_MESSAGES_FILE = 'ExampleSdkProcessMessages';
    //Убедимся, что тестовый .msgs существует и удалим тестовый .php файл
    $TEST_PHP_DI = DirItem::inst(__DIR__ . '/classes', $TEST_MESSAGES_FILE, PsConst::EXT_PHP)->remove();
    $TEST_MSGS_DI = DirItem::inst(__DIR__ . '/classes', $TEST_MESSAGES_FILE, PsConst::EXT_MSGS);
    check_condition($TEST_MSGS_DI->isFile(), "File {$TEST_MSGS_DI->getAbsPath()} must exists");
    dolog('Loading all files, ended with Messages.msgs');
    //TODO - после нужно получать эту информацию из какого-нибудь места
    $exceptDirs[] = DirManager::autogen()->relDirPath();
    $exceptDirs[] = DirManager::resources()->relDirPath();
    $exceptDirs[] = DirManager::stuff()->relDirPath();
    //$exceptDirs = array();
    $items = DirManager::inst()->getDirContentFull(null, function (DirItem $di) {
        dolog($di->getAbsPath());
        return $di->isFile() && ends_with($di->getName(), 'Messages.msgs');
    }, $exceptDirs);
    dolog('Message files for processing: {}', count($items));
    //Проверим, что был выбран тестовый файл
    check_condition(in_array($TEST_MSGS_DI, $items, true), "Test file [{$TEST_MESSAGES_FILE}] is not included");
    //Удалим его из массива...
    array_remove_value($items, $TEST_MSGS_DI, true);
    //И поместим наверх, чтобы он был обработан первым
    array_unshift($items, $TEST_MSGS_DI);
    /* @var $msgsDi DirItem */
    foreach ($items as $msgsDi) {
        dolog('PROCESSING [{}]', $msgsDi->getAbsPath());
        //Сбросим методы
        $METHODS = array();
        //Извлечём название класса
        $CLASS = $msgsDi->getNameNoExt();
        //DirItem файла с сообщениями php
        $classDi = DirItem::inst($msgsDi->getDirname(), $CLASS, PsConst::EXT_PHP);
        //Получаем сообщения из файла .msgs
        $messages = $msgsDi->getFileAsProps();
        foreach ($messages as $MSG_KEY => $MGG_VALUE) {
            dolog(' >> {}={}', $MSG_KEY, $MGG_VALUE);
            //Получим список всех параметров из макросов ({0}, {1}, {2} и т.д.)
            preg_match_all("/\\{(.+?)\\}/", $MGG_VALUE, $args);
            $args = array_values(array_unique($args[1]));
            sort($args, SORT_NUMERIC);
            $ARGS = array();
            for ($index = 0; $index < count($args); $index++) {
                $arg = $args[$index];
                $lineDescr = PsStrings::replaceWithBraced('[{}] in line [{}={}]', $msgsDi->getRelPath(), $MSG_KEY, $MGG_VALUE);
                check_condition(is_inumeric($arg), "Invalid argument [{$arg}] for {$lineDescr}");
                check_condition($index == $args[$index], "Missing index [{$index}] for {$lineDescr}");
                $ARGS[] = '$p' . $index;
            }
            $ARGS = join(', ', $ARGS);
            //Добавляем метод
            dolog(" A: {}::{} ({})", $CLASS, $MSG_KEY, $ARGS);
            $PARAMS['SUPPORT_CLASS'] = PsMsgs::getClass();
            $PARAMS['MESSAGE'] = $MGG_VALUE;
            $PARAMS['FUNCTION'] = $MSG_KEY;
            $PARAMS['ARGS'] = $ARGS;
            $METHODS[] = PsStrings::replaceMapBracedKeys($METHOD_PATTERN, $PARAMS);
        }
        dolog('Made methods: ({})', count($METHODS));
        if ($METHODS) {
            $PARAMS['FILE'] = $msgsDi->getAbsPath();
            $PARAMS['CLASS'] = $CLASS;
            $PARAMS['DATE'] = date(DF_PS);
            $PARAMS['METHODS'] = "\n" . join("\n\n", $METHODS) . "\n";
            $CLASS_PHP = PsStrings::replaceMapBracedKeys($CLASS_PATTERN, $PARAMS);
            $classDi->putToFile($CLASS_PHP);
        }
        /*
         * Если обрабатываем тестовый файл - проверим его
         */
        //TEST CLASS VALIDATION START >>>
        if ($msgsDi->equals($TEST_MSGS_DI)) {
            dolog('');
            dolog('Validating test class {}', $TEST_MESSAGES_FILE);
            //Проверим, что .php был сгенерирован
            check_condition($TEST_PHP_DI->isFile(), "File {$TEST_PHP_DI->getAbsPath()} was not created!");
            //Проверим, что для каждого сообщения был создан метод
            $methods = PsUtil::newReflectionClass($TEST_MESSAGES_FILE)->getMethods();
            $messages = $TEST_MSGS_DI->getFileAsProps();
            check_condition(count($methods) == count($messages), 'Methods count missmatch, check ' . $TEST_PHP_DI->getAbsPath());
            /* @var $method ReflectionMethod */
            foreach ($methods as $method) {
                check_condition(array_key_exists($method->getName(), $messages), "No method {$TEST_MESSAGES_FILE}::" . $method->getName());
            }
            //Проверим, что возвращают методы тестового сгенерированного класса
            function doTest($className, $methodName, array $params, $expected)
            {
                $method = "{$className}::{$methodName}";
                $actual = call_user_func_array($method, $params);
                dolog("{}({})='{}', Expected='{}'", $method, join(', ', $params), $actual, $expected);
                check_condition($actual == $expected, "{$actual} != {$expected}");
            }
            doTest($TEST_MESSAGES_FILE, 'message1', array(), 'Message 1');
            doTest($TEST_MESSAGES_FILE, 'message2', array('a', 'b', 'c'), 'Parametred a,c,b');
            dolog('Test class is valid!');
        }
        //TEST CLASS VALIDATION END <<<
    }
    //# DirItems validation end
}
Esempio n. 7
0
 /** @return DirManager */
 public function getResourcesDm($ident = null, $subDir = null)
 {
     $this->assertHasAccess($ident, !!$ident);
     return DirManager::resources(array('folded', $this->getFoldingGroup(), $ident, $subDir));
 }