Download the PHP package maxkaemmerer/commands without Composer

On this page you can find all versions of the php package maxkaemmerer/commands. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package commands

maxkaemmerer/commands

Travis branch Coveralls github PHP from Packagist Packagist Packagist

Description:

This library offers interfaces and implementations for a simple command, command-handler, command-bus structure.

This is of course not an original idea, but my preferred and fairly simple implementation.

The code is fully tested, I however do not take responsibility for use in production.

Use at your own risk.

Installation:

composer require maxkaemmerer/commands

Usage:

Generally you don't want to register each CommandHandler by hand. You might want to use dependency injection via a container or service-manager.

(An example for the Symfony Framework would be using a CompilerPass)

You would also want to inject the CommandBus itself via dependency injection wherever you need it.

Feel free to create your own implementations of CommandBus and Payload if you require something more advanced.

Register a Command Handler:

Register a CommandHandler to the CommandBus. The CommandHandler's handles() method returns the name of the Command that it handles.

Best practice would be using the fully qualified class name of the command. MyCommand::class

The CommandHandler::handle($command) method is where your actual domain logic happens.

Feel free to inject services, a container, or whatever else you need, into your CommandHandlers.

$commandBus = new SimpleCommandBus();

$commandBus->registerHandler(new class implements CommandHandler
{

    /**
     * @param Command $command
     */
    public function handle(Command $command): void
    {
        echo $command->payload()->get('message');
    }

    /**
     * @return string
     * The name of the command this handler handles. Command::name()
     */
    public function handles(): string
    {
        return 'commandName';
    }

});

Dispatch a Command:

Dispatching a Command causes the CommandBus to look for a CommandHandler who's CommandHandler:handles() method matches the Command's name, specified by Command:name(), and calls it's CommandHandler::handle($command) method.

IMPORTANT: CommandHandler's and the CommandBus never return anything.

...

// The CommandHandler was registered

$commandBus->dispatch(new class implements Command
{

    /**
     * @return CommandPayload
     */
    public function payload(): CommandPayload
    {
        // You would of course set the Payload in the constructor of your Command implementation
        return Payload::fromArray(['message' => 'Hello World!']);
    }

    /**
     * @return string
     * The name of this command, it is recommended to use the fully qualified class name.
     */
    public function name(): string
    {
        return 'commandName';
    }

});

Full Example:

<?php

use MaxKaemmerer\Commands\Command;
use MaxKaemmerer\Commands\CommandHandler;
use MaxKaemmerer\Commands\CommandPayload;
use MaxKaemmerer\Commands\Exception\CommandException;
use MaxKaemmerer\Commands\Implementations\Payload;
use MaxKaemmerer\Commands\Implementations\SimpleCommandBus;

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

try {
    $commandBus = new SimpleCommandBus();

    $commandBus->registerHandler(new class implements CommandHandler
    {

        /**
         * @param Command $command
         */
        public function handle(Command $command): void
        {
            echo $command->payload()->get('message');
        }

        /**
         * @return string
         * The name of the command this handler handles. Command::name()
         */
        public function handles(): string
        {
            return 'commandName';
        }

    });

    $commandBus->dispatch(new class implements Command
    {

        /**
         * @return CommandPayload
         */
        public function payload(): CommandPayload
        {
            // You would of course set the Payload in the constructor of your Command implementation
            return Payload::fromArray(['message' => 'Hello World!']);
        }

        /**
         * @return string
         * The name of this command, it is recommended to use the fully qualified class name.
         */
        public function name(): string
        {
            return 'commandName';
        }

    });

} catch (CommandException $exception) {
    error_log($exception->getMessage());
}

Result: Hello World!


All versions of commands with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package maxkaemmerer/commands contains the following files

Loading the files please wait ....