Skip to content

Commit ad760cd

Browse files
author
Jean-François Hivert
committed
First release
0 parents  commit ad760cd

8 files changed

Lines changed: 1742 additions & 0 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# PHP-CLI SHELL
2+
3+
This repository is the base of PHP Shell project. It contains base classes for create a PHP Shell.
4+
You must have a service which has an API and develop classes to use PHP-CLI Shell and your API.
5+
6+
There is two project which use PHP-CLI Shell:
7+
- PHPIPAM: https://github.com/cloudwatt/php-cli-shell_phpipam
8+
- PATCHMANAGER: https://github.com/cloudwatt/php-cli-shell_patchmanager
9+
10+
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+
13+
You can use one of this two projects for help you to develop your own project.
14+
15+
16+
# INSTALLATION
17+
18+
#### APT PHP
19+
__*https://launchpad.net/~ondrej/+archive/ubuntu/php*__
20+
* add-apt-repository ppa:ondrej/php
21+
* apt-get update
22+
* apt install php7.1-cli php7.1-mbstring php7.1-readline
23+
24+
#### REPOSITORY TOOLS-CLI
25+
* git clone https://github.com/cloudwatt/php-cli-shell_base
26+
* git checkout ????

classes/arrayObject.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
class MyArrayObject implements IteratorAggregate, ArrayAccess, Countable
3+
{
4+
protected $_array;
5+
6+
public function __construct($input = array())
7+
{
8+
$this->_array = (array) $input;
9+
}
10+
11+
public function merge(array $input)
12+
{
13+
// /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
14+
$this->_array = array_merge($this->_array, $input);
15+
return $this;
16+
}
17+
18+
public function merge_recursive(array $input)
19+
{
20+
// /!\ On ecrase les anciennes valeurs avec les nouvelles pour permettre l'override
21+
$this->_array = array_merge_recursive($this->_array, $input);
22+
return $this;
23+
}
24+
25+
public function key_exists($key)
26+
{
27+
return array_key_exists($key, $this->_array);
28+
}
29+
30+
public function getIterator()
31+
{
32+
return new ArrayIterator($this->_array);
33+
}
34+
35+
public function offsetSet($offset, $value)
36+
{
37+
if(is_null($offset)) {
38+
$this->_array[] = $value;
39+
} else {
40+
$this->_array[$offset] = $value;
41+
}
42+
}
43+
44+
public function offsetExists($offset)
45+
{
46+
return isset($this->_array[$offset]);
47+
}
48+
49+
public function offsetUnset($offset)
50+
{
51+
unset($this->_array[$offset]);
52+
}
53+
54+
public function offsetGet($offset)
55+
{
56+
if($this->offsetExists($offset)) {
57+
return $this->{$offset};
58+
}
59+
else {
60+
return null;
61+
}
62+
}
63+
64+
public function count()
65+
{
66+
return count($this->_array);
67+
}
68+
69+
public function __isset($name)
70+
{
71+
return isset($this->_array[$name]);
72+
}
73+
74+
public function __unset($name)
75+
{
76+
unset($this->_array[$name]);
77+
}
78+
79+
public function __get($name)
80+
{
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+
}
90+
}
91+
else {
92+
throw new Exception("This attribute ".$name." does not exist", E_USER_ERROR);
93+
}
94+
}
95+
}

classes/config.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
include_once(__DIR__ . '/arrayObject.php');
3+
4+
class CONFIG implements IteratorAggregate
5+
{
6+
private static $_instance;
7+
8+
private $_filename;
9+
private $_configuration;
10+
11+
private $_position = 0;
12+
13+
14+
private function __construct($filename)
15+
{
16+
$this->_filename = array();
17+
$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+
}
24+
}
25+
26+
private function _readConfig($filename)
27+
{
28+
if(file_exists($filename) && is_readable($filename))
29+
{
30+
if(!in_array($filename, $this->_filename, true))
31+
{
32+
$this->_filename[] = $filename;
33+
$json = file_get_contents($filename);
34+
$configuration = json_decode($json, true);
35+
$this->_configuration->merge_recursive($configuration);
36+
}
37+
38+
return true;
39+
}
40+
else {
41+
return false;
42+
}
43+
}
44+
45+
public static function getInstance($filename = null)
46+
{
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+
}
56+
else {
57+
self::$_instance->_readConfig($filename);
58+
}
59+
60+
return self::$_instance;
61+
}
62+
63+
public function getIterator()
64+
{
65+
return $this->_configuration->getIterator();
66+
}
67+
68+
public function __get($name)
69+
{
70+
if($this->_configuration->key_exists($name)) {
71+
return $this->_configuration[$name];
72+
}
73+
else {
74+
throw new Exception("Configuration ".$name." does not exist", E_USER_ERROR);
75+
}
76+
}
77+
}

classes/thread.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
class MyThread extends Thread
3+
{
4+
protected $_object;
5+
protected $_method;
6+
protected $_arguments;
7+
8+
protected $_result;
9+
10+
11+
public function __construct(Threaded $object, $method, array $arguments)
12+
{
13+
$this->_object = $object;
14+
$this->_method = $method;
15+
$this->_arguments = $arguments;
16+
}
17+
18+
protected function _getId()
19+
{
20+
return (string) $this->getThreadId();
21+
}
22+
23+
public function run()
24+
{
25+
$this->_result[$this->_getId()] = call_user_func_array(array($this->_object, $this->_method), $this->_arguments);
26+
}
27+
28+
public function getResult()
29+
{
30+
return $this->_result[$this->_getId()];
31+
}
32+
33+
public function __get($name)
34+
{
35+
if($name === 'result') {
36+
return $this->getResult();
37+
}
38+
39+
throw new Exception("This attribute does not exist", E_USER_ERROR);
40+
}
41+
}

0 commit comments

Comments
 (0)