Skip to content

Commit 746d631

Browse files
committed
Add ads to --version
1 parent 405bef8 commit 746d631

3 files changed

Lines changed: 133 additions & 17 deletions

File tree

bin/php-matrix

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33

44
declare(strict_types=1);
55

6-
use TypistTech\PhpMatrix\Console\Application;
6+
namespace TypistTech\PhpMatrix;
77

8-
include $_composer_autoload_path ?? __DIR__.'/../vendor/autoload.php';
8+
use TypistTech\PhpMatrix\Console\Runner;
9+
use function in_array;
10+
use const PHP_EOL;
11+
use const PHP_SAPI;
912

10-
Application::make()
11-
->run();
13+
// Taken from https://box-project.github.io/box/faq/#what-is-the-canonical-way-to-write-a-cli-entry-file
14+
if (!in_array(PHP_SAPI, ['micro', 'cli', 'phpdbg', 'embed'], true)) {
15+
echo PHP_EOL . 'This app may only be invoked from a command line, got "' . PHP_SAPI . '"' . PHP_EOL;
16+
exit(1);
17+
}
18+
19+
include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
20+
21+
Runner::run();

src/Console/Application.php

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,107 @@
44

55
namespace TypistTech\PhpMatrix\Console;
66

7-
use Composer\InstalledVersions;
8-
use Symfony\Component\Console\Application as ConsoleApplication;
7+
use Symfony\Component\Console\Application as SymfonyConsoleApplication;
8+
use Symfony\Component\Console\Helper\FormatterHelper;
99

10-
class Application
10+
class Application extends SymfonyConsoleApplication
1111
{
12-
private const string NAME = 'php-matrix';
12+
private const BANNER = <<<BANNER
13+
____ _ _ ____ __ __ _ _
14+
| _ \| | | | _ \ | \/ | __ _| |_ _ __(_)_ __
15+
| |_) | |_| | |_) | | |\/| |/ _` | __| '__| \ \/ /
16+
| __/| _ | __/ | | | | (_| | |_| | | |> <
17+
|_| |_| |_|_| |_| |_|\__,_|\__|_| |_/_/\_\
18+
BANNER;
1319

14-
public static function make(): ConsoleApplication
20+
public const BUILD_TIMESTAMP = '@datetime@';
21+
22+
public function getLongVersion(): string
1523
{
16-
$app = new ConsoleApplication(
17-
self::NAME,
18-
InstalledVersions::getPrettyVersion('typisttech/php-matrix') ?? 'unknown',
24+
$longVersion = self::BANNER;
25+
$longVersion .= PHP_EOL.PHP_EOL;
26+
27+
$app = sprintf(
28+
'%-15s <info>%s</info> %s',
29+
$this->getName(),
30+
$this->getVersion(),
31+
self::BUILD_TIMESTAMP,
32+
);
33+
$longVersion .= $app;
34+
35+
$githubUrl = sprintf(
36+
'<href=https://github.com/typisttech/php-matrix/releases/tag/%1$s>https://github.com/typisttech/php-matrix/releases/tag/%1$s</>',
37+
$this->getVersion(),
38+
);
39+
// https://github.com/box-project/box/blob/b0123f358f2a32488c92e09bf56f16d185e4e3cb/src/Configuration/Configuration.php#L2116
40+
if ((bool) preg_match('/^(?<tag>.+)-\d+-g(?<hash>[a-f0-9]{7})$/', $this->getVersion(), $matches)) {
41+
// Not on a tag.
42+
$githubUrl = sprintf(
43+
'<href=https://github.com/typisttech/php-matrix/compare/%1$s...%2$s>https://github.com/typisttech/php-matrix/compare/%1$s...%2$s</>',
44+
$matches['tag'],
45+
$matches['hash'],
46+
);
47+
}
48+
$longVersion .= PHP_EOL.$githubUrl;
49+
50+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>PHP:</>';
51+
52+
$phpVersion = sprintf(
53+
'%-15s %s',
54+
'Version',
55+
PHP_VERSION,
56+
);
57+
$longVersion .= PHP_EOL.$phpVersion;
58+
59+
$phpSapi = sprintf(
60+
'%-15s %s',
61+
'SAPI',
62+
PHP_SAPI,
63+
);
64+
$longVersion .= PHP_EOL.$phpSapi;
65+
66+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>Support Composer SemVer:</>';
67+
68+
$supportBlock = (new FormatterHelper)
69+
->formatBlock(
70+
[
71+
'If you find this tool useful, please consider supporting its development.',
72+
'Every contribution counts, regardless how big or small.',
73+
'I am eternally grateful to all sponsors who fund my open source journey.',
74+
],
75+
'question',
76+
true,
77+
);
78+
$longVersion .= PHP_EOL.$supportBlock;
79+
80+
$sponsorUrl = sprintf(
81+
'%1$-15s <href=%2$s>%2$s</>',
82+
'GitHub Sponsor',
83+
'https://github.com/sponsors/tangrufus',
1984
);
85+
$longVersion .= PHP_EOL.PHP_EOL.$sponsorUrl;
86+
87+
$longVersion .= PHP_EOL.PHP_EOL.'<comment>Hire Tang Rufus:</>';
2088

21-
$app->addCommands([
22-
new ComposerCommand,
23-
new ConstraintCommand,
24-
]);
89+
$hireBlock = (new FormatterHelper)
90+
->formatBlock(
91+
[
92+
'I am looking for my next role, freelance or full-time.',
93+
'If you find this tool useful, I can build you more weird stuffs like this.',
94+
"Let's talk if you are hiring PHP / Ruby / Go developers.",
95+
],
96+
'error',
97+
true,
98+
);
99+
$longVersion .= PHP_EOL.$hireBlock;
100+
101+
$sponsorUrl = sprintf(
102+
'%1$-15s <href=%2$s>%2$s</>',
103+
'Contact',
104+
'https://typist.tech/contact/',
105+
);
106+
$longVersion .= PHP_EOL.PHP_EOL.$sponsorUrl;
25107

26-
return $app;
108+
return $longVersion;
27109
}
28110
}

src/Console/Runner.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\PhpMatrix\Console;
6+
7+
class Runner
8+
{
9+
private const string NAME = 'PHP Matrix';
10+
11+
private const string GIT_TAG = '@git-tag@';
12+
13+
public static function run(): int
14+
{
15+
$app = new Application(self::NAME, self::GIT_TAG);
16+
17+
$app->addCommands([
18+
new ComposerCommand,
19+
new ConstraintCommand,
20+
]);
21+
22+
return $app->run();
23+
}
24+
}

0 commit comments

Comments
 (0)