Skip to content

Commit 3b4145c

Browse files
committed
refactored front-controller, added commands structure
1 parent f9f9ecc commit 3b4145c

16 files changed

Lines changed: 447 additions & 243 deletions

bin/indexer.php

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,23 @@
66
set_time_limit(0);
77
ini_set('memory_limit','1000M');
88
ini_set('display_errors', 'stderr');
9-
if(php_sapi_name() == 'cli') {
10-
if(count($argv) < 2) {
11-
echo "error: not enough arguments";
12-
} elseif ($argv[1] == 'generate') {
13-
array_shift($argv);
14-
array_shift($argv);
15-
16-
$verbose = false;
17-
if(count($argv) > 0 && $argv[0] == '-verbose') {
18-
array_shift($argv);
19-
$verbose = true;
20-
}
219

22-
$phpCompletePsr = new IndexGenerator($verbose);
10+
/** @var $command \Command\CommandInterface */
2311

24-
$plugins = explode("-u", implode("", $argv));
25-
foreach ($plugins as $pluginFile) {
26-
if(empty($pluginFile)) {
27-
continue;
28-
}
29-
$phpCompletePsr->addPlugin(trim($pluginFile));
30-
}
12+
$arguments = [];
3113

32-
$index = $phpCompletePsr->generateIndex();
33-
34-
$jsonIndex = json_encode($index);
35-
$lastJsonError = json_last_error();
36-
if($lastJsonError != JSON_ERROR_NONE) {
37-
printJsonError($lastJsonError);
38-
exit;
39-
}
40-
41-
$phpCompletePsr->writeToFile($phpCompletePsr->getIndexFileName(), $jsonIndex);
42-
$phpCompletePsr->writeToFile($phpCompletePsr->getReportFileName(), implode("\n", $phpCompletePsr->getInvalidClasses()));
14+
if(php_sapi_name() == 'cli') {
15+
if(count($argv) < 2) {
16+
$command = new \Command\ErrorCommand;
17+
} elseif ($argv[1] == 'generate') {
18+
$command = new \Command\GenerateCommand;
4319
} else if($argv[1] == 'update') {
44-
array_shift($argv);
45-
array_shift($argv);
46-
$file = array_shift($argv);
47-
$cacheFileName = array_shift($argv);
48-
$verbose = false;
49-
50-
$p = new IndexGenerator($verbose);
51-
$plugins = explode("-u", implode("", $argv));
52-
foreach ($plugins as $pluginFile) {
53-
if(empty($pluginFile)) {
54-
continue;
55-
}
56-
$p->addPlugin($pluginFile);
57-
}
58-
59-
$p->writeUpdatedClassInfo($file, $cacheFileName);
60-
//echo "Time Elapsed: ".(microtime(true) - $time)."s\n";
61-
//echo "highest memory ". memory_get_peak_usage();
20+
$command = new \Command\UpdateCommand;
6221
} else {
63-
echo "not a valid argument";
64-
exit;
22+
$command = new \Command\ErrorCommand;
6523
}
6624
} else {
67-
exit;
68-
}
69-
70-
function printJsonError($errorCode)
71-
{
72-
switch (json_last_error()) {
73-
case JSON_ERROR_NONE:
74-
echo ' - No errors';
75-
break;
76-
case JSON_ERROR_DEPTH:
77-
echo ' - Maximum stack depth exceeded';
78-
break;
79-
case JSON_ERROR_STATE_MISMATCH:
80-
echo ' - Underflow or the modes mismatch';
81-
break;
82-
case JSON_ERROR_CTRL_CHAR:
83-
echo ' - Unexpected control character found';
84-
break;
85-
case JSON_ERROR_SYNTAX:
86-
echo ' - Syntax error, malformed JSON';
87-
break;
88-
case JSON_ERROR_UTF8:
89-
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
90-
break;
91-
default:
92-
echo ' - Unknown error';
93-
break;
94-
}
95-
echo "\n";
25+
$command = new \Command\ErrorCommand;
9626
}
9727

28+
$command->run($arguments);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Command;
4+
5+
abstract class AbstractCommand implements CommandInterface{
6+
protected $container;
7+
public function __construct(){
8+
$this->container = new \DI\Container();
9+
}
10+
public function get($serviceName){
11+
return $this->container->get($serviceName);
12+
}
13+
protected function isVerbose($arguments){
14+
$verbose = false;
15+
if(count($arguments) > 0 && $arguments[0] == '-verbose') {
16+
array_shift($arguments);
17+
$verbose = true;
18+
}
19+
return $verbose;
20+
}
21+
protected function addPlugins($arguments){
22+
$generator = $this->get("IndexGenerator");
23+
$plugins = explode("-u", implode("", $arguments));
24+
foreach ($plugins as $pluginFile) {
25+
if(empty($pluginFile)) {
26+
continue;
27+
}
28+
$generator->addPlugin(trim($pluginFile));
29+
}
30+
}
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Command;
4+
5+
interface CommandInterface {
6+
public function run(array $arguments = []);
7+
}

bin/src/Command/ErrorCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Command;
4+
5+
class ErrorCommand implements CommandInterface{
6+
public function run(array $arguments = []){
7+
echo "Error\n";
8+
}
9+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Command;
4+
5+
class GenerateCommand extends AbstractCommand{
6+
public function run(array $arguments = []){
7+
$verbose = $this->isVerbose($arguments);
8+
$this->addPlugins($arguments);
9+
$generator = $this->get("IndexGenerator");
10+
11+
$index = $this->prepareIndex($generator->generateIndex());
12+
$indexWriter = $this->get('IndexWriter');
13+
14+
$indexWriter->writeIndex($index);
15+
$indexWriter->writeReport($generator->getInvalidClasses());
16+
}
17+
protected function prepareIndex($index){
18+
$jsonIndex = json_encode($index);
19+
$lastJsonError = json_last_error();
20+
if($lastJsonError != JSON_ERROR_NONE) {
21+
$this->printJsonError($lastJsonError);
22+
exit;
23+
}
24+
return $jsonIndex;
25+
}
26+
protected function printJsonError($errorCode)
27+
{
28+
switch (json_last_error()) {
29+
case JSON_ERROR_NONE:
30+
echo ' - No errors';
31+
break;
32+
case JSON_ERROR_DEPTH:
33+
echo ' - Maximum stack depth exceeded';
34+
break;
35+
case JSON_ERROR_STATE_MISMATCH:
36+
echo ' - Underflow or the modes mismatch';
37+
break;
38+
case JSON_ERROR_CTRL_CHAR:
39+
echo ' - Unexpected control character found';
40+
break;
41+
case JSON_ERROR_SYNTAX:
42+
echo ' - Syntax error, malformed JSON';
43+
break;
44+
case JSON_ERROR_UTF8:
45+
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
46+
break;
47+
default:
48+
echo ' - Unknown error';
49+
break;
50+
}
51+
echo "\n";
52+
}
53+
}

bin/src/Command/UpdateCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Command;
4+
5+
class UpdateCommand extends AbstractCommand{
6+
public function run(array $arguments = []){
7+
array_shift($arguments);
8+
array_shift($arguments);
9+
$file = array_shift($arguments);
10+
$cacheFileName = array_shift($arguments);
11+
$verbose = false;
12+
13+
$writer = $this->get("IndexWriter");
14+
$this->addPlugins($arguments);
15+
16+
$writer->writeUpdatedClassInfo($file, $cacheFileName);
17+
}
18+
}

bin/src/DI/Container.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace DI;
4+
5+
class Container {
6+
private $map = [];
7+
8+
public function __construct(){
9+
$this->map = [
10+
'Index' => new \DTO\Index(),
11+
'PathUtils' => new \Phine\Path\Path(),
12+
'ClassValidator' => new \Validator\ClassValidator(),
13+
'ClassParser' => new \Parser\ClassParser()
14+
];
15+
$this->map['Path'] = new \Utils\PathResolver($this->get("PathUtils"));
16+
$this->map['Composer'] = new \Utils\Composer($this->get("Path"));
17+
$this->map['ClassUtils'] = new \Utils\ClassUtils(
18+
$this->get("Path"),
19+
$this->get("ClassParser"),
20+
$this->get("ClassValidator")
21+
);
22+
$this->map["IndexGenerator"] = new \IndexGenerator(
23+
$this->get("Path"),
24+
$this->get("Composer"),
25+
$this->get("ClassUtils")
26+
);
27+
$this->map["IndexWriter"] = new \Utils\IndexWriter(
28+
$this->get("IndexGenerator")
29+
);
30+
}
31+
public function get($service){
32+
if(array_key_exists($service, $this->map)){
33+
return $this->map[$service];
34+
}
35+
else {
36+
throw new \Exception("Unknown service \"{$service}\"");
37+
}
38+
}
39+
public function set($serviceName, $service, $overwrite = false){
40+
if(!array_key_exists($serviceName, $this->map) || $overwrite){
41+
$this->map[$serviceName] = $service;
42+
}
43+
else {
44+
throw new \Exception("Service \"{$serviceName}\" already exists");
45+
}
46+
return $this;
47+
}
48+
}

bin/src/DTO/ClassData.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace DTO;
4+
5+
class ClassData{
6+
public $interfaces = [];
7+
public $parentClasses = [];
8+
public $methods = [];
9+
public $parameters = [];
10+
public function toArray(){
11+
return get_object_vars($this);
12+
}
13+
}

bin/src/DTO/FQCN.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace DTO;
4+
5+
class FQCN {
6+
public $className;
7+
public $namespace;
8+
public function __construct($className, $namespace = ""){
9+
$this->className = $className;
10+
$this->namespace = $namespace;
11+
}
12+
public function toString(){
13+
return $this->namespace . '\\' . $this->className;
14+
}
15+
public function __toString(){
16+
return $this->toString();
17+
}
18+
}

0 commit comments

Comments
 (0)