getNamespaces example


  protected $namespaces;

  /** * Constructs a QueryFactory object. */
  public function __construct(KeyValueFactoryInterface $key_value_factory) {
    $this->keyValueFactory = $key_value_factory;
    $this->namespaces = Query::getNamespaces($this);
  }

  /** * {@inheritdoc} */
  public function get(EntityTypeInterface $entity_type$conjunction) {
    return new Query($entity_type$conjunction$this->namespaces, $this->keyValueFactory);
  }

  /** * {@inheritdoc} */

  protected $namespaces;

  /** * Constructs a QueryFactory object. * * Initializes the list of namespaces used to locate query * classes for different entity types. */
  public function __construct() {
    $this->namespaces = QueryBase::getNamespaces($this);
  }

  /** * {@inheritdoc} */
  public function get(EntityTypeInterface $entity_type$conjunction) {
    return new Query($entity_type$conjunction$this->namespaces);
  }

  /** * {@inheritdoc} */
$data['application']['name'] = $application->getName();
            if ('UNKNOWN' !== $application->getVersion()) {
                $data['application']['version'] = $application->getVersion();
            }
        }

        $data['commands'] = $commands;

        if ($describedNamespace) {
            $data['namespace'] = $describedNamespace;
        } else {
            $data['namespaces'] = array_values($description->getNamespaces());
        }

        $this->writeData($data$options);
    }

    /** * Writes data as json. */
    private function writeData(array $data, array $options): void
    {
        $flags = $options['json_encoding'] ?? 0;

        

    /** * @dataProvider getNamespacesProvider */
    public function testGetNamespaces(array $expected, array $names)
    {
        $application = new TestApplication();
        foreach ($names as $name) {
            $application->add(new Command($name));
        }

        $this->assertSame($expectedarray_keys((new ApplicationDescription($application))->getNamespaces()));
    }

    public static function getNamespacesProvider()
    {
        return [
            [['_global']['foobar']],
            [['a', 'b']['b:foo', 'a:foo', 'b:bar']],
            [['_global', 22, 33, 'b', 'z']['z:foo', '1', '33:foo', 'b:foo', '22:foo:bar']],
        ];
    }
}

/** * Constructs a PgsqlQueryFactory object. * * @param \Drupal\Core\Database\Connection $connection * The database connection used by the entity query. * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager * The workspace manager. */
  public function __construct(Connection $connection, WorkspaceManagerInterface $workspace_manager) {
    $this->connection = $connection;
    $this->workspaceManager = $workspace_manager;
    $this->namespaces = QueryBase::getNamespaces($this);
  }

  /** * {@inheritdoc} */
  public function get(EntityTypeInterface $entity_type$conjunction) {
    $class = QueryBase::getClass($this->namespaces, 'Query');
    return new $class($entity_type$conjunction$this->connection, $this->namespaces, $this->workspaceManager);
  }

  /** * {@inheritdoc} */


            $this->writeText("<comment>Usage:</comment>\n", $options);
            $this->writeText(" command [options] [arguments]\n\n", $options);

            $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions())$options);

            $this->writeText("\n");
            $this->writeText("\n");

            $commands = $description->getCommands();
            $namespaces = $description->getNamespaces();
            if ($describedNamespace && $namespaces) {
                // make sure all alias commands are included when describing a specific namespace                 $describedNamespaceInfo = reset($namespaces);
                foreach ($describedNamespaceInfo['commands'] as $name) {
                    $commands[$name] = $description->getCommand($name);
                }
            }

            // calculate max. width based on available commands per namespace             $width = $this->getColumnWidth(array_merge(...array_values(array_map(fn ($namespace) => array_intersect($namespace['commands']array_keys($commands))array_values($namespaces)))));

            

class ListCommand extends Command
{
    /** * @return void */
    protected function configure()
    {
        $this
            ->setName('list')
            ->setDefinition([
                new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name', null, fn () => array_keys((new ApplicationDescription($this->getApplication()))->getNamespaces())),
                new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
                new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
                new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
            ])
            ->setDescription('List commands')
            ->setHelp(<<<'EOF' The <info>%command.name%</info> command lists all commands: <info>%command.full_name%</info> You can also display the commands for a specific namespace: <info>%command.full_name% test</info> You can also output the information in other formats by using the <comment>--format</comment> option: <info>%command.full_name% --format=xml</info> It's also possible to get raw list of commands (useful for embedding command runner): <info>%command.full_name% --raw</info>
if ($paths) {
            $io->table(['Namespace', 'Paths']$this->buildTableRows($paths));
        } else {
            $alternatives = [];
            $namespace = $this->parseTemplateName($name)[0];

            if (FilesystemLoader::MAIN_NAMESPACE === $namespace) {
                $message = 'No template paths configured for your application';
            } else {
                $message = sprintf('No template paths configured for "@%s" namespace', $namespace);
                foreach ($this->getFilesystemLoaders() as $loader) {
                    $namespaces = $loader->getNamespaces();
                    foreach ($this->findAlternatives($namespace$namespaces) as $namespace) {
                        $alternatives[] = '@'.$namespace;
                    }
                }
            }

            $this->error($io$message$alternatives);

            if (!$alternatives && $paths = $this->getLoaderPaths()) {
                $io->table(['Namespace', 'Paths']$this->buildTableRows($paths));
            }
        }

    public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
    {
        $path = $this->ensureExt($path$ext);

        $foundPaths = [];
        $appPaths   = [];

        foreach ($this->getNamespaces() as $namespace) {
            if (isset($namespace['path']) && is_file($namespace['path'] . $path)) {
                $fullPath = $namespace['path'] . $path;
                $fullPath = realpath($fullPath) ?: $fullPath;

                if ($prioritizeApp) {
                    $foundPaths[] = $fullPath;
                } elseif (strpos($fullPath, APPPATH) === 0) {
                    $appPaths[] = $fullPath;
                } else {
                    $foundPaths[] = $fullPath;
                }
            }


            $this->writeText("<comment>Usage:</comment>\n", $options);
            $this->writeText(" command [options] [arguments]\n\n", $options);

            $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions())$options);

            $this->writeText("\n");
            $this->writeText("\n");

            $commands = $description->getCommands();
            $namespaces = $description->getNamespaces();
            if ($describedNamespace && $namespaces) {
                // make sure all alias commands are included when describing a specific namespace                 $describedNamespaceInfo = reset($namespaces);
                foreach ($describedNamespaceInfo['commands'] as $name) {
                    $commands[$name] = $description->getCommand($name);
                }
            }

            // calculate max. width based on available commands per namespace             $width = $this->getColumnWidth(array_merge(...array_values(array_map(fn ($namespace) => array_intersect($namespace['commands']array_keys($commands))array_values($namespaces)))));

            
$this->expectException(CommandNotFoundException::class);
        $this->expectExceptionMessage('The command "foofoo" does not exist.');
        $application = new Application();
        $application->get('foofoo');
    }

    public function testGetNamespaces()
    {
        $application = new Application();
        $application->add(new \FooCommand());
        $application->add(new \Foo1Command());
        $this->assertEquals(['foo']$application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces');
    }

    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');
    }

    
$data['application']['name'] = $application->getName();
            if ('UNKNOWN' !== $application->getVersion()) {
                $data['application']['version'] = $application->getVersion();
            }
        }

        $data['commands'] = $commands;

        if ($describedNamespace) {
            $data['namespace'] = $describedNamespace;
        } else {
            $data['namespaces'] = array_values($description->getNamespaces());
        }

        $this->writeData($data$options);
    }

    /** * Writes data as json. */
    private function writeData(array $data, array $options): void
    {
        $flags = $options['json_encoding'] ?? 0;

        

    }

    protected function describeApplication(Application $application, array $options = []): void
    {
        $describedNamespace = $options['namespace'] ?? null;
        $description = new ApplicationDescription($application$describedNamespace);
        $title = $this->getApplicationTitle($application);

        $this->write($title."\n".str_repeat('=', Helper::width($title)));

        foreach ($description->getNamespaces() as $namespace) {
            if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
                $this->write("\n\n");
                $this->write('**'.$namespace['id'].':**');
            }

            $this->write("\n\n");
            $this->write(implode("\n", array_map(fn ($commandName) => sprintf('* [`%s`](#%s)', $commandNamestr_replace(':', '', $description->getCommand($commandName)->getName()))$namespace['commands'])));
        }

        foreach ($description->getCommands() as $command) {
            $this->write("\n\n");
            

class ListCommand extends Command
{
    /** * @return void */
    protected function configure()
    {
        $this
            ->setName('list')
            ->setDefinition([
                new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name', null, fn () => array_keys((new ApplicationDescription($this->getApplication()))->getNamespaces())),
                new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
                new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', fn () => (new DescriptorHelper())->getFormats()),
                new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'),
            ])
            ->setDescription('List commands')
            ->setHelp(<<<'EOF' The <info>%command.name%</info> command lists all commands: <info>%command.full_name%</info> You can also display the commands for a specific namespace: <info>%command.full_name% test</info> You can also output the information in other formats by using the <comment>--format</comment> option: <info>%command.full_name% --format=xml</info> It's also possible to get raw list of commands (useful for embedding command runner): <info>%command.full_name% --raw</info>
return array_values(array_unique(array_filter(array_merge([], ...$namespaces))));
    }

    /** * Finds a registered namespace by a name or an abbreviation. * * @throws NamespaceNotFoundException When namespace is incorrect or ambiguous */
    public function findNamespace(string $namespace): string
    {
        $allNamespaces = $this->getNamespaces();
        $expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*';
        $namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);

        if (empty($namespaces)) {
            $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);

            if ($alternatives = $this->findAlternatives($namespace$allNamespaces)) {
                if (1 == \count($alternatives)) {
                    $message .= "\n\nDid you mean this?\n ";
                } else {
                    $message .= "\n\nDid you mean one of these?\n ";
                }
Home | Imprint | This part of the site doesn't use cookies.