未检测到第三方(供应商)Symfony Bundle中的命令。怎么了?

问题描述 投票:0回答:1

按照https://github.com/glennthehuman/encryption-bundle/blob/master/Resources/doc/index.md中的指示

我执行了:php bin/console jagilpe:encryption:user:generate_keys但是我得到了:

“ jagilpe:encryption:user”名称空间中没有定义命令。

所以,我检查了此folder structure使用代码:

<?php

namespace Jagilpe\EncryptionBundle\Command;

use Doctrine\Common\Util\ClassUtils;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Jagilpe\EncryptionBundle\Entity\PKEncryptionEnabledUserInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class CreateUserKeysCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('jagilpe:encryption:user:generate_keys')
            ->setDescription('Generates the encryption keys of a user')
            ->addArgument(
                'usename',
                InputArgument::OPTIONAL,
                'The name of the user whose keys we want to create.'
            )
            ->addOption(
                'all',
                null,
                InputOption::VALUE_NONE,
                'If the keys of all users should be generated.'
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Input parameters
        $userName = $input->getArgument('usename');
        $allUsers = $input->getOption('all');

        if (!$userName && !$allUsers) {
            throw new \RuntimeException('Wrong parameters given');
        }

        if ($userName && $allUsers) {
            throw new \RuntimeException('Ambiguous parameters given');
        }

        $users = $this->getUsers($userName);

        $total = count($users);
        $message = "Generating the encryption keys for $total users";
        $output->writeln($message);
        $progress = new ProgressBar($output, $total);
        foreach ($users as $user) {
            $this->generateKeys($user);
            $this->saveUser($user);
            $progress->advance();
        }
        $progress->finish();
        $output->writeln('');
    }

    private function getUsers($userName)
    {
        $container = $this->getContainer();
        $entityManager = $container->get('doctrine')->getManager();
        $encryptionSettings = $container->getParameter('jagilpe_encryption.settings');
        $userClasses = $encryptionSettings['user_classes'];

        $users = array();
        foreach ($userClasses as $userClass) {
            $userRepo = $entityManager->getRepository($userClass);

            if ($userName) {
                $user = $userRepo->findOneBy(array('username' => $userName));
                $users = array($user);
                break;
            }
            else {
                $users = array_merge($users, $userRepo->findAll());
            }
        }

        return $users;
    }

    private function generateKeys(PKEncryptionEnabledUserInterface $user)
    {
        if (!$user->getPublicKey() || !$user->getPrivateKey()) {
            $container = $this->getContainer();
            $keyManager = $container->get('jagilpe_encryption.key_manager');
            $keyManager->generateUserPKIKeys($user);
        }
    }

    private function saveUser(PKEncryptionEnabledUserInterface $user)
    {
        $userClass = ClassUtils::getClass($user);
        $userRepo = $this->getContainer()->get('doctrine')->getManager()->getRepository($userClass);
        $userRepo->save($user);
    }
}

这怎么了?

顺便说一句,我能够毫无问题地安装捆绑软件。可以正确访问源代码并在我自己的代码中使用。我只是无法正确运行上述命令。我还在自己的Command目录中创建了自己的命令,并且可以正确检测和执行它们。

php symfony command symfony4 symfony-2.1
1个回答
0
投票

这个概念帮助我从symfony 4. *中的第三方供应商那里获得了有效的命令。

只需在services.yaml文件中注册所需的命令,就可以开始使用。

Jagilpe\EncryptionBundle\Command\CreateUserKeysCommand:
    tags:
        - { name: 'console.command', command: 'jagilpe:encryption:user:generate_keys' }
© www.soinside.com 2019 - 2024. All rights reserved.