Swap FileHelper methods for new Ini parser

This commit is contained in:
Samuel Georges 2015-10-03 07:30:11 +10:00
parent 8ed6a3cc51
commit 91d3d4dbde
12 changed files with 25 additions and 172 deletions

View File

@ -1,5 +1,6 @@
* **Build 300** (2015-10-xx)
- Added new helper `Twig::parse` for parsing Twig.
- Page settings now support infinite array nesting with October flavored INI syntax via `Ini::parse` and `Ini::render`.
* **Build 298** (2015-09-24)
- Added the ability to use a wildcard URL parameter in CMS pages (see CMS > Pages docs).

View File

@ -1,5 +1,6 @@
<?php namespace Cms\Classes;
use Ini;
use Cache;
use Config;
use Validator;
@ -7,7 +8,6 @@ use SystemException;
use ValidationException;
use Cms\Classes\ViewBag;
use Cms\Classes\CodeBase;
use Cms\Classes\FileHelper;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\Extension as CmsTwigExtension;
use System\Twig\Extension as SystemTwigExtension;
@ -227,7 +227,7 @@ class CmsCompoundObject extends CmsObject
$content = [];
if ($this->settings) {
$content[] = FileHelper::formatIniString($this->settings);
$content[] = Ini::render($this->settings);
}
if ($this->code) {

View File

@ -5,6 +5,7 @@ use Lang;
use Cache;
use Config;
use Validator;
use Cms\Helpers\File as FileHelper;
use ApplicationException;
use ValidationException;
use RecursiveDirectoryIterator;

View File

@ -18,7 +18,6 @@ use Twig_Environment;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Classes\FileHelper as CmsFileHelper;
use Cms\Models\MaintenanceSettings;
use System\Models\RequestLog;
use System\Classes\ErrorHandler;

View File

@ -1,4 +1,4 @@
<?php namespace Cms\Classes;
<?php namespace Cms\Helpers;
/**
* Defines some file-system helpers for the CMS system.
@ -6,7 +6,7 @@
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*/
class FileHelper
class File
{
/**
* Validates a CMS object file or directory name.
@ -68,48 +68,4 @@ class FileHelper
return true;
}
/**
* Formats an INI file string from an array
* @param array $data Data to format.
* @param int $level Specifies the level of array value.
* @return string Returns the INI file string.
*/
public static function formatIniString($data, $level = 1)
{
$content = null;
$sections = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
if ($level == 1) {
$sections[$key] = self::formatIniString($value, $level+1);
}
else {
foreach ($value as $val) {
$content .= $key.'[] = "'.self::escapeIniString($val).'"'.PHP_EOL;
}
}
}
elseif (strlen($value)) {
$content .= $key.' = "'.self::escapeIniString($value).'"'.PHP_EOL;
}
}
foreach ($sections as $key => $section) {
$content .= PHP_EOL.'['.$key.']'.PHP_EOL.$section.PHP_EOL;
}
return trim($content);
}
/**
* Escapes a string for saving in INI format
* @param string $string Specifies the string to escape
* @return string Returns the processed string
*/
public static function escapeIniString($string)
{
return str_replace('"', '\"', $string);
}
}

View File

@ -11,9 +11,6 @@
this.options = options || {};
// @deprecated remove if year >= 2016
if (this.options.triggerType !== false && this.options.triggerAction === false) this.options.triggerAction = this.options.triggerType
if (this.options.triggerCondition === false)
throw new Error('Trigger condition is not specified.')

View File

@ -27,12 +27,12 @@ return [
'October\Rain\Foundation\Providers\ArtisanServiceProvider',
'October\Rain\Database\DatabaseServiceProvider',
'October\Rain\Filesystem\FilesystemServiceProvider',
'October\Rain\Parse\ParseServiceProvider',
'October\Rain\Html\HtmlServiceProvider',
'October\Rain\Html\UrlServiceProvider',
'October\Rain\Network\NetworkServiceProvider',
'October\Rain\Scaffold\ScaffoldServiceProvider',
'October\Rain\Flash\FlashServiceProvider',
'October\Rain\Mail\MailServiceProvider',
'October\Rain\Parse\ParseServiceProvider',
];

View File

@ -1,10 +0,0 @@
var1 = "value 1"
var2 = "value 21"
[section]
sectionVar1 = "section value 1"
sectionVar2 = "section value 2"
[section data]
sectionVar3 = "section value 3"
sectionVar4 = "section value 4"

View File

@ -1,2 +0,0 @@
var1 = "value 1"
var2 = "value 21"

View File

@ -1,15 +0,0 @@
var1 = "value 1"
var2 = "value 21"
[section]
sectionVar1 = "section value 1"
sectionVar2 = "section value 2"
subsection[] = "subsection value 1"
subsection[] = "subsection value 2"
sectionVar3 = "section value 3"
[section data]
sectionVar3 = "section value 3"
sectionVar4 = "section value 4"
subsection[] = "subsection value 1"
subsection[] = "subsection value 2"

View File

@ -1,92 +0,0 @@
<?php
use Cms\Classes\FileHelper;
class FileHelperTest extends TestCase
{
public function testValidateName()
{
$this->assertFalse(FileHelper::validateName(''));
$this->assertTrue(FileHelper::validateName('01test-testdat'));
$this->assertTrue(FileHelper::validateName('test/testdat'));
$this->assertFalse(FileHelper::validateName('test\testdat'));
$this->assertTrue(FileHelper::validateName('01test-test.dat'));
$this->assertFalse(FileHelper::validateName('test@test.dat'));
$this->assertFalse(FileHelper::validateName('test::test'));
$this->assertFalse(FileHelper::validateName('@test'));
}
public function testFormatIniString()
{
$data = [
'var1'=>'value 1',
'var2'=>'value 21'
];
$path = base_path().'/tests/fixtures/cms/filehelper/simple.ini';
$this->assertFileExists($path);
$str = FileHelper::formatIniString($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'
],
'var1'=>'value 1',
'var2'=>'value 21'
];
$path = base_path().'/tests/fixtures/cms/filehelper/sections.ini';
$this->assertFileExists($path);
$str = FileHelper::formatIniString($data);
$this->assertEquals($this->getContents($path), $str);
$data = [
'section' => [
'sectionVar1' => 'section value 1',
'sectionVar2' => 'section value 2',
'subsection' => [
'subsectionVar1' => 'subsection value 1',
'subsectionVar2' => 'subsection value 2'
],
'sectionVar3' => 'section value 3'
],
'section data' => [
'sectionVar3' => 'section value 3',
'sectionVar4' => 'section value 4',
'subsection' => [
'subsectionVar1' => 'subsection value 1',
'subsectionVar2' => 'subsection value 2'
]
],
'var1'=>'value 1',
'var2'=>'value 21'
];
$path = base_path().'/tests/fixtures/cms/filehelper/subsections.ini';
$this->assertFileExists($path);
$str = FileHelper::formatIniString($data);
$this->assertEquals($this->getContents($path), $str);
}
//
// Helpers
//
protected function getContents($path)
{
$content = file_get_contents($path);
$content = preg_replace('~\R~u', PHP_EOL, $content); // Normalize EOL
return $content;
}
}

View File

@ -0,0 +1,18 @@
<?php
use Cms\Helpers\File as FileHelper;
class FileTest extends TestCase
{
public function testValidateName()
{
$this->assertFalse(FileHelper::validateName(''));
$this->assertTrue(FileHelper::validateName('01test-testdat'));
$this->assertTrue(FileHelper::validateName('test/testdat'));
$this->assertFalse(FileHelper::validateName('test\testdat'));
$this->assertTrue(FileHelper::validateName('01test-test.dat'));
$this->assertFalse(FileHelper::validateName('test@test.dat'));
$this->assertFalse(FileHelper::validateName('test::test'));
$this->assertFalse(FileHelper::validateName('@test'));
}
}