Skip to content

Commit

Permalink
Version 0.5.13
Browse files Browse the repository at this point in the history
- Improved performance of `Inphinit\Experimental\Session` and `Inphinit\Packages`
- `Inphinit\Helper::parseVersion()` use "Semantic Versioning 2.0.0" now
- New param value for Request::is() method, eg: `Request::is('prefetch')` check if exists `Purpose` or `X-Moz` or `X-Purpose` headers, if exist check if value is `prefetch` (case-insenstive)
- Improved performance of `Inphinit\Routing\Route` if is not using regex
- Removed unnecessary check in `UtilsAutoload` function

- Classes that are no longer experimental and are now available in the main namespace (can still be used with the "experimental" namespace to avoid breaking old projects):
    - `Inphinit\Experimental\Config` moved to `Inphinit\Config`
    - `Inphinit\Experimental\Debug` moved to `Inphinit\Debug`
    - `Inphinit\Experimental\Dir` moved to `Inphinit\Dir`
    - `Inphinit\Experimental\Dom\Document` moved to
    - `Inphinit\Experimental\Dom\DomException` moved to
    - `Inphinit\Experimental\Dom\Selector` moved to
    - `Inphinit\Experimental\Exception` moved to `Inphinit\Exception`
    - `Inphinit\Experimental\Maintenance` moved to `Inphinit\Maintenance`

- Methods from `Inphinit\Experimental\File` class that are no longer experimental and are now available in `Inphinit\File` class (can still be used with the `Inphinit\Experimental\File` class to avoid breaking old projects):

    - `Inphinit\Experimental\File::lines()` moved to `Inphinit\File::lines()`
    - `Inphinit\Experimental\File::portion()` moved to `Inphinit\File::portion()`
  • Loading branch information
brcontainer committed May 10, 2020
1 parent 36da409 commit cd62bea
Show file tree
Hide file tree
Showing 25 changed files with 1,768 additions and 1,631 deletions.
199 changes: 1 addition & 198 deletions src/Experimental/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,203 +9,6 @@

namespace Inphinit\Experimental;

use Inphinit\Helper;
use Inphinit\Storage;

class Config implements \IteratorAggregate
class Config extends \Inphinit\Config
{
private static $exceptionlevel = 3;
private $data = array();
private $path;

/**
* Return items from a config file in a object (iterator or with ->)
*
* @param string $path
* @throws \Inphinit\Experimental\Exception
* @return void
*/
public function __construct($path)
{
$this->path = 'application/Config/' . strtr($path, '.', '/') . '.php';

$this->reload();
}

/**
* Create a Negotiation instance
*
* @param string $path
* @throws \Inphinit\Experimental\Exception
* @return \Inphinit\Experimental\Config
*/
public static function load($path)
{
self::$exceptionlevel = 4;

return new static($path);
}

/**
* Reload configuration from file
*
* @param string $path
* @throws \Inphinit\Experimental\Exception
* @return \Inphinit\Experimental\Config
*/
public function reload()
{
$level = self::$exceptionlevel;

self::$exceptionlevel = 2;

if (false === \Inphinit\File::exists(INPHINIT_PATH . $this->path)) {
throw new Exception('File not found ' . $this->path, $level);
}

foreach (\UtilsSandboxLoader($this->path) as $key => $value) {
$this->data[$key] = $value;
}

return $this;
}

/**
* Reload configuration from file
*
* @return bool
*/
public function save()
{
if (Storage::createFolder('tmp/cfg')) {
$wd = preg_replace('#,(\s+|)\)#', '$1)', var_export($this->data, true));
$path = Storage::temp('<?php' . EOL . 'return ' . $wd . ';' . EOL, 'tmp/cfg');

if ($path) {
$response = copy($path, INPHINIT_PATH . $this->path);

unlink($path);

return $response;
}
}

return false;
}

/**
* Get all values like array or get specific item by level (multidimensional) using path
*
* @param string $path (optional) Path with "dots"
* @param string $alternative (optional) alternative value does not find the selected value, default is null
* @return mixed
*/
public function get($path = null, $alternative = null)
{
if ($path === null) {
return $this->data;
}

return Helper::extract($path, $this->data, $alternative);
}

/**
* Set value by path in specific level (multidimensional)
*
* @param string $path Path with "dots"
* @param mixed $value Define value
* @return \Inphinit\Experimental\Config
*/
public function set($path, $value)
{
$paths = explode('.', $path);

$key = array_shift($paths);

$tree = $value;

foreach (array_reverse($paths) as $item) {
$tree = array($item => $tree);
}

$this->data[$key] = $tree;

$tree = null;

return $this;
}

/**
* Magic method for get specific item by ->
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}

/**
* Magic method for set value (this method don't save data)
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
$this->data[$name] = $value;
}

/**
* Magic method for check if value exists in top-level
*
* @param string $name
* @return bool
*/
public function __isset($name)
{
return array_key_exists($name, $this->data);
}

/**
* Magic method for unset variable with `unset()` function
*
* @param string $name
* @return void
*/
public function __unset($name)
{
unset($this->data[$name]);
}

/**
* Allow iteration with `for`, `foreach` and `while`
*
* Example:
* <pre>
* <code>
* $foo = new Config('file'); //or Config::load('file')
*
* foreach ($foo as $key => $value) {
* var_dump($key, $value);
* echo EOL;
* }
* </code>
* </pre>
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}

public function __destruct()
{
$this->data = null;
}
}
Loading

0 comments on commit cd62bea

Please sign in to comment.