findNamespace() public method

Finds a registered namespace by a name or an abbreviation.
public findNamespace ( string $namespace ) : string
$namespace string A namespace or abbreviation to search for
return string A registered namespace
 private function inspectApplication()
 {
     $this->commands = array();
     $this->namespaces = array();
     $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
     foreach ($this->sortCommands($all) as $namespace => $commands) {
         $names = array();
         /** @var Command $command */
         foreach ($commands as $name => $command) {
             if (!$command->getName()) {
                 continue;
             }
             if ($command->getName() === $name) {
                 $this->commands[$name] = $command;
             } else {
                 $this->aliases[$name] = $command;
             }
             $names[] = $name;
         }
         $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
     }
 }
コード例 #2
0
 /**
  * @expectedException        Symfony\Component\Console\Exception\CommandNotFoundException
  * @expectedExceptionMessage There are no commands defined in the "bar" namespace.
  */
 public function testFindInvalidNamespace()
 {
     $application = new Application();
     $application->findNamespace('bar');
 }
コード例 #3
0
ファイル: ApplicationTest.php プロジェクト: TeamA-ict/TeamA
 public function testFindNamespace()
 {
     $application = new Application();
     $application->add(new \FooCommand());
     $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
     $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation');
     $application->add(new \Foo2Command());
     $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
     try {
         $application->findNamespace('f');
         $this->fail('->findNamespace() throws an \\InvalidArgumentException if the abbreviation is ambiguous');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->findNamespace() throws an \\InvalidArgumentException if the abbreviation is ambiguous');
         $this->assertEquals('The namespace "f" is ambiguous (foo, foo1).', $e->getMessage(), '->findNamespace() throws an \\InvalidArgumentException if the abbreviation is ambiguous');
     }
     try {
         $application->findNamespace('bar');
         $this->fail('->findNamespace() throws an \\InvalidArgumentException if no command is in the given namespace');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->findNamespace() throws an \\InvalidArgumentException if no command is in the given namespace');
         $this->assertEquals('There are no commands defined in the "bar" namespace.', $e->getMessage(), '->findNamespace() throws an \\InvalidArgumentException if no command is in the given namespace');
     }
 }