Skip to content

Commit

Permalink
Merge pull request #127 from mp3000mp/serialize
Browse files Browse the repository at this point in the history
Added "serialization" support
  • Loading branch information
DavidePastore authored Oct 13, 2020
2 parents 06b91ec + 5ea0bbf commit 857b640
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![Gitter][ico-gitter]][link-gitter]

Config is a file configuration loader that supports PHP, INI, XML, JSON,
YML and Properties files and strings.
YML, Properties and serialized files and strings.

## Requirements

Expand Down Expand Up @@ -185,6 +185,7 @@ $config = Config::load('config.json');
$ini = $config->toString(new Ini()); // Encode to string if you want to save the file yourself

$config->toFile('config.yaml');
$config->toFile('config.txt', new Serialize()); // you can also force the writer
```

### Using with default values
Expand Down
2 changes: 2 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Config extends AbstractConfig
'Noodlehaus\Parser\Xml',
'Noodlehaus\Parser\Yaml',
'Noodlehaus\Parser\Properties',
'Noodlehaus\Parser\Serialize'
];

/**
Expand All @@ -46,6 +47,7 @@ class Config extends AbstractConfig
'Noodlehaus\Writer\Xml',
'Noodlehaus\Writer\Yaml',
'Noodlehaus\Writer\Properties',
'Noodlehaus\Writer\Serialize'
];

/**
Expand Down
61 changes: 61 additions & 0 deletions src/Parser/Serialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Noodlehaus\Parser;

use Noodlehaus\Exception\ParseException;

/**
* Class Serialize
*
* @package Config
*/
class Serialize implements ParserInterface
{

/**
* {@inheritdoc}
*/
public function parseFile($filename)
{
$data = file_get_contents($filename);

return (array) $this->parse($data, $filename);
}

/**
* {@inheritdoc}
*/
public function parseString($config)
{
return (array) $this->parse($config);
}


/**
* Completes parsing of JSON data
*
* @param string $data
* @param string $filename
* @return array|null
*
* @throws ParseException If there is an error parsing the serialized data
*/
protected function parse($data = null, $filename = null)
{
$serializedData = @unserialize($data);
if($serializedData === false){

throw new ParseException(error_get_last());
}

return $serializedData;
}

/**
* {@inheritdoc}
*/
public static function getSupportedExtensions()
{
return ['txt'];
}
}
28 changes: 28 additions & 0 deletions src/Writer/Serialize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Noodlehaus\Writer;

/**
* Class Serialize
*
* @package Config
*/
class Serialize extends AbstractWriter
{

/**
* {@inheritdoc}
*/
public function toString($config, $pretty = true)
{
return serialize($config);
}

/**
* {@inheritdoc}
*/
public static function getSupportedExtensions()
{
return ['txt'];
}
}
71 changes: 71 additions & 0 deletions tests/Parser/SerializeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Noodlehaus\Parser\Test;

use PHPUnit\Framework\TestCase;
use Noodlehaus\Parser\Serialize;

/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-21 at 22:37:22.
*/
class SerializeTest extends TestCase
{
/**
* @var Serialize
*/
protected $serialize;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->serialize = new Serialize();
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Noodlehaus\Parser\Serialize::getSupportedExtensions()
*/
public function testGetSupportedExtensions()
{
$expected = ['txt'];
$actual = $this->serialize->getSupportedExtensions();
$this->assertEquals($expected, $actual);
}

/**
* @covers Noodlehaus\Parser\Serialize::parseFile()
* @covers Noodlehaus\Parser\Serialize::parse()
* @expectedException Noodlehaus\Exception\ParseException
* @expectedExceptionMessage unserialize(): Error at offset 57 of 58 bytes
*/
public function testLoadInvalidSerialize()
{
$this->serialize->parseFile(__DIR__ . '/../mocks/fail/error.txt');
}

/**
* @covers Noodlehaus\Parser\Serialize::parseFile()
* @covers Noodlehaus\Parser\Serialize::parseString()
* @covers Noodlehaus\Parser\Serialize::parse()
*/
public function testLoadSerialize()
{
$file = $this->serialize->parseFile(__DIR__ . '/../mocks/pass/config.txt');
$string = $this->serialize->parseString(file_get_contents(__DIR__ . '/../mocks/pass/config.txt'));

$this->assertEquals('localhost', $file['host']);
$this->assertEquals('80', $file['port']);

$this->assertEquals('localhost', $string['host']);
$this->assertEquals('80', $string['port']);
}
}
102 changes: 102 additions & 0 deletions tests/Writer/SerializeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Noodlehaus\Writer\Test;

use Noodlehaus\Writer\Serialize;
use PHPUnit\Framework\TestCase;

class SerializeTest extends TestCase
{
/**
* @var Serialize
*/
protected $writer;

/**
* @var string
*/
protected $temp_file;

/**
* @var array
*/
protected $data;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->writer = new Serialize();
$this->temp_file = tempnam(sys_get_temp_dir(), 'config.txt');
$this->data = [
'application' => [
'name' => 'configuration',
'secret' => 's3cr3t',
],
'host' => 'localhost',
'port' => 80,
'servers' => [
'host1',
'host2',
'host3',
],
];
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
unlink($this->temp_file);
}

/**
* @covers Noodlehaus\Writer\Serialize::getSupportedExtensions()
*/
public function testGetSupportedExtensions()
{
$expected = ['txt'];
$actual = $this->writer->getSupportedExtensions();
$this->assertEquals($expected, $actual);
}

/**
* @covers Noodlehaus\Writer\Serialize::toString()
*/
public function testSerialize()
{
$actual = $this->writer->toString($this->data, false);
$expected = 'a:4:{s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}}';

$this->assertEquals($expected, $actual);
}

/**
* @covers Noodlehaus\Writer\Serialize::toString()
* @covers Noodlehaus\Writer\Serialize::toFile()
*/
public function testWriteSerialize()
{
$this->writer->toFile($this->data, $this->temp_file);

$this->assertFileExists($this->temp_file);
$this->assertFileEquals($this->temp_file, __DIR__.'/../mocks/pass/config4.txt');
}

/**
* @covers Noodlehaus\Writer\Serialize::toString()
* @covers Noodlehaus\Writer\Serialize::toFile()
* @expectedException Noodlehaus\Exception\WriteException
* @expectedExceptionMessage There was an error writing the file
*/
public function testUnwritableFile()
{
chmod($this->temp_file, 0444);

$this->writer->toFile($this->data, $this->temp_file);
}
}
1 change: 1 addition & 0 deletions tests/mocks/fail/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:2:{s:11:"application";a:1:{s:4:"host";s:9:"localhost";}}
1 change: 1 addition & 0 deletions tests/mocks/pass/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:4:{s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}}
1 change: 1 addition & 0 deletions tests/mocks/pass/config4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:4:{s:11:"application";a:2:{s:4:"name";s:13:"configuration";s:6:"secret";s:6:"s3cr3t";}s:4:"host";s:9:"localhost";s:4:"port";i:80;s:7:"servers";a:3:{i:0;s:5:"host1";i:1;s:5:"host2";i:2;s:5:"host3";}}

0 comments on commit 857b640

Please sign in to comment.