Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Refactor configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jubianchi committed Feb 28, 2014
1 parent 0319ce4 commit d9ccc58
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 522 deletions.
14 changes: 7 additions & 7 deletions .phpswitch.yml
@@ -1,32 +1,32 @@
phpswitch:
version: phpswitch-5.3.27
version: phpswitch-5.5.9
versions:
phpswitch_5_3_27:
phpswitch_5_3_28:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl'
config:
date.timezone: Europe/Paris
phar.readonly: Off
phpswitch_5_4_19:
phpswitch_5_4_24:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl'
config:
date.timezone: Europe/Paris
phar.readonly: Off
phpswitch_5_5_3:
phpswitch_5_5_9:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl'
config:
date.timezone: Europe/Paris
phar.readonly: Off
phpswitch_apxs2_5_3_27:
phpswitch_apxs2_5_3_28:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl --with-apxs2=/usr/bin/apxs2'
config:
date.timezone: Europe/Paris
phar.readonly: Off
phpswitch_apxs2_5_4_19:
phpswitch_apxs2_5_4_24:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl --with-apxs2=/usr/bin/apxs2'
config:
date.timezone: Europe/Paris
phar.readonly: Off
phpswitch_apxs2_5_5_3:
phpswitch_apxs2_5_5_9:
options: '--disable-all --enable-ctype --enable-dom --enable-json --enable-phar --enable-libxml --enable-xml --enable-simplexml --enable-tokenizer --with-xsl --enable-hash --enable-pcntl --enable-posix --with-curl=/usr --with-libedit --enable-mbstring --enable-session --with-openssl --with-apxs2=/usr/bin/apxs2'
config:
date.timezone: Europe/Paris
Expand Down
198 changes: 122 additions & 76 deletions src/jubianchi/PhpSwitch/Configuration.php
Expand Up @@ -10,28 +10,95 @@

namespace jubianchi\PhpSwitch;

use jubianchi\PhpSwitch\Configuration\Dumper;

class Configuration implements \IteratorAggregate
abstract class Configuration implements \IteratorAggregate
{
const ROOT = 'phpswitch';

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

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

/** @var \jubianchi\PhpSwitch\Configuration\Dumper */
protected $dumper;

/** @var \jubianchi\PhpSwitch\Configuration\Validator */
protected $validator;

/** @var array */
private $configuration = array();

/** @var \jubianchi\PhpSwitch\Configuration\Dumper */
private $dumper;
/**
* @param string $path
* @param Configuration\Validator $validator
* @param Configuration\Dumper $dumper
*/
public function __construct($path, Configuration\Dumper $dumper = null, Configuration\Validator $validator = null)
{
$this->path = $path;
$this
->setDumper($dumper)
->setValidator($validator)
;

}

/**
* @return string
*/
public function getPath()
{
return $this->path;
}

/**
* @param \jubianchi\PhpSwitch\Configuration\Dumper $dumper
*
* @return \jubianchi\PhpSwitch\Configuration
*/
public function setDumper(Configuration\Dumper $dumper = null)
{
$this->dumper = $dumper ?: new Configuration\Dumper();

return $this;
}

/**
* @return \jubianchi\PhpSwitch\Configuration\Dumper
*/
public function getDumper()
{
return $this->dumper;
}

public function __construct($name = '.phpswitch.yml', Dumper $dumper = null)
/**
* @param \jubianchi\PhpSwitch\Configuration\Validator $validator
*
* @return \jubianchi\PhpSwitch\Configuration
*/
public function setValidator(Configuration\Validator $validator = null)
{
$this->name = $name;
$this->setDumper($dumper ?: new Dumper());
$this->validator = $validator ?: new Configuration\Validator\Pass();

return $this;
}

/**
* @return \jubianchi\PhpSwitch\Configuration\Dumper
*/
public function getValidator()
{
return $this->validator;
}

/**
* @return array
*/
abstract protected function read();

protected function doRead()
{
$this->configuration = $this->validator->validate($this->read());

return $this;
}

/**
Expand All @@ -44,13 +111,14 @@ public function __construct($name = '.phpswitch.yml', Dumper $dumper = null)
*/
public function get($offset, $default = null)
{
$offset = str_replace('-', '_', $offset);
$offset = preg_split('/(?<!\\\)\./', $offset);
$reference = $this->configuration;
$this->doRead();

$offset = self::parseOffest($offset);
$reference = & $this->configuration;
$current = $sep = '';

foreach ($offset as $key) {
$key = preg_replace('/\\\\./', '.', $key);
$key = self::unescapeKey($key);
$current .= $sep . $key;

if (false === array_key_exists($key, $reference) || null === $reference[$key]) {
Expand All @@ -69,6 +137,31 @@ public function get($offset, $default = null)
return $reference;
}

/**
* @param string $offset
*
* @return bool
*/
public function has($offset)
{
$this->doRead();

$offset = self::parseOffest($offset);
$reference = & $this->configuration;

foreach ($offset as $key) {
$key = self::unescapeKey($key);

if (false === array_key_exists($key, $reference) || null === $reference[$key]) {
return false;
}

$reference = & $reference[$key];
}

return true;
}

/**
* @param string $offset
* @param mixed $value
Expand All @@ -79,13 +172,14 @@ public function get($offset, $default = null)
*/
public function set($offset, $value)
{
$offset = str_replace('-', '_', $offset);
$offset = preg_split('/(?<!\\\)\./', $offset);
$this->doRead();

$offset = self::parseOffest($offset);
$reference = & $this->configuration;
$current = $sep = '';

foreach ($offset as $key) {
$key = preg_replace('/\\\\./', '.', $key);
$key = self::unescapeKey($key);
$current .= $sep . $key;
if (false === isset($reference[$key])) {
$reference[$key] = null;
Expand All @@ -98,86 +192,38 @@ public function set($offset, $value)

$reference = $value;

return $this;
}

/**
* @param array $values
*
* @return \jubianchi\PhpSwitch\Configuration
*/
public function setValues(array $values)
{
$this->configuration = $values;
$this->dumper->dump($this->path, array(self::ROOT => $this->configuration));

return $this;
}

/**
* @return array
*/
public function getValues()
{
return $this->configuration;
}

/**
* @return \RecursiveArrayIterator
*/
public function getIterator()
{
return new \RecursiveArrayIterator($this->configuration);
return new \RecursiveArrayIterator($this->doRead()->configuration);
}

/**
* @throws \RuntimeException
*
* @return \jubianchi\PhpSwitch\Configuration
*/
public function dump()
{
$this->dumper->dump($this->path, $this);

return $this;
}

/**
* @param \jubianchi\PhpSwitch\Configuration\Dumper $dumper
* @param string $offset
*
* @return \jubianchi\PhpSwitch\Configuration
* @return array
*/
public function setDumper(Dumper $dumper)
private static function parseOffest($offset)
{
$this->dumper = $dumper;

return $this;
}
$offset = str_replace('-', '_', $offset);

/**
* @return \jubianchi\PhpSwitch\Configuration\Dumper
*/
public function getDumper()
{
return $this->dumper;
return preg_split('/(?<!\\\)\./', $offset);
}

/**
* @param string $path
* @param string $key
*
* @return \jubianchi\PhpSwitch\Configuration
*/
public function setPath($path)
{
$this->path = $path;

return $this;
}

/**
* @return string
*/
public function getPath()
private static function unescapeKey($key)
{
return $this->path;
return preg_replace('/\\\\./', '.', $key);
}
}

0 comments on commit d9ccc58

Please sign in to comment.