zfcampus / zf-console

Create console applications in PHP
BSD 3-Clause "New" or "Revised" License
64 stars 25 forks source link

Please add SIMPLE documentation how to get it running #38

Open kaurov opened 7 years ago

kaurov commented 7 years ago

I've read your HUGE and SOFISTICATED instruction on how to install the app... and I did not found how to RUN it. Can you please add this in the 1st paragraph?

jcaillot commented 7 years ago

zf-console is not that kind of module you need to get wired and configured. You should better think of it as a standalone micro framework or let's say a... php script. Here is my simplified but hopefully complete demo setup :

bin/zf-cons.php

<?php
require_once __DIR__. '/../vendor/autoload.php';

use Zend\Console\Console;
use ZF\Console\Application;

$application = new Application(
    "ZF-Console setup",   "1.0",                                   
    include __DIR__ . '/zf-cons-routes.php',        
    Console::getInstance()
);

$exit = $application->run();
exit($exit);

bin/zf-cons-routes.php

<?php

use Zend\Console\Adapter\AdapterInterface;
use Zend\Console\ColorInterface;
use ZF\Console\Route;

return array(

    // first route
    // example usage: $ php bin/zf-cons.php hello Ato
    [

        'name'              => 'hello [<name>]',
        'description'       => 'Show welcome message',
        'short_description' => 'Show welcome message',
        'defaults'          => [
                        'name' => 'World',
        ],
        'handler'           => function(Route $route, AdapterInterface $console) {
            $name = $route->getMatchedParam('name');
            $console->write("Hello, ");
            $console->write("$name", ColorInterface::LIGHT_RED);
            $console->writeLine("!");
        },
    ],
    // second route
    // example usage: $ php bin/zf-cons.php demo foo
    [

        'name'              => 'demo [<param1>] [<param2>]',
        'description'       => 'just a demo with an external controller handler',
        'short_description' => 'just a demo',
        'defaults'          =>  ['param1' => 'nope'],
        'handler'           =>  'bin\Command\DemoCommand'

    ],

);

bin/Command/DemoCommand.php

<?php

namespace bin\Command;

use Zend\Console\Adapter\AdapterInterface;
use Zend\Console\ColorInterface;
use ZF\Console\Route;

class DemoCommand
{
    public function __invoke(Route $route, AdapterInterface $console)
    {
        $param1 = $route->getMatchedParam('param1');
        $param2 = $route->getMatchedParam('param2');

        $console->write("This is a demo console command, first param was: ");
        $console->write("$param1", ColorInterface::LIGHT_GREEN);
        $console->writeLine(".");
        $console->write("Second param was: ");
        $console->write("$param2", ColorInterface::LIGHT_GREEN);
        $console->writeLine(".");

    }
}

(composer dump-autoload, for everything to be found)

USAGE:

php bin/zf-cons.php hello hatchi or php bin/zf-cons.php demo hatchi pishi

next (important) step will be to get the ZF3 instanciated into this script. The trick is to call its Zend\Mvc\Application::init method but NOT run. Hope this helps !

ArnaudLigny commented 7 years ago

Some examples here: #33.

janithl commented 6 years ago

Please add @jcaillot's example to the docs, it's a lifesaver.