Skip to content

Commit 6fd69bc

Browse files
committed
Initial commit
1 parent 8f96a6b commit 6fd69bc

File tree

9 files changed

+205
-0
lines changed

9 files changed

+205
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# IDE
2+
.idea
3+
4+
# PHP
5+
vendor

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Kristoffer Högberg
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Console Skeleton
2+
================
3+
4+
Skeleton for a console application, including:
5+
* DI-Container
6+
* Services and commands [autowired in yaml-config](config/services.yaml)
7+
* [Example Command](src/Command/ExampleCommand.php)
8+
* [Example test](tests/Command/ExampleCommandTest.php)
9+
10+
## Getting started
11+
12+
This will create a new project with the skeleton for a console application:
13+
14+
```bash
15+
composer create-project hmazter/console-skeleton the-new-app-name
16+
cd the-new-app-name
17+
```
18+
19+
The skeleton can then be executed with:
20+
```bash
21+
./app
22+
```
23+
24+
Tests can be run with:
25+
```bash
26+
composer test
27+
```
28+
29+
Go ahead and:
30+
* Edit/replace command in src/Commad/ExampleCommand
31+
* Add more commands in src/Command
32+
* Add additional classed that you need in src/
33+
* Edit/replace the test in tests/Command/ExampleCommandTest

app

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__ . '/vendor/autoload.php';
5+
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10+
11+
// Load services and commands from services.yaml
12+
$container = new ContainerBuilder();
13+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/config'));
14+
$loader->load('services.yaml');
15+
16+
// compile the DI container
17+
$container->compile();
18+
19+
// create new console application
20+
$application = new Application();
21+
22+
// add all services from the container that are tagged "console.command" as commands in the app
23+
$consoleCommands = $container->findTaggedServiceIds('console.command');
24+
foreach ($consoleCommands as $consoleCommand => $tags) {
25+
$application->add($container->get($consoleCommand));
26+
}
27+
28+
$application->run();

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "hmazter/console-skeleton",
3+
"description": "Skeleton for a console application with DI-container and yaml-config",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Kristoffer Högberg",
9+
"email": "[email protected]",
10+
"homepage": "https://hmazter.com"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.2",
15+
"symfony/console": "^4.0",
16+
"symfony/dependency-injection": "^4.0",
17+
"symfony/config": "^4.0",
18+
"symfony/yaml": "^4.0"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "^7.0",
22+
"symfony/var-dumper": "^4.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"App\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"Tests\\": "tests/"
32+
}
33+
},
34+
"scripts": {
35+
"test": "./vendor/bin/phpunit"
36+
}
37+
}

config/services.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#parameters:
2+
3+
services:
4+
# Autowire all services in the app except Commands
5+
App\:
6+
resource: '../src/*'
7+
exclude: '../src/Command'
8+
autowire: true
9+
public: false
10+
11+
# Autowire all Commands in the app, and make them public to allow for registering in the application
12+
App\Command\:
13+
resource: '../src/Command'
14+
autowire: true
15+
public: true
16+
tags: [console.command]

phpunit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="application">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Command/ExampleCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Command;
5+
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class ExampleCommand extends Command
11+
{
12+
public function __construct()
13+
{
14+
parent::__construct();
15+
16+
// TODO: inject and initialize any services needed for the command here
17+
}
18+
19+
protected function configure()
20+
{
21+
$this->setName('example')
22+
->setDescription('Example command');
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output)
26+
{
27+
$output->writeln('This is an example, replace or edit me');
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tests\Command;
5+
6+
use App\Command\ExampleCommand;
7+
use App\Pwned\PwnedPasswords;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Console\Application;
10+
use Symfony\Component\Console\Tester\CommandTester;
11+
12+
class ExampleCommandTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function command_outputs_info()
18+
{
19+
$application = new Application();
20+
$application->add(new ExampleCommand());
21+
22+
$command = $application->find('example');
23+
$commandTester = new CommandTester($command);
24+
$commandTester->execute(['command' => $command->getName()]);
25+
26+
$output = $commandTester->getDisplay();
27+
$this->assertContains('This is an example', $output);
28+
}
29+
}

0 commit comments

Comments
 (0)