Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Console\CommandLoader;
13
 
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Exception\CommandNotFoundException;
16
 
17
/**
18
 * A simple command loader using factories to instantiate commands lazily.
19
 *
20
 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
21
 */
22
class FactoryCommandLoader implements CommandLoaderInterface
23
{
24
    private array $factories;
25
 
26
    /**
27
     * @param callable[] $factories Indexed by command names
28
     */
29
    public function __construct(array $factories)
30
    {
31
        $this->factories = $factories;
32
    }
33
 
34
    public function has(string $name): bool
35
    {
36
        return isset($this->factories[$name]);
37
    }
38
 
39
    public function get(string $name): Command
40
    {
41
        if (!isset($this->factories[$name])) {
42
            throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name));
43
        }
44
 
45
        $factory = $this->factories[$name];
46
 
47
        return $factory();
48
    }
49
 
50
    public function getNames(): array
51
    {
52
        return array_keys($this->factories);
53
    }
54
}