Coder Social home page Coder Social logo

elbakerino / console-di-annotations Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 27 KB

PHP Console App: auto-config skeleton with Dependency Injection, Annotations.

License: MIT License

PHP 92.37% Dockerfile 7.59% Shell 0.04%
php php-skeleton php-cli php-console php-di php-annotation

console-di-annotations's Introduction

PHP Console with DI and Annotations

Build PHP Console applications with Dependency Injection and Annotations.

Setup app skeleton and install dependencies with composer:

composer create-project elbakerino/console

That's it - ready to code!

Run demo commands:

# get help
php cli -h

# how to access operands, see `Commands\Demo->handle`    
php cli demo
php cli demo World
php cli demo:welcome
php cli demo:bye

For Docker, download repository and spin up services:

git clone https://github.com/elbakerino/console-di-annotations.git

docker-compose up

# open second terminal:
# open `bash` in docker service `app` with user `www-data`
docker-compose exec -u www-data app bash
composer install

Command Setup

At _commands.php demo GetOpt-PHP commands are registered with inline documentation.

Here also the commands are registered for found annotations.

For only manual command, it's as short as:

<?php
return static function(GetOpt\GetOpt $get_opt) {
    // already dependency injected!

    $get_opt->addCommand(new GetOpt\Command('demo', [Commands\Demo::class, 'handle']));
    //                   new GetOpt\Command(<name>,  <handler>,                     <options>[optional])
};

You can register commands with annotations, like the demo commands (todo: annotation command docs).

See Commands\Demo for a demo class command handler, any callable/resolvable is possible as command handler.

See Commands\DemoMultiple for a demo class command handler which uses annotations at method level, not class level.

Schematic use of defined commands:

php cli <name> <..operand> <..-a=opt>

More Details

In launch.php the whole setup and auto-config is done, see inline-comments for details.

In _definitions.php you can define/overwrite service definitions for PHP-DI.

See Lib\ConsoleApp for the execution of GetOpt, use as base to switch to another console framework.

See Annotations for example annotations, further docs on annotation will follow / can be found below in the libraries links.

Configure Docker in docker-compose.yml. Simply change PHP version, image base and add other PHP extensions in Dockerfile, rebuild image with docker-compose up --build. When changing docker-opcache.ini or docker-vhost.conf a rebuild is needed.

For further details see:

Install any other dependency for your project from packagist.

Todos

There is not really more needed, you can write simple and efficient console apps.

Some nice to haves would be:

  • print line, print success, print error helper functions
  • print in color helper functions
  • input handling helper functions
  • some middleware pipelines around commands
    • time and performance middleware
  • logging with PSR logger (monolog) by default

Downloads

  • See Composer Package.
  • Repository git clone https://github.com/elbakerino/console-di-annotations.git

License

This project is free software distributed under the MIT License.

See: LICENSE.

Contributors

By committing your code to the code repository you agree to release the code under the MIT License attached to the repository.


Author: Michael Becker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.