Skip to content

Commit 8a6ad4d

Browse files
committed
change how application version (short/long) is handled
1 parent b7bd079 commit 8a6ad4d

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `about` command to display current long version and more information about this package.
13+
1014
### Changed
1115

16+
- option `--version` display now only long version without application description.
1217
- upgrade [docker-php-extension-installer](https://github.com/mlocati/docker-php-extension-installer) to version 1.4.12
1318
- `rdfkafka` extension is now supported by PHP 8.1
1419
- `bin/devkit.php` is made available into the Composer `bin-dir`
@@ -24,6 +29,7 @@ using the [Keep a CHANGELOG](http://keepachangelog.com) principles.
2429

2530
- drop support for PHP 7.3 has ended 6th December 2021.
2631
- drop support for Composer v1
32+
- `Bartlett\PHPToolbox\Console\ApplicationInterface::VERSION` constant to define current version (replaced by Composer Runtime API v2)
2733

2834
## [1.3.0] - 2022-01-09
2935

bin/devkit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// @link https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/
44

55
use Bartlett\PHPToolbox\Console\ApplicationInterface;
6+
use Bartlett\PHPToolbox\Console\Command\About;
67
use Bartlett\PHPToolbox\Console\Command\UpdateExtensions;
78
use Bartlett\PHPToolbox\Console\Command\UpdateTools;
89
use Bartlett\PHPToolbox\DependencyInjection\ContainerFactory;
@@ -11,6 +12,7 @@
1112

1213
$container = (new ContainerFactory())->create(basename(__FILE__, '.php'));
1314
$application = $container->get(ApplicationInterface::class);
15+
$application->add(new About());
1416
$application->add(new UpdateExtensions());
1517
$application->add(new UpdateTools());
1618
exit($application->run());

src/Console/Application.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
*/
88
namespace Bartlett\PHPToolbox\Console;
99

10+
use Composer\InstalledVersions;
11+
1012
use Symfony\Component\Console\Application as SymfonyApplication;
1113
use Symfony\Component\DependencyInjection\ContainerInterface;
1214

15+
use function sprintf;
16+
use function substr;
17+
1318
/**
1419
* @since Release 1.0.0alpha1
1520
* @author Laurent Laville
@@ -23,7 +28,10 @@ final class Application extends SymfonyApplication implements ApplicationInterfa
2328
*/
2429
public function __construct()
2530
{
26-
parent::__construct(self::NAME, self::VERSION);
31+
parent::__construct(
32+
self::NAME,
33+
$this->getInstalledVersion(false)
34+
);
2735
}
2836

2937
/**
@@ -34,4 +42,36 @@ public function setContainer(ContainerInterface $container = null)
3442
{
3543
$this->container = $container;
3644
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
public function getHelp(): string
50+
{
51+
return sprintf(
52+
'<info>%s</info> version <comment>%s</comment>',
53+
$this->getName(),
54+
$this->getVersion()
55+
);
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function getLongVersion(): string
62+
{
63+
return $this->getInstalledVersion();
64+
}
65+
66+
public function getInstalledVersion(bool $withRef = true): string
67+
{
68+
$packageName = 'bartlett/docker-php-toolbox';
69+
70+
$version = InstalledVersions::getPrettyVersion($packageName);
71+
if (!$withRef) {
72+
return $version;
73+
}
74+
$commitHash = InstalledVersions::getReference($packageName);
75+
return sprintf('%s@%s', $version, substr($commitHash, 0, 7));
76+
}
3777
}

src/Console/ApplicationInterface.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@
1717
interface ApplicationInterface extends ContainerAwareInterface
1818
{
1919
public const NAME = 'Helper to discover and install PHP extensions and/or tools';
20-
public const VERSION = '1.2.0';
2120

2221
/**
2322
* @param CommandLoaderInterface $commandLoader
2423
* @return void
2524
*/
2625
public function setCommandLoader(CommandLoaderInterface $commandLoader);
26+
27+
/**
28+
* Gets the name of the application.
29+
*
30+
* @return string
31+
*/
32+
public function getName();
33+
34+
/**
35+
* Gets the current version installed of the application.
36+
*/
37+
public function getInstalledVersion(bool $withRef = true): string;
2738
}

src/Console/Command/About.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of the Docker-PHP-Toolbox package.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
namespace Bartlett\PHPToolbox\Console\Command;
9+
10+
use Bartlett\PHPToolbox\Console\ApplicationInterface;
11+
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Console\Style\SymfonyStyle;
16+
17+
use function sprintf;
18+
19+
/**
20+
* Shows short information about this package.
21+
*
22+
* @since Release 1.4.0
23+
* @author Laurent Laville
24+
*/
25+
final class About extends Command implements CommandInterface
26+
{
27+
public const NAME = 'about';
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
protected function configure(): void
33+
{
34+
$this->setName(self::NAME)
35+
->setDescription('Shows short information about this package')
36+
;
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
protected function execute(InputInterface $input, OutputInterface $output): int
43+
{
44+
$io = new SymfonyStyle($input, $output);
45+
46+
/** @var ApplicationInterface $app */
47+
$app = $this->getApplication();
48+
49+
$lines = [
50+
sprintf(
51+
'<info>%s</info> version <comment>%s</comment>',
52+
$app->getName(),
53+
$app->getInstalledVersion()
54+
),
55+
sprintf(
56+
'<comment>Please visit %s for more information.</comment>',
57+
'https://llaville.github.io/docker-php-toolbox/'
58+
),
59+
];
60+
$io->text($lines);
61+
62+
return self::SUCCESS;
63+
}
64+
}

0 commit comments

Comments
 (0)