Skip to content

Commit e0582f2

Browse files
committed
CLI
1 parent 3b779d6 commit e0582f2

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

Engine/Console/CommandInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawidjez
5+
* Date: 14/12/2018
6+
* Time: 12:51
7+
*/
8+
9+
namespace Engine\Console;
10+
11+
12+
interface CommandInterface {
13+
public function execute(array $args): void;
14+
}

Engine/Console/Commands/Help.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawidjez
5+
* Date: 14/12/2018
6+
* Time: 12:50
7+
*/
8+
9+
namespace Console\Commands;
10+
11+
12+
use Engine\Console\CommandInterface;
13+
14+
class Help implements CommandInterface {
15+
16+
public function execute(array $args): void {
17+
$mappings = [
18+
[
19+
'dir' => APP_PATH . '/console/',
20+
'namespace' => 'AppConsole\\Commands\\'
21+
],
22+
[
23+
'dir' => APP_ROOT . '/Engine/Console/Commands/',
24+
'namespace' => 'Console\\Commands\\'
25+
],
26+
];
27+
$found = false;
28+
foreach ($mappings as $mapping) {
29+
if (!$found && file_exists($mapping['dir'] . $commandName . '.php')) {
30+
require_once($mapping['dir'] . $commandName . '.php');
31+
$found = $mapping['namespace'] . $commandName;
32+
}
33+
}
34+
}
35+
}

Engine/Console/Commands/Serve.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawidjez
5+
* Date: 14/12/2018
6+
* Time: 12:50
7+
*/
8+
9+
namespace Console\Commands;
10+
11+
12+
use Engine\Console\CommandInterface;
13+
14+
class Serve implements CommandInterface {
15+
16+
public function execute(array $args = []): void {
17+
$host = 'localhost:8000';
18+
if (count($args)) {
19+
if (isset($args[0])) {
20+
$host = $args[0];
21+
}
22+
}
23+
24+
$command = 'open http://' . $host . ';php -S ' . $host;
25+
26+
exec($command);
27+
}
28+
}

Engine/Console/Console.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawidjez
5+
* Date: 14/12/2018
6+
* Time: 12:09
7+
*/
8+
9+
namespace Engine\Console;
10+
11+
12+
class Console {
13+
14+
public function __constructor(){
15+
16+
}
17+
18+
}

Engine/Console/Shift.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawidjez
5+
* Date: 14/12/2018
6+
* Time: 11:57
7+
*/
8+
9+
namespace Engine\Console;
10+
11+
12+
use Grabower\CliTypo\CliTypo;
13+
use ReflectionClass;
14+
15+
class Shift {
16+
private $_args = [];
17+
protected $_description='xd';
18+
public function __construct(array $argv) {
19+
$this->setArgs($argv);
20+
}
21+
22+
/**
23+
* @param string $name
24+
* @return mixed|null
25+
*/
26+
public function getArg(string $name) {
27+
return isset($this->_args[$name]) ? $this->_args[$name] : null;
28+
}
29+
30+
/**
31+
* @return array
32+
*/
33+
public function getArgs(): array {
34+
return $this->_args;
35+
}
36+
37+
/**
38+
* @param array $args
39+
*/
40+
public function setArgs(array $args): void {
41+
$this->_args = $args;
42+
}
43+
44+
public function run(): void {
45+
$cliTypo = new CliTypo();
46+
if (count($this->_args) < 2) {
47+
$cliTypo->alert()->error('Shift CLI needs at least one parameter');
48+
exit();
49+
}
50+
$commandName = ucfirst($this->_args[1]);
51+
$cliTypo->text()->write($commandName);
52+
53+
$mappings = [
54+
[
55+
'dir' => APP_PATH . '/console/',
56+
'namespace' => 'AppConsole\\Commands\\'
57+
],
58+
[
59+
'dir' => APP_ROOT . '/Engine/Console/Commands/',
60+
'namespace' => 'Console\\Commands\\'
61+
],
62+
];
63+
$found = false;
64+
foreach ($mappings as $mapping) {
65+
if (!$found && file_exists($mapping['dir'] . $commandName . '.php')) {
66+
require_once($mapping['dir'] . $commandName . '.php');
67+
$found = $mapping['namespace'] . $commandName;
68+
}
69+
}
70+
if (!$found) {
71+
$cliTypo->alert()->error('Command ' . $commandName . ' not found');
72+
exit();
73+
}
74+
75+
$cl = new $found();
76+
$class = new ReflectionClass($cl);
77+
$method = $class->getMethod('execute');
78+
$args = array_slice($this->_args, 2, count($this->_args));
79+
$method->invokeArgs($cl, $args);
80+
}
81+
82+
}

0 commit comments

Comments
 (0)