Skip to content

Commit

Permalink
V2 development
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Jun 7, 2021
1 parent 8e002b1 commit db5e66b
Show file tree
Hide file tree
Showing 71 changed files with 2,448 additions and 658 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/vendor/
/tests/_envTest/var/
/tests_env/var/
!**/.gitkeep
/composer.lock
.phpunit.result.cache
Expand Down
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0snapshot
- 8.0

before_install:
- sudo apt-get -qq update
Expand All @@ -13,4 +9,4 @@ before_script:
- composer self-update
- composer install

script: vendor/bin/phpunit
script: vendor/bin/phpunit
28 changes: 25 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning] (http://semver.org/).
For change log format, use [Keep a Changelog] (http://keepachangelog.com/).

All notable changes to this project will be documented in this file. This project adheres
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.0.0-beta1] - 2021-06-07

### Added

- Console with `league/climate` library

### Changed

- Namespace move from `Berlioz\CliCore` to `Berlioz\Cli\Core`
- Refactoring
- Bump minimal compatibility to PHP 8

### Removed

- Dependency to `psr/log`
- Dependency to `ulrichsg/getopt-php`

## [1.1.0] - 2020-11-05

### Added

- PHP 8 compatibility in `composer.json`

### Changed

- Bump `ulrichsg/getopt-php` required package version in `composer.json`

## [1.0.0] - 2020-05-29

First version
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Ronan GIRON
Copyright (c) 2021 Ronan GIRON

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Berlioz Framework CLI Core

[![Latest Version](https://img.shields.io/packagist/v/berlioz/cli-core.svg?style=flat-square)](https://github.com/BerliozFramework/CliCore/releases)
[![Software license](https://img.shields.io/github/license/BerliozFramework/CliCore.svg?style=flat-square)](https://github.com/BerliozFramework/CliCore/blob/master/LICENSE)
[![Build Status](https://img.shields.io/travis/com/BerliozFramework/CliCore/master.svg?style=flat-square)](https://travis-ci.com/BerliozFramework/CliCore)
[![Quality Grade](https://img.shields.io/codacy/grade/4c097c0d66794c59b5126da59170705c/master.svg?style=flat-square)](https://www.codacy.com/manual/BerliozFramework/CliCore)
[![Software license](https://img.shields.io/github/license/BerliozFramework/CliCore.svg?style=flat-square)](https://github.com/BerliozFramework/CliCore/blob/2.x/LICENSE)
[![Build Status](https://img.shields.io/travis/com/BerliozFramework/CliCore/2.x.svg?style=flat-square)](https://travis-ci.com/BerliozFramework/CliCore)
[![Quality Grade](https://img.shields.io/codacy/grade/4c097c0d66794c59b5126da59170705c/2.x.svg?style=flat-square)](https://www.codacy.com/manual/BerliozFramework/CliCore)
[![Total Downloads](https://img.shields.io/packagist/dt/berlioz/cli-core.svg?style=flat-square)](https://packagist.org/packages/berlioz/cli-core)

It's the CLI core of Berlioz Framework.
Expand Down
12 changes: 3 additions & 9 deletions berlioz
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ $autoloadFiles = [__DIR__ . '/../../autoload.php',
__DIR__ . '/vendor/autoload.php'];

foreach ($autoloadFiles as $file) {
if (file_exists($file)) {
if (true === file_exists($file)) {
$autoloadFile = $file;
break;
}
}

if (is_null($autoloadFile)) {
if (null === $autoloadFile) {
fwrite(STDERR, 'You must install Berlioz/CliCore with Composer, looks at https://getcomposer.org/ to do that.' . PHP_EOL);
exit(1);
}
Expand All @@ -38,14 +38,8 @@ require $autoloadFile;
// Application
try {
// App
$app = new \Berlioz\CliCore\App\CliApp();
$app = new \Berlioz\Cli\Core\App\CliApp();
exit($app->handle());
} catch (\GetOpt\ArgumentException $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit(1);
} catch (\Berlioz\CliCore\Exception\CommandException $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit($e->getCode() ?: 1);
} catch (\Throwable $e) {
fwrite(STDERR, rtrim((string) $e) . PHP_EOL);
exit(1);
Expand Down
27 changes: 14 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "berlioz/cli-core",
"type": "library",
"type": "berlioz-package",
"description": "Berlioz Framework CLI Core",
"license": "MIT",
"homepage": "http://getberlioz.com",
"homepage": "https://getberlioz.com",
"authors": [
{
"name": "Ronan Giron",
Expand All @@ -12,42 +12,43 @@
],
"autoload": {
"psr-4": {
"Berlioz\\CliCore\\": "src/"
"Berlioz\\Cli\\Core\\": "src/"
},
"exclude-from-classmap": [
"/tests/"
]
},
"autoload-dev": {
"psr-4": {
"Berlioz\\CliCore\\Tests\\": "tests/",
"Berlioz\\CliCore\\TestProject\\": "tests/_envTest/src/"
"Berlioz\\Cli\\Core\\Tests\\": "tests/",
"Berlioz\\Cli\\Core\\TestProject\\": "tests_env/src/"
},
"exclude-from-classmap": [
"/tests/_envTest/"
"/tests_env/"
]
},
"minimum-stability": "beta",
"prefer-stable": true,
"require": {
"php": "^7.1 || ^8.0",
"ext-json": "*",
"php": "^8.0",
"ext-mbstring": "*",
"berlioz/core": "^1.0",
"ulrichsg/getopt-php": "^3.4"
"berlioz/core": "^2.0",
"league/climate": "^3.7"
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.0 || ^9.0"
"phpunit/phpunit": "^9.3"
},
"provide": {
"psr/container-implementation": "^1.0",
"psr/log-implementation": "^1.0",
"psr/event-dispatcher-implementation": "^1.0",
"psr/simple-cache-implementation": "^1.0"
},
"bin": [
"berlioz"
],
"config": {
"berlioz": {
"package": "Berlioz\\CliCore\\CliCorePackage"
"package": "Berlioz\\Cli\\Core\\BerliozPackage"
}
}
}
17 changes: 10 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./tests/bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Berlioz Framework CLI Core test suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
4 changes: 2 additions & 2 deletions resources/config.default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"commands": {
"berlioz:cache-clear": "\\Berlioz\\CliCore\\Command\\Berlioz\\CacheClearCommand",
"berlioz:config": "\\Berlioz\\CliCore\\Command\\Berlioz\\ConfigCommand"
"berlioz:cache-clear": "\\Berlioz\\Cli\\Core\\Command\\Berlioz\\CacheClearCommand",
"berlioz:config": "\\Berlioz\\Cli\\Core\\Command\\Berlioz\\ConfigCommand"
}
}
138 changes: 17 additions & 121 deletions src/App/CliApp.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2020 Ronan GIRON
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
Expand All @@ -12,146 +12,42 @@

declare(strict_types=1);

namespace Berlioz\CliCore\App;
namespace Berlioz\Cli\Core\App;

use Berlioz\CliCore\Command\CommandInterface;
use Berlioz\CliCore\Exception\CommandException;
use Berlioz\Config\ConfigInterface;
use Berlioz\Cli\Core\Command\CommandHandler;
use Berlioz\Cli\Core\Container\ServiceProvider;
use Berlioz\Cli\Core\Exception\CliException;
use Berlioz\Config\Adapter\JsonAdapter;
use Berlioz\Config\Config;
use Berlioz\Config\Exception\ConfigException;
use Berlioz\Core\App\AbstractApp;
use Berlioz\Core\Debug;
use Berlioz\Core\Exception\BerliozException;
use GetOpt\Argument;
use GetOpt\ArgumentException;
use GetOpt\Command;
use GetOpt\GetOpt;
use GetOpt\Option;
use Psr\SimpleCache\InvalidArgumentException;
use Throwable;
use Berlioz\ServiceContainer\Container;

/**
* Class CliApp.
*
* @package Berlioz\CliCore\App
*/
class CliApp extends AbstractApp
{
/**
* Create commands from config.
*
* @param ConfigInterface $config
*
* @return array
* @throws BerliozException
* @throws CommandException
* @throws ConfigException
*/
private function createCommandsFromConfig(ConfigInterface $config)
{
$commands = $config->get('commands', []);

if (!is_array($commands)) {
throw new BerliozException('Bad configuration format, "commands" key must be an array');
}

$commandsObjects = [];

/**
* @var string $name
* @var CommandInterface $command
*/
foreach ($commands as $name => $command) {
if (!is_string($command)) {
throw new CommandException(sprintf('Command declaration must be a class name'));
}
if (!class_exists($command)) {
throw new CommandException(sprintf('Command class "%s" not found', $command));
}
if (!is_a($command, CommandInterface::class, true)) {
throw new CommandException(
sprintf('Command class "%s" must be implement "%s" interface', $command, CommandInterface::class)
);
}

$commandsObjects[] =
(new Command($name, $command))
->setShortDescription($command::getShortDescription() ?? '')
->setDescription($command::getDescription() ?? $command::getShortDescription() ?? '')
->addOptions($command::getOptions())
->addOperands($command::getOperands());
}

return $commandsObjects;
}

/**
* Get commands.
*
* @return Command[]
* @throws CommandException
* @throws ConfigException
* @throws BerliozException
* @throws InvalidArgumentException
* @inheritDoc
*/
private function getCommands(): array
protected function boot(): void
{
// Get from cache if exists
if (null !== ($commandsList = $this->getCore()->getCacheManager()->get('berlioz-clicore-commands'))) {
return $commandsList;
}

$commandsList = $this->createCommandsFromConfig($this->getCore()->getConfig());

// Save commands list in cache
$this->getCore()->getCacheManager()->set('berlioz-clicore-commands', $commandsList);

return $commandsList;
}

///////////////
/// HANDLER ///
///////////////

/**
* Handle.
*
* @param array|string|Argument|null $arguments
* @param array|null $argv
*
* @return int
* @throws CommandException
* @throws ArgumentException
* @throws InvalidArgumentException
* @throws Throwable
* @throws CliException
*/
public function handle($arguments = null): int
public function handle(?array $argv = null): int
{
$getOpt = new GetOpt();
$getOpt->addOption(Option::create(null, 'help')->setDescription('Shows this help'));
$getOpt->addCommands($this->getCommands());
$getOpt->process($arguments);

if ($getOpt->getOption('help') || is_null($command = $getOpt->getCommand())) {
print $getOpt->getHelpText();
return 0;
}

// Create instance of command and invoke method
try {
$commandActivity = (new Debug\Activity('Command'))->start();

// Create instance of command
/** @var CommandInterface $commandObj */
$commandObj =
$this
->getCore()
->getServiceContainer()
->getInstantiator()
->newInstanceOf($command->getHandler());
/** @var CommandHandler $commandHandler */
$commandHandler = $this->get(CommandHandler::class);

// Run command
return $commandObj->run($getOpt);
} finally {
$this->getCore()->getDebug()->getTimeLine()->addActivity($commandActivity->end());
}
return $commandHandler->handle($argv);
}
}
Loading

0 comments on commit db5e66b

Please sign in to comment.