Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| CommandLoader | |
100.00% |
15 / 15 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
| get_class_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doHas | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doGet | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| doGetNames | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNames | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Command loader for the changelogger tool CLI. |
| 4 | * |
| 5 | * @package automattic/jetpack-changelogger |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Changelogger; |
| 9 | |
| 10 | use Symfony\Component\Console\Command\Command; |
| 11 | use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; |
| 12 | use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 13 | |
| 14 | /** |
| 15 | * Command loader for the changelogger tool CLI. |
| 16 | */ |
| 17 | class CommandLoader implements CommandLoaderInterface { |
| 18 | /** |
| 19 | * Get the class name for a command. |
| 20 | * |
| 21 | * @param string $name Command name. |
| 22 | * @return string Class name. |
| 23 | */ |
| 24 | private function get_class_name( $name ) { |
| 25 | return __NAMESPACE__ . '\\' . ucfirst( $name ) . 'Command'; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Checks if a command exists. |
| 30 | * |
| 31 | * @param string $name Command name. |
| 32 | * @return bool |
| 33 | */ |
| 34 | protected function doHas( $name ) { |
| 35 | return class_exists( $this->get_class_name( $name ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Loads a command. |
| 40 | * |
| 41 | * @param string $name Command name. |
| 42 | * @return Command |
| 43 | * @throws CommandNotFoundException If the command is not found. |
| 44 | */ |
| 45 | protected function doGet( $name ) { |
| 46 | $class = $this->get_class_name( $name ); |
| 47 | if ( ! class_exists( $class ) ) { |
| 48 | throw new CommandNotFoundException( "Command \"$name\" does not exist." ); |
| 49 | } |
| 50 | return new $class(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Return all command names. |
| 55 | * |
| 56 | * @return string[] All registered command names |
| 57 | */ |
| 58 | protected function doGetNames() { |
| 59 | $names = array(); |
| 60 | foreach ( new \DirectoryIterator( __DIR__ ) as $file ) { |
| 61 | if ( substr( $file->getBasename(), -11 ) === 'Command.php' ) { |
| 62 | $names[] = lcfirst( substr( $file->getBasename(), 0, -11 ) ); |
| 63 | } |
| 64 | } |
| 65 | sort( $names ); |
| 66 | return $names; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Checks if a command exists. |
| 71 | * |
| 72 | * @param string $name Command name. |
| 73 | * @return bool |
| 74 | */ |
| 75 | public function has( $name ): bool { |
| 76 | return $this->doHas( $name ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Loads a command. |
| 81 | * |
| 82 | * @param string $name Command name. |
| 83 | * @return Command |
| 84 | * @throws CommandNotFoundException If the command is not found. |
| 85 | */ |
| 86 | public function get( $name ): Command { |
| 87 | return $this->doGet( $name ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Return all command names. |
| 92 | * |
| 93 | * @return string[] All registered command names |
| 94 | */ |
| 95 | public function getNames(): array { |
| 96 | return $this->doGetNames(); |
| 97 | } |
| 98 | } |