Skip to content

Commit 579afb7

Browse files
author
Jean-François Hivert
committed
Version 1.1
1 parent 3e45d87 commit 579afb7

7 files changed

Lines changed: 690 additions & 175 deletions

File tree

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
This repository is the base of PHP Shell project. It contains base classes for create a PHP Shell.
44
You must have a service which has an API and develop classes to use PHP-CLI Shell and your API.
55

6-
There is two project which use PHP-CLI Shell:
6+
There is three projects which use PHP-CLI Shell:
77
- PHPIPAM: https://github.com/cloudwatt/php-cli-shell_phpipam
88
- PATCHMANAGER: https://github.com/cloudwatt/php-cli-shell_patchmanager
9+
- FIREWALL: https://github.com/cloudwatt/php-cli-shell_firewall
910

1011
PHPIPAM is an IPAM in PHP with REST API: https://phpipam.net/
11-
PATCHMANAGER is an DCIM in JAVA with SOAP and REST API: https://patchmanager.com/
12+
PATCHMANAGER is an DCIM in JAVA with SOAP and REST API: https://patchmanager.com/
13+
FIREWALL is an ACL manager with firewall appliance templating.
14+
*FIREWALL service can use PHPIPAM API for autocompletion objects.*
1215

13-
You can use one of this two projects for help you to develop your own project.
16+
You can use one of this three projects for help you to develop your own project.
1417

1518

1619
# INSTALLATION
@@ -23,4 +26,4 @@ __*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
2326

2427
#### REPOSITORY
2528
* git clone https://github.com/cloudwatt/php-cli-shell_base
26-
* git checkout tags/v1.0
29+
* git checkout tags/v1.1

classes/arrayObject.php

Lines changed: 113 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
class MyArrayObject implements IteratorAggregate, ArrayAccess, Countable
2+
class MyArrayObject implements Iterator, ArrayAccess, Countable
33
{
44
protected $_array;
55

@@ -8,28 +8,70 @@ public function __construct($input = array())
88
$this->_array = (array) $input;
99
}
1010

11-
public function merge(array $input)
11+
public function merge($input)
1212
{
13-
// /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
13+
$input = $this->_toArray($input);
1414
$this->_array = array_merge($this->_array, $input);
1515
return $this;
1616
}
1717

18-
public function merge_recursive(array $input)
18+
public function merge_recursive($input)
1919
{
20-
// /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
20+
$input = $this->_toArray($input);
2121
$this->_array = array_merge_recursive($this->_array, $input);
2222
return $this;
2323
}
2424

25+
public function replace($input)
26+
{
27+
$input = $this->_toArray($input);
28+
$this->_array = array_replace($this->_array, $input);
29+
return $this;
30+
}
31+
32+
public function replace_recursive($input)
33+
{
34+
$input = $this->_toArray($input);
35+
$this->_array = array_replace_recursive($this->_array, $input);
36+
return $this;
37+
}
38+
39+
public function keys()
40+
{
41+
return array_keys($this->_array);
42+
}
43+
2544
public function key_exists($key)
2645
{
2746
return array_key_exists($key, $this->_array);
2847
}
2948

30-
public function getIterator()
49+
public function rewind()
50+
{
51+
$return = reset($this->_array);
52+
return $this->_format($return);
53+
}
54+
55+
public function current()
3156
{
32-
return new ArrayIterator($this->_array);
57+
$return = current($this->_array);
58+
return $this->_format($return);
59+
}
60+
61+
public function key()
62+
{
63+
return key($this->_array);
64+
}
65+
66+
public function next()
67+
{
68+
$return = next($this->_array);
69+
return $this->_format($return);
70+
}
71+
72+
public function valid()
73+
{
74+
return (key($this->_array) !== null);
3375
}
3476

3577
public function offsetSet($offset, $value)
@@ -66,9 +108,65 @@ public function count()
66108
return count($this->_array);
67109
}
68110

111+
public function toArray()
112+
{
113+
return $this->_toArray($this->_array, true);
114+
}
115+
116+
public function toObject()
117+
{
118+
$array = $this->_toArray($this->_array, true);
119+
return new ArrayObject($array, ArrayObject::ARRAY_AS_PROPS);
120+
}
121+
122+
protected function _toArray($input, $recursive = false)
123+
{
124+
/**
125+
* Ne pas utiliser de référence foreach($input as &$array) sinon:
126+
* Uncaught Error: An iterator cannot be used with foreach by reference
127+
*/
128+
foreach($input as $key => $array)
129+
{
130+
if(is_array($array) || $array instanceof MyArrayObject || $array instanceof ArrayObject) {
131+
$input[$key] = $this->_toArray($array);
132+
}
133+
}
134+
135+
if(is_array($input)) {
136+
return $input;
137+
}
138+
elseif(is_object($input))
139+
{
140+
// /!\ If get_class() is called with anything other than an object, an E_WARNING level error is raised
141+
switch(get_class($input))
142+
{
143+
//case get_class(): {
144+
case static::class: {
145+
return $input->toArray();
146+
}
147+
case 'ArrayObject': {
148+
return $input->getArrayCopy();
149+
}
150+
}
151+
}
152+
153+
throw new Exception('Argument 1 passed must be of the type array or ArrayObject', E_USER_ERROR);
154+
}
155+
156+
protected function _format($data)
157+
{
158+
if(is_array($data)) {
159+
$className = static::class;
160+
return new $className($data);
161+
}
162+
else {
163+
return $data;
164+
}
165+
}
166+
69167
public function __isset($name)
70168
{
71-
return isset($this->_array[$name]);
169+
return array_key_exists($name, $this->_array);
72170
}
73171

74172
public function __unset($name)
@@ -78,18 +176,16 @@ public function __unset($name)
78176

79177
public function __get($name)
80178
{
81-
if(array_key_exists($name, $this->_array))
82-
{
83-
if(is_array($this->_array[$name])) {
84-
$className = static::class;
85-
return new $className($this->_array[$name]);
86-
}
87-
else {
88-
return $this->_array[$name];
89-
}
179+
if(isset($this->{$name})) {
180+
return $this->_format($this->_array[$name]);
90181
}
91182
else {
92183
throw new Exception("This attribute ".$name." does not exist", E_USER_ERROR);
93184
}
94185
}
186+
187+
public function debug()
188+
{
189+
return $this->toArray();
190+
}
95191
}

classes/config.php

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
include_once(__DIR__ . '/arrayObject.php');
2+
require_once(__DIR__ . '/arrayObject.php');
33

44
class CONFIG implements IteratorAggregate
55
{
@@ -11,55 +11,71 @@ class CONFIG implements IteratorAggregate
1111
private $_position = 0;
1212

1313

14-
private function __construct($filename)
14+
private function __construct()
1515
{
1616
$this->_filename = array();
1717
$this->_configuration = new MyArrayObject();
18-
19-
$status = $this->_readConfig($filename);
20-
21-
if(!$status) {
22-
throw new Exception("Config file does not exist or is not readable", E_USER_ERROR);
23-
}
2418
}
2519

26-
private function _readConfig($filename)
20+
private function _readConfig($filename, $fileMissingError = true)
2721
{
28-
if(file_exists($filename) && is_readable($filename))
22+
if(file_exists($filename))
2923
{
30-
if(!in_array($filename, $this->_filename, true))
24+
if(is_readable($filename))
3125
{
32-
$this->_filename[] = $filename;
33-
$json = file_get_contents($filename);
34-
$configuration = json_decode($json, true);
35-
$this->_configuration->merge_recursive($configuration);
36-
}
26+
if(!in_array($filename, $this->_filename, true))
27+
{
28+
$this->_filename[] = $filename;
29+
$json = file_get_contents($filename);
30+
$configuration = json_decode($json, true);
31+
32+
if($configuration === null) {
33+
throw new Exception("Configuration '".$filename."' is not a valid JSON", E_USER_ERROR);
34+
}
3735

38-
return true;
36+
$this->_configuration->replace_recursive($configuration);
37+
}
38+
}
39+
else {
40+
throw new Exception("Configuration '".$filename."' is not readable", E_USER_ERROR);
41+
}
42+
}
43+
elseif($fileMissingError) {
44+
throw new Exception("Configuration '".$filename."' does not exist", E_USER_ERROR);
3945
}
4046
else {
4147
return false;
4248
}
49+
50+
return true;
4351
}
4452

4553
public static function getInstance($filename = null)
4654
{
47-
if(self::$_instance === null)
48-
{
49-
if($filename !== null) {
50-
self::$_instance = new self($filename);
51-
}
52-
else {
53-
throw new Exception("Config filename argument is null", E_USER_ERROR);
54-
}
55+
if(self::$_instance === null) {
56+
self::$_instance = new self();
5557
}
56-
else {
57-
self::$_instance->_readConfig($filename);
58+
59+
if($filename !== null) {
60+
self::loadConfigurations($filename);
5861
}
5962

6063
return self::$_instance;
6164
}
6265

66+
public static function loadConfigurations($filenames, $fileMissingError = true)
67+
{
68+
$status = true;
69+
$filenames = (array) $filenames;
70+
71+
foreach($filenames as $filename) {
72+
$_status = self::$_instance->_readConfig($filename, $fileMissingError);
73+
$status = $status && $_status;
74+
}
75+
76+
return $status;
77+
}
78+
6379
public function getIterator()
6480
{
6581
return $this->_configuration->getIterator();

0 commit comments

Comments
 (0)