-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliApplication.php
More file actions
52 lines (43 loc) · 1.29 KB
/
Copy pathCliApplication.php
File metadata and controls
52 lines (43 loc) · 1.29 KB
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
<?php
namespace Ptf;
if (php_sapi_name() != 'cli' && !defined('STDIN')) {
exit;
}
require_once 'Application.php';
/**
* Base class for all Ptf CLI applications.
*/
abstract class CliApplication extends Application
{
/**
* Run the application.
*/
public static function run(): void
{
static::initAutoloader(Core\Autoloader::getInstance());
/** @var App\Context $context */
$context = static::getContext();
$args = $_SERVER['argv'];
$route = isset($args[1]) && strpos($args[1], ':') !== false ? str_replace(':', '/', $args[1]) : null;
if (!Core\Router::matchRoute($route, $context)) {
echo "Unknown command \"{$args[1]}\"\n\n";
static::showUsage($args);
return;
}
$output = $context->getCliOutput();
if ($output->hasContent()) {
echo $output->getContent();
} else {
echo $context->getView()->fetch();
}
}
/**
* Display a usage message for the application.<br />
* Overwrite this function to display an individual message.
*/
public static function showUsage(): void
{
$context = static::getContext();
echo "Usage: php {$context->getBasePath(true)} controller:action [options]\n";
}
}