Example #1
0
 public function testParseWithFilters()
 {
     $filters = [];
     $filters['upper'] = function ($value) {
         return strtoupper($value);
     };
     $filters['lower'] = function ($value) {
         return strtolower($value);
     };
     $content = '';
     $content .= '{foo} {foo|upper} {foo|lower} ';
     $content .= '{posts}{title}{title|upper}{title|lower}{/posts}';
     $vars = ['foo' => 'Bar', 'posts' => [['title' => 'Dog'], ['title' => 'Cat']]];
     $result = TextParser::parse($content, $vars, ['filters' => $filters]);
     $this->assertEquals('Bar BAR bar DogDOGdogCatCATcat', $result);
 }
 public function initVersion($versionType)
 {
     $versionTypes = ['migration', 'seeder', 'custom'];
     if (!in_array($versionType, $versionTypes)) {
         throw new SystemException('Unknown version type.');
     }
     $this->version = $this->getNextVersion();
     if ($versionType == 'custom') {
         $this->scriptFileName = null;
         return;
     }
     $templateFiles = ['migration' => 'migration.php.tpl', 'seeder' => 'seeder.php.tpl'];
     $templatePath = '$/rainlab/builder/classes/migrationmodel/templates/' . $templateFiles[$versionType];
     $templatePath = File::symbolizePath($templatePath);
     $fileContents = File::get($templatePath);
     $scriptFileName = $versionType . str_replace('.', '-', $this->version);
     $pluginCodeObj = $this->getPluginCodeObj();
     $this->code = TextParser::parse($fileContents, ['className' => Str::studly($scriptFileName), 'namespace' => $pluginCodeObj->toUpdatesNamespace(), 'tableNamePrefix' => $pluginCodeObj->toDatabasePrefix()]);
     $this->scriptFileName = $scriptFileName;
 }
Example #3
0
 public function getProcessedPlaceholderMarkup($placeholderName, $placeholderContents)
 {
     if (array_key_exists($placeholderName, $this->processedBlockMarkupCache)) {
         return $this->processedBlockMarkupCache[$placeholderName];
     }
     /*
      * Process snippets
      */
     $markup = Snippet::processPageMarkup($this->getFileName() . md5($placeholderName), $this->theme, $placeholderContents);
     /*
      * Inject global view variables
      */
     $globalVars = ViewHelper::getGlobalVars();
     if (!empty($globalVars)) {
         $markup = TextParser::parse($markup, $globalVars);
     }
     return $this->processedBlockMarkupCache[$placeholderName] = $markup;
 }
Example #4
0
 /**
  * Renders a requested content file.
  * The framework uses this method internally.
  * @param string $name The content view to load.
  * @param array $parameters Parameter variables to pass to the view.
  * @return string
  */
 public function renderContent($name, $parameters = [])
 {
     /*
      * Extensibility
      */
     if (($event = $this->fireEvent('page.beforeRenderContent', [$name], true)) || ($event = Event::fire('cms.page.beforeRenderContent', [$this, $name], true))) {
         $content = $event;
     } elseif (($content = Content::loadCached($this->theme, $name)) === null) {
         throw new CmsException(Lang::get('cms::lang.content.not_found_name', ['name' => $name]));
     }
     $fileContent = $content->parsedMarkup;
     /*
      * Inject global view variables
      */
     $globalVars = ViewHelper::getGlobalVars();
     if (!empty($globalVars)) {
         $parameters = (array) $parameters + $globalVars;
     }
     /*
      * Parse basic template variables
      */
     if (!empty($parameters)) {
         $fileContent = TextParser::parse($fileContent, $parameters);
     }
     /*
      * Extensibility
      */
     if (($event = $this->fireEvent('page.renderContent', [$name, $fileContent], true)) || ($event = Event::fire('cms.page.renderContent', [$this, $name, $fileContent], true))) {
         return $event;
     }
     return $fileContent;
 }
 protected function generateMigrationCode($upCode, $downCode)
 {
     $templatePath = '$/rainlab/builder/classes/databasetablemodel/templates/migration-code.php.tpl';
     $templatePath = File::symbolizePath($templatePath);
     $fileContents = File::get($templatePath);
     return TextParser::parse($fileContents, ['upCode' => $upCode, 'downCode' => $downCode]);
 }
 protected function makeFile($filePath, $templateName)
 {
     $path = $this->destinationPath . DIRECTORY_SEPARATOR . $filePath;
     if (File::isFile($path)) {
         throw new ApplicationException(Lang::get('rainlab.builder::lang.common.error_file_exists', ['path' => $path]));
     }
     $fileDirectory = dirname($path);
     if (!File::isDirectory($fileDirectory)) {
         if (!File::makeDirectory($fileDirectory, 0777, true, true)) {
             throw new SystemException(Lang::get('rainlab.builder::lang.common.error_make_dir', ['name' => $fileDirectory]));
         }
     }
     $templatePath = $this->templatesPath . DIRECTORY_SEPARATOR . $templateName;
     if (!File::isFile($templatePath)) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.template_not_found', ['name' => $templateName]));
     }
     $fileContents = File::get($templatePath);
     if (!$fileContents) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.error_loading_template', ['name' => $templateName]));
     }
     $fileContents = TextParser::parse($fileContents, $this->variables);
     if (@File::put($path, $fileContents) === false) {
         throw new SystemException(Lang::get('rainlab.builder::lang.common.error_generating_file', ['path' => $path]));
     }
     @File::chmod($path);
 }