Ejemplo n.º 1
0
 /**
  * Formats an INI file string from an array
  *
  * @param array $vars Data to format.
  * @param int $level Specifies the level of array value.
  * @return string Returns the INI file string.
  * @static 
  */
 public static function render($vars = array(), $level = 1)
 {
     return \October\Rain\Parse\Ini::render($vars, $level);
 }
 public function testRender()
 {
     $parser = new IniParser();
     $data = ['var1' => 'value 1', 'var2' => 'value 21'];
     $path = __DIR__ . '/../fixtures/parse/simple.ini';
     $this->assertFileExists($path);
     $str = $parser->render($data);
     $this->assertNotEmpty($str);
     $this->assertEquals($this->getContents($path), $str);
     $data = ['section' => ['sectionVar1' => 'section value 1', 'sectionVar2' => 'section value 2'], 'section data' => ['sectionVar3' => 'section value 3', 'sectionVar4' => 'section value 4'], 'emptysection' => [], 'var1' => 'value 1', 'var2' => 'value 21'];
     $path = __DIR__ . '/../fixtures/parse/sections.ini';
     $this->assertFileExists($path);
     $str = $parser->render($data);
     $this->assertEquals($this->getContents($path), $str);
     $data = ['section' => ['sectionVar1' => 'section value 1', 'sectionVar2' => 'section value 2', 'subsection' => ['subsection value 1', 'subsection value 2'], 'sectionVar3' => 'section value 3'], 'section data' => ['sectionVar3' => 'section value 3', 'sectionVar4' => 'section value 4', 'subsection' => ['subsection value 1', 'subsection value 2']], 'var1' => 'value 1', 'var2' => 'value 21'];
     $path = __DIR__ . '/../fixtures/parse/subsections.ini';
     $this->assertFileExists($path);
     $str = $parser->render($data);
     $this->assertEquals($this->getContents($path), $str);
 }
Ejemplo n.º 3
0
 /**
  * Parses a CMS object file content.
  * The expected file format is following:
  * <pre>
  * INI settings section
  * ==
  * PHP code section
  * ==
  * Twig markup section
  * </pre>
  * If the content has only 2 sections they are considered as settings and Twig.
  * If there is only a single section, it is considered as Twig.
  * @param string $content Specifies the file content.
  * @return array Returns an array with the following indexes: 'settings', 'markup', 'code'.
  * The 'markup' and 'code' elements contain strings. The 'settings' element contains the 
  * parsed INI file as array. If the content string doesn't contain a section, the corresponding
  * result element has null value.
  */
 public static function parse($content, $options = [])
 {
     extract(array_merge(['isCompoundObject' => true], $options));
     $result = ['settings' => [], 'code' => null, 'markup' => null];
     if (!$isCompoundObject || !strlen($content)) {
         return $result;
     }
     $iniParser = new Ini();
     $sections = preg_split('/^={2,}\\s*/m', $content, -1);
     $count = count($sections);
     foreach ($sections as &$section) {
         $section = trim($section);
     }
     if ($count >= 3) {
         $result['settings'] = @$iniParser->parse($sections[0], true) ?: [self::ERROR_INI => $sections[0]];
         $result['code'] = $sections[1];
         $result['code'] = preg_replace('/^\\s*\\<\\?php/', '', $result['code']);
         $result['code'] = preg_replace('/^\\s*\\<\\?/', '', $result['code']);
         $result['code'] = preg_replace('/\\?\\>\\s*$/', '', $result['code']);
         $result['code'] = trim($result['code'], PHP_EOL);
         $result['markup'] = $sections[2];
     } elseif ($count == 2) {
         $result['settings'] = @$iniParser->parse($sections[0], true) ?: [self::ERROR_INI => $sections[0]];
         $result['markup'] = $sections[1];
     } elseif ($count == 1) {
         $result['markup'] = $sections[0];
     }
     return $result;
 }