Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Muetze42 authored Dec 10, 2022
0 parents commit c83a2b4
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
indent_size = 4
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

*.blade.php diff=html
*.css diff=css
*.html diff=html
*.md diff=markdown
*.php diff=php

/.github export-ignore
CHANGELOG.md export-ignore
.styleci.yml export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/vendor
/.idea
/.vscode
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Taylor Otwell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Console App Template

Creating Console Applications with [Laravel Console Commands](https://laravel.com/docs/artisan) based on [Symfony Console Commands](https://symfony.com/doc/current/console.html).

---
DONT FORGET TO RENAME `bin/app` TO YOUR APP COMMAND AND CHANGE IT `composer.json`:

```json
{
"bin": [
"bin/app"
]
}
```
11 changes: 11 additions & 0 deletions bin/app
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env php
<?php

$autoloader = file_exists(__DIR__.'/../../../autoload.php') ? __DIR__.'/../../../autoload.php' : __DIR__.'/../vendor/autoload.php';

require $autoloader;

use NormanHuth\ConsoleApp\App;

new App();
//new App('My App', '1.0', 'hello:world');
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "norman-huth/console-app-template",
"license": "MIT",
"autoload": {
"psr-4": {
"NormanHuth\\ConsoleApp\\": "src/"
}
},
"authors": [
{
"name": "Norman Huth",
"homepage": "https://huth.it"
}
],
"support": {
"issues": "https://github.com/Muetze42/console-app-template/issues",
"source": "https://github.com/Muetze42/console-app-template"
},
"require": {
"php": "^8.0",
"illuminate/console": "^9.0",
"illuminate/events": "^9.0"
},
"bin": [
"bin/app"
],
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
92 changes: 92 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace NormanHuth\ConsoleApp;

use Exception;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Console\Application;

class App
{
/**
* The Laravel Container Instance
*
* @var Container
*/
protected Container $container;

/**
* The Laravel Dispatcher Instance
*
* @var Dispatcher
*/
protected Dispatcher $events;

/**
* The Laravel Application Instance
*
* @var Application
*/
protected Application $artisan;

/**
* @param int|string|null $version
* @throws Exception
*/
public function __construct(string $appName = 'Console App', int|string $version = null, string $defaultCommand = null)
{
$this->container = new Container;
$this->events = new Dispatcher($this->container);
$this->artisan = new Application($this->container, $this->events, $this->setVersion($version));

if ($defaultCommand) {
$this->artisan->setDefaultCommand($defaultCommand);
}

$this->artisan->setName($appName);

$this->resolveCommands('Console'.DIRECTORY_SEPARATOR.'Commands');

$this->artisan->run();
}

/**
* Get Version from the composer.json file or set default to 1
*
* @param int|string|null $version
* @return string
*/
protected function setVersion(int|string|null $version): string
{
if (!$version) {
$content = file_get_contents(__DIR__.'/../composer.json');
$data = json_decode($content, true);

$version = data_get($data, 'version', 1);
}

return (string)$version;
}

/**
* Register all the commands in the given directory.
*
* @param string $path
* @return void
*/
protected function resolveCommands(string $path): void
{
$path = trim($path, '/\\');
$items = glob(__DIR__.DIRECTORY_SEPARATOR.$path.'/*.php');
foreach ($items as $item) {
$class = __NAMESPACE__.'\\'.$path.'\\'.pathinfo($item, PATHINFO_FILENAME);
$this->artisan->resolve($class);
}

$directories = glob(__DIR__.DIRECTORY_SEPARATOR.$path.'/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$this->resolveCommands($path.DIRECTORY_SEPARATOR.basename($directory));
}
}
}
35 changes: 35 additions & 0 deletions src/Console/Commands/Example/HelloWorld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace NormanHuth\ConsoleApp\Console\Commands\Example;

use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

class HelloWorld extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'hello:world';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This is a example hello world command';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->comment('Hello World');

return SymfonyCommand::SUCCESS;
}
}

0 comments on commit c83a2b4

Please sign in to comment.