Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Cli Laravel Package

laminas/laminas-cli

Console tooling for Laminas applications and components. Provides a CLI entry point, command discovery/registration, and integration helpers to build and run project-specific commands via Composer and your framework configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require laminas/laminas-cli
    

    This adds the vendor/bin/laminas CLI runner to your project.

  2. First Use Case: Run a built-in command (e.g., list to see available commands):

    vendor/bin/laminas list
    

    For Laminas MVC/Mezzio apps, this will display commands registered in your application’s configuration.

  3. Where to Look First:

    • Configuration: Check config/autoload/laminas-cli.global.php (or equivalent) for registered commands.
    • Command Registration: For custom commands, reference the Symfony Console docs and the README’s registration examples.

Implementation Patterns

Core Workflows

  1. Command Registration:

    • Laminas MVC:
      // config/autoload/laminas-cli.global.php
      return [
          'laminas-cli' => [
              'commands' => [
                  'app:custom-command' => \App\Command\CustomCommand::class,
              ],
          ],
          'service_manager' => [
              'factories' => [
                  \App\Command\CustomCommand::class => \App\Factory\CustomCommandFactory::class,
              ],
          ],
      ];
      
    • Mezzio:
      // config/autoload/laminas-cli.global.php
      return [
          'laminas-cli' => [
              'commands' => [
                  'app:custom-command' => \App\Command\CustomCommand::class,
              ],
          ],
          'dependencies' => [
              'factories' => [
                  \App\Command\CustomCommand::class => \App\Factory\CustomCommandFactory::class,
              ],
          ],
      ];
      
  2. Command Execution:

    vendor/bin/laminas app:custom-command [options]
    

    Use --container=<path> to specify a custom container file (e.g., vendor/bin/laminas --container=config/container.php app:custom-command).

  3. Dependency Injection:

    • For commands with dependencies, use a factory (e.g., CustomCommandFactory) to resolve them via the container.
    • Example factory:
      namespace App\Factory;
      use Psr\Container\ContainerInterface;
      use App\Command\CustomCommand;
      
      class CustomCommandFactory
      {
          public function __invoke(ContainerInterface $container): CustomCommand
          {
              return new CustomCommand(
                  $container->get(\App\Service\SomeService::class)
              );
          }
      }
      
  4. Symfony Console Attributes (v1.15.0+): Decorate commands with attributes for metadata (e.g., descriptions, arguments):

    use Symfony\Component\Console\Attribute\AsCommand;
    
    #[AsCommand(name: 'app:custom-command', description: 'Does something awesome')]
    class CustomCommand extends Command
    {
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            // Command logic
        }
    }
    
  5. Integration with Artisan-like Workflows:

    • Use vendor/bin/laminas as a drop-in replacement for php artisan in Laravel projects.
    • Example: Create a bin/console symlink:
      ln -s vendor/bin/laminas bin/console
      

Gotchas and Tips

Pitfalls

  1. Missing --container Flag:

    • If commands fail with InvalidArgumentException, ensure you’re either:
      • Running in a Laminas MVC/Mezzio app (where the container is auto-detected), or
      • Explicitly passing --container=<path> to point to a PSR-11 container file.
    • Fix: Update to laminas-cli v1.1.1+ (includes a bugfix for this).
  2. Command Not Found:

    • If vendor/bin/laminas list shows no commands, verify:
      • The laminas-cli config key exists in your autoload configuration.
      • Commands are registered under the commands key (e.g., 'app:command-name' => \Namespace\Command::class).
    • Fix: Check for typos in command names or missing factory mappings.
  3. Symfony Console Version Mismatch:

    • laminas-cli v1.15.0+ requires Symfony Console v6+. Older versions may break with newer Symfony releases.
    • Fix: Update laminas-cli or pin Symfony Console to a compatible version in composer.json.
  4. Circular Dependencies in Factories:

    • If commands fail to instantiate due to circular dependencies, refactor factories to avoid tight coupling.
    • Tip: Use allow_override: true in config/autoload/global.php for development:
      return [
          'laminas-cli' => [
              'allow_override' => true, // Debugging only!
          ],
      ];
      

Debugging Tips

  1. Verbose Output: Add -v or -vv to commands for debugging:

    vendor/bin/laminas -vv app:custom-command
    
  2. Container Inspection: Dump the container contents to verify service availability:

    // In a command's execute() method:
    $container = $this->getApplication()->getKernel()->getContainer();
    var_dump($container->get(\App\Service\SomeService::class));
    
  3. PSR-11 Container Validation: Ensure your custom container implements Psr\Container\ContainerInterface and handles NotFoundException for missing services.

Extension Points

  1. Dynamic Command Loading: Register commands dynamically via a Laminas\Cli\Command\CommandProviderInterface:

    $provider = new class implements CommandProviderInterface {
        public function getCommands(): array
        {
            return [
                'app:dynamic-command' => \DynamicCommand::class,
            ];
        }
    };
    // Register the provider in your container.
    
  2. Custom Command Helpers: Extend Laminas\Cli\Command\Command for reusable logic:

    abstract class BaseCommand extends Command
    {
        protected function log(string $message): void
        {
            $this->getApplication()->getKernel()->getLogger()->info($message);
        }
    }
    
  3. Environment-Specific Commands: Use config/autoload/{environment}.global.php to conditionally register commands:

    // config/autoload/local.global.php
    return [
        'laminas-cli' => [
            'commands' => [
                'app:dev-only-command' => \DevCommand::class,
            ],
        ],
    ];
    
  4. Integration with Laravel:

    • Override vendor/bin/laminas with a custom script to leverage Laravel’s service container:
      # bin/laminas
      #!/usr/bin/env php
      <?php
      require __DIR__.'/../vendor/autoload.php';
      $container = require __DIR__.'/../bootstrap/app.php';
      $cli = new \Laminas\Cli\Cli($container);
      $cli->run();
      
    • Register commands in Laravel’s AppServiceProvider:
      public function register()
      {
          $this->app->extend('laminas-cli.commands', function ($commands) {
              $commands['app:laravel-command'] = \App\Command\LaravelCommand::class;
              return $commands;
          });
      }
      

Performance Tips

  1. Avoid Overhead in Production: Disable debug mode in config/autoload/global.php:

    return [
        'laminas-cli' => [
            'debug' => false,
        ],
    ];
    
  2. Cache Command Metadata: For large applications, cache command metadata (e.g., descriptions) in a static file to avoid reflection overhead:

    // In a command provider:
    $metadata = require __DIR__.'/../data/command-metadata.php';
    return $metadata['commands'];
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata