Skip to content

Commit

Permalink
Added all the new files, requires testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc André Audet committed Jan 24, 2018
1 parent d494030 commit 9430f01
Show file tree
Hide file tree
Showing 28 changed files with 2,112 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Autoloader.php
@@ -0,0 +1,77 @@
<?php


/**
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


namespace Cervo;


/**
* Autoloader for Cervo.
*
* @author Marc André Audet <maudet@nevraxe.com>
*/
final class Autoloader
{
/** @var Context The current context */
private $context;

/**
* Autoloader constructor.
*
* @param Context $context
*/
public function __construct(Context $context)
{
$this->context = $context;

spl_autoload_register($this);
}

public function __invoke($class)
{
$ex = explode('\\', $class);

if (count($ex) < 3) {
return;
}

$path = $this->context->getModulesManager()->getModulePath($ex[0], $ex[1]);

if (strlen($path) <= 0) {
return;
}

$file_path = $path . \DIRECTORY_SEPARATOR . implode(\DIRECTORY_SEPARATOR, array_slice($ex, 2)) . '.php';

if (file_exists($file_path)) {
require $file_path;
}
}
}
48 changes: 48 additions & 0 deletions src/Config/ArrayConfig.php
@@ -0,0 +1,48 @@
<?php


/**
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


namespace Cervo\Config;


/**
* Configuration manager for Cervo.
*
* @author Marc André Audet <maudet@nevraxe.com>
*/
class ArrayConfig extends BaseConfig
{
public function __construct(?array $values = null)
{
if (is_array($values)) {
$this->setFromArrayRecursive($values);
}
}
}
123 changes: 123 additions & 0 deletions src/Config/BaseConfig.php
@@ -0,0 +1,123 @@
<?php


/**
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


namespace Cervo\Config;


/**
* Configuration manager for Cervo.
*
* @author Marc André Audet <maudet@nevraxe.com>
*/
class BaseConfig
{
/**
* The currently set values in a multi-dimensional array.
* @var array
*/
private $values = [];

public function __construct()
{

}

/**
* Set the value at the specified configuration path.
*
* @param string|array $name The configuration path
* @param mixed $value
*
* @return $this
*/
public function set($name, $value)
{
if (!is_array($name)) {
$name = explode('/', $name);
}

$current = &$this->values;

foreach ($name as $key) {
$current = &$current[$key];
}

$current = $value;

return $this;
}

/**
* Return the value for the specified configuration path.
* If this value is not set, return the default value.
* Return null if not set.
*
* @param string $name The configuration path
*
* @return mixed
*/
public function get(string $name)
{
$current = &$this->values;

foreach (explode('/', $name) as $key) {

if ($current[$key]) {
$current = &$current[$key];
} else {
return null;
}

}

return $current;
}

/**
* Recursively set all the values from an array.
* Usually used when importing.
*
* @param array $array
* @param array $current_path
*/
public function setFromArrayRecursive(array $array, array $current_path = [])
{
foreach ($array as $key => $el) {

if (is_array($el)) {
$this->setFromArrayRecursive($el, array_merge($current_path, [$key]));
} else {
$this->set(array_merge($current_path, [$key]), $el);
}

}
}
}
48 changes: 48 additions & 0 deletions src/Config/JsonConfig.php
@@ -0,0 +1,48 @@
<?php


/**
*
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <maudet@nevraxe.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


namespace Cervo\Config;


/**
* Configuration manager for Cervo.
*
* @author Marc André Audet <maudet@nevraxe.com>
*/
class JsonConfig extends BaseConfig
{
public function __construct(?string $json_file_path = null)
{
if ($json_file_path !== null && file_exists($json_file_path)) {
$this->setFromArrayRecursive(json_decode(file_get_contents($json_file_path), true));
}
}
}

0 comments on commit 9430f01

Please sign in to comment.