无法让 Symfony 3 控制台工作 - 自动加载问题

Can't get Symfony 3 Console to work - Autoloading Problems

我需要有关 Symfony 3 控制台组件的帮助。

我无法让它工作,我的自定义总是出现致命错误 类。

这里是代码:

clhelper.php

#!/usr/bin/env php
<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;
use CLHelper\FilesSorterCommand;

$application = new Application();
$application->add(new FilesSorterCommand());
$application->run();

FilesSorterCommand.php

namespace CLHelper;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Files Sorter
 */
class FilesSorterCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('files:sort')
            ->setDescription('Sortiert Dateien mit bestimmter Endung.')
            ->addArgument(
                'extension',
                InputArgument::REQUIRED,
                'Welche Dateiendung?'
            )
            ->addArgument(
                'folder',
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
                'Welche Ordner (Mehrfachangabe möglich)'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $extension = $input->getArgument('extension');

        $output->writeln($extension);
    }
}

如果我尝试使用 php clhelper.php 运行 这个 CL 应用程序,我会收到错误消息:

PHP Fatal error: Class 'CLHelper\FilesSorterCommand' not found in /Users/xyz/Sites/Symfony/clhelper/clhelper.php on line 10

Fatal error: Class 'CLHelper\FilesSorterCommand' not found in /Users/xyz/Sites/Symfony/clhelper/clhelper.php on line 10

在@cerad 发表评论后,我成功地将其添加到我的 composer.json 中:

"autoload": {
    "psr-4": {
        "CLHelper\": ""
    }
}

现在可以正常使用了。