CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.4 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.4
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
    • Command
    • Console
      • Command
      • Exception
      • Helper
      • TestSuite
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class CommandCollection

Collection for Commands.

Used by Applications to specify their console commands. CakePHP will use the mapped commands to construct and dispatch shell commands.

Namespace: Cake\Console

Property Summary

  • $commands protected
    array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>>

    Command list

Method Summary

  • __construct() public

    Constructor

  • add() public

    Add a command to the collection

  • addMany() public

    Add multiple commands at once.

  • autoDiscover() public

    Automatically discover commands in CakePHP, the application and all plugins.

  • count() public

    Implementation of Countable.

  • discoverDirectory() public

    Auto-discover commands in a filesystem directory.

  • discoverPlugin() public

    Auto-discover commands from the named plugin.

  • get() public

    Get the target for a command.

  • getIterator() public

    Implementation of IteratorAggregate.

  • has() public

    Check whether the named shell exists in the collection.

  • keys() public

    Get the list of available command names.

  • remove() public

    Remove a command from the collection if it exists.

  • replace() public

    Replace a command from the collection with another command if it exists.

  • resolveNames() protected

    Resolve names based on existing commands

Method Detail

__construct() ¶ public

__construct(array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>> $commands = [])

Constructor

Parameters
array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>> $commands optional

The map of commands to add to the collection.

add() ¶ public

add(string $name, Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface> $command): $this

Add a command to the collection

Parameters
string $name

The name of the command you want to map.

Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface> $command

The command to map. Can be a FQCN or CommandInterface instance.

Returns
$this
Throws
InvalidArgumentException

addMany() ¶ public

addMany(array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>> $commands): $this

Add multiple commands at once.

Parameters
array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>> $commands

A map of command names => command classes/instances.

Returns
$this
See Also
\Cake\Console\CommandCollection::add()

autoDiscover() ¶ public

autoDiscover(): array<string, class-string<Cake\Console\CommandInterface>>

Automatically discover commands in CakePHP, the application and all plugins.

Commands will be located using filesystem conventions. Commands are discovered in the following order:

  • CakePHP provided commands
  • Application commands

Commands defined in the application will overwrite commands with the same name provided by CakePHP.

Returns
array<string, class-string<Cake\Console\CommandInterface>>

An array of command names and their classes.

count() ¶ public

count(): int

Implementation of Countable.

Get the number of commands in the collection.

Returns
int

discoverDirectory() ¶ public

discoverDirectory(string $path, string $namespace, string $prefix): array<string, class-string<Cake\Console\CommandInterface>>

Auto-discover commands in a filesystem directory.

Useful for registering commands that live in a subdirectory and namespace of the application (e.g. src/Command/Maintenance/) without listing each command individually, typically from Application::console():

$commands->addMany($commands->discoverDirectory(
    ROOT . '/src/Command/Maintenance',
    'App\\Command\\Maintenance',
    'maintenance ',
));

The directory is scanned non-recursively. A non-empty $prefix is required. The prefix produces a prefix.command long name so discovered commands whose short name collides with an existing command is exposed only under its prefixed name, and does not replace an existing command. The prefixed long name itself follows the same last-wins precedence as discoverPlugin() / autoDiscover(). The path and namespace are normalized, so a trailing separator is optional.

Parameters
string $path

The directory to scan.

string $namespace

The namespace the commands live in.

string $prefix

Prefix applied to each command's full name.

Returns
array<string, class-string<Cake\Console\CommandInterface>>

Discovered commands.

Throws
InvalidArgumentException
When `$prefix` is empty.

discoverPlugin() ¶ public

discoverPlugin(string $plugin): array<string, class-string<Cake\Console\CommandInterface>>

Auto-discover commands from the named plugin.

Discovered commands will have their names de-duplicated with existing commands in the collection. If a command is already defined in the collection and discovered in a plugin, only the long name (plugin.command) will be returned.

Parameters
string $plugin

The plugin to scan.

Returns
array<string, class-string<Cake\Console\CommandInterface>>

Discovered plugin commands.

get() ¶ public

get(string $name): Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>

Get the target for a command.

Parameters
string $name

The named shell.

Returns
Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>

Either the command class or an instance.

Throws
InvalidArgumentException
when unknown commands are fetched.

getIterator() ¶ public

getIterator(): Traversable<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>>

Implementation of IteratorAggregate.

Returns
Traversable<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>>

has() ¶ public

has(string $name): bool

Check whether the named shell exists in the collection.

Parameters
string $name

The named shell.

Returns
bool

keys() ¶ public

keys(): array<string>

Get the list of available command names.

Returns
array<string>

Command names

remove() ¶ public

remove(string $name): $this

Remove a command from the collection if it exists.

Parameters
string $name

The named shell.

Returns
$this

replace() ¶ public

replace(string $oldName, string $newName, Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface> $command): $this

Replace a command from the collection with another command if it exists.

Parameters
string $oldName

Name of command to remove.

string $newName

The name of the command you want to map.

Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface> $command

The command to map. Can be a FQCN or CommandInterface instance.

Returns
$this

resolveNames() ¶ protected

resolveNames(array<array<string, string>> $input): array<string, class-string<Cake\Console\CommandInterface>>

Resolve names based on existing commands

Parameters
array<array<string, string>> $input

The results of a CommandScanner operation.

Returns
array<string, class-string<Cake\Console\CommandInterface>>

A flat map of command names => class names.

Property Detail

$commands ¶ protected

Command list

Type
array<string, \Cake\Console\CommandInterface|class-string<Cake\Console\CommandInterface>>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs