-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentManager.php
119 lines (102 loc) · 3.24 KB
/
ComponentManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace ProgA\SystemDependecies;
require 'Component.php';
class ComponentManager {
private $components;
private static $componentManager;
function __construct()
{
$this->setComponents(array('TELNET', 'TCPIP','NETCARD', 'DNS', 'foo', 'HTML', 'BROWSER'));
}
public static function getInstance()
{
if (self::$componentManager === NULL) {
self::$componentManager = new ComponentManager();
}
return self::$componentManager;
}
/**
* @return mixed
*/
public function getComponents()
{
return $this->components;
}
/**
* @param $componentName
* @return Component
*/
public function getComponent($componentName)
{
return $this->components[$componentName];
}
/**
* @param $name
* @param $components
* @return $this
*/
public function setComponent($name, $components)
{
$this->components[$name] = new Component($name, $components);
return $this;
}
public function setComponents($componentsList)
{
foreach($componentsList as $component) {
$this->components[$component] = new Component($component, array());
}
}
public function listInstalledComponents()
{
/** @var Component $component */
foreach($this->getComponents() as $component) {
if($component->isInstalled()) {
echo " {$component->getName()}\n";
}
}
}
public function main(ComponentManager $componentManager = null) {
if($componentManager == null) {
$componentManager = $this->getInstance();
}
$handle = fopen($_SERVER['argv'][1], "r");
if ($handle) {
while (($buffer = fgets($handle, 80)) !== false) {
echo $buffer;
$keywords = preg_split('/\s+/', $buffer, -1, PREG_SPLIT_NO_EMPTY);
switch($keywords[0]) {
case 'DEPEND':
//setup component dependencies
$componentManager->setComponent($keywords[1], array_slice($keywords, 2));
break;
case 'INSTALL':
//install component
$componentManager->getComponent($keywords[1])->install(true);
break;
case 'REMOVE':
//remove component
$componentManager->getComponent($keywords[1])->remove();
break;
case 'LIST':
//list installed components
$componentManager->listInstalledComponents();
break;
case 'END':
//end of input
break;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
echo "\n";
fclose($handle);
//var_dump($componentManager->getComponents());
}
}
}
if (file_exists($_SERVER['argv'][1])) {
ComponentManager::getInstance()->main();
} else {
echo "The file {$_SERVER['argv'][1]} does not exist\n";
}