Skip to content

PHP Cody Tutorial Exercise I

Alexander Miertsch edited this page Feb 8, 2021 · 11 revisions

In the tutorial preparation we've set up folder structure, exercises, InspectIO tutorial board and Cody. Your folder structure should look like this:

cody-tutorial
|__ php-iio-cody-tutorial
|__ php-inspectio-cody

The Cody Server should be running and listen on http://localhost:3311:

cd cody-tutorial/php-inspectio-cody
docker-compose ps

         Name                        Command               State           Ports         
-----------------------------------------------------------------------------------------
iio_iio-ic-react-http_1   docker-php-entrypoint vend ...   Up      0.0.0.0:3311->8080/tcp

Note: Use dev.sh to start the Cody server. After that you can use docker-compose commands to stop, start again, inspect logs, etc. A file watcher restarts the server on changes. If something does not work as expected try to restart the server manually and take a look at the logs: docker-compose stop && docker-compose up -d && docker-compose logs -f

Test-Driven Exercises

Navigate to the tutorial project in a second console cd cody-tutorial/php-iio-cody-tutorial and run the first exercise using docker-compose run --rm composer exercise1

Of course the test case is failing. it is looking for a Command called RegisterUser and that's the first class we want to generate from an InspectIO event map. You might have noticed the commented hooks in codyconfig.php. What we need is a CommandHook, so let's create one:

# current dir: cody-tutorial/php-inspectio-cody
# Create a Hook folder
mkdir src/Hook
# Create CommandHook.php
touch src/Hook/CommandHook.php

Open src/Hook/CommandHook.php in your favorite editor and copy this content into it:

<?php
declare(strict_types=1);

namespace EventEngine\InspectioCody\Hook;

use EventEngine\InspectioCody\Board\BaseHook;
use EventEngine\InspectioCody\Http\Message\CodyResponse;
use EventEngine\InspectioCody\Http\Message\Response;
use EventEngine\InspectioGraphCody\Node;
use stdClass;

final class CommandHook extends BaseHook //BaseHook provides some helper methods like writeFile()
{
    /**
     * @param  Node     $command Information about Command sticky received from InspectIO event map
     * @param  stdClass $context Context object populated in codyconfig.php
     * @return CodyResponse      Response sent back to InspectIO, shown in Cody Console
     */
    public function __invoke(Node $command, stdClass $context): CodyResponse
    {
        $commandName = $command->name();
        $commandFile = $commandName . '.php';
        $commandPath = $context->path . '/Command/' . $commandFile;

        $code = <<<CODE
        <?php
        declare(strict_types=1);
        
        namespace Cody\Tutorial\Command;
        
        class $commandName
        {
            
        }
        CODE;

        $this->writeFile($code, $commandPath);

        return Response::fromCody(
            "Command \"{$commandName}\" generated", 
            ["Command written to {$commandPath}"]
        );
    }
}
Clone this wiki locally