addArgument example

private readonly BundleConfigGeneratorInterface $bundleDumper,
        private readonly string $projectDir
    ) {
        parent::__construct();
    }

    /** * {@inheritdoc} */
    protected function configure(): void
    {
        $this->addArgument('dumpFilePath', InputArgument::OPTIONAL, 'By default to var/plugins.json', 'var/plugins.json');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $config = $this->bundleDumper->getConfig();

        \file_put_contents(
            $this->projectDir . '/' . $input->getArgument('dumpFilePath'),
            \json_encode($config, \JSON_PRETTY_PRINT)
        );

        
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

$container = new ContainerBuilder();
$container->
    register('foo', 'FooClass')->
    addArgument(new Reference('bar'))
    ->setPublic(true)
;
$container->
    register('bar', 'BarClass')
    ->setPublic(true)
;
$container->compile();

return $container;

#[Package('core')] class ResolveAppUrlChangeCommand extends Command
{
    public function __construct(private readonly Resolver $appUrlChangeResolver)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->addArgument('strategy', InputArgument::OPTIONAL, 'The strategy that should be applied');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new ShopwareStyle($input$output);

        $availableStrategies = $this->appUrlChangeResolver->getAvailableStrategies();
        $strategy = $input->getArgument('strategy');

        if ($strategy === null || !\array_key_exists($strategy$availableStrategies)) {
            if ($strategy !== null) {
                
#[AsCommand(name: 'debug:serializer', description: 'Display serialization information for classes')] class DebugCommand extends Command
{
    public function __construct(private readonly ClassMetadataFactoryInterface $serializer)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('class', InputArgument::REQUIRED, 'A fully qualified class name')
            ->setHelp("The <info>%command.name% 'App\Entity\Dummy'</info> command dumps the serializer groups for the dummy class.")
        ;
    }

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

        if (!class_exists($class)) {
            $io = new SymfonyStyle($input$output);
            $io->error(sprintf('Class "%s" was not found.', $class));

            
$this->assertEquals([new DummyService()]$resolver->resolve($request$argument));
    }

    public function testErrorIsTruncated()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.');
        $container = new ContainerBuilder();
        $container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());

        $container->register('argument_resolver.service', ServiceValueResolver::class)->addArgument(null)->setPublic(true);
        $container->register(DummyController::class)->addTag('controller.service_arguments')->setPublic(true);

        $container->compile();

        $request = $this->requestWithAttributes(['_controller' => [DummyController::class, 'index']]);
        $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
        $container->get('argument_resolver.service')->resolve($request$argument)->current();
    }

    private function requestWithAttributes(array $attributes)
    {
        
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;

class TestServiceContainerRefPassesTest extends TestCase
{
    public function testProcess()
    {
        $container = new ContainerBuilder();
        $container->register('test.private_services_locator', ServiceLocator::class)
            ->setPublic(true)
            ->addArgument(0, []);

        $container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
        $container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);

        $container->register('Test\public_service')
            ->setPublic(true)
            ->addArgument(new Reference('Test\private_used_shared_service'))
            ->addArgument(new Reference('Test\private_used_non_shared_service'))
            ->addArgument(new Reference('Test\soon_private_service'))
        ;

        
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class GenerateMigrationCommand extends ShopwareCommand
{
    public function configure(): void
    {
        $this
            ->addOption('plugin', 'p', InputOption::VALUE_OPTIONAL, 'Plugin Name')
            ->addArgument('migrationName', InputArgument::REQUIRED, 'Migration Name')
            ->setDescription('Generates a migration file for the core or for a specific plugin');
    }

    /** * {@inheritdoc} */
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $pluginName = $input->getOption('plugin');
        $migrationName = $input->getArgument('migrationName');
        $migrationDirectory = $this->findMigrationDirectory($pluginName);

        
return [];
    }

    /** * {@inheritdoc} */
    protected function configure()
    {
        $this
            ->setName('sw:settings:label:find:missing')
            ->setDescription('Dump missing settings labels from the database into php arrays files')
            ->addArgument(
                'locale',
                InputArgument::REQUIRED,
                'Locale to be exported.'
            )
            ->addOption(
                'target',
                null,
                InputOption::VALUE_REQUIRED,
                'The folder where the exported files should be placed. Defaults to Shopware\'s root folder',
                null
            );
    }
public function __construct(
        private readonly KernelPluginCollection $kernelPluginCollection,
        private readonly string $coreDir,
        private readonly string $shopwareVersion
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('directory', InputArgument::OPTIONAL)
            ->addArgument('namespace', InputArgument::OPTIONAL)
            ->addOption('plugin', 'p', InputOption::VALUE_REQUIRED)
            ->addOption(
                'name',
                '',
                InputOption::VALUE_REQUIRED,
                'An optional descriptive name for the migration which will be used as a suffix for the filename.'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
return [];
    }

    /** * {@inheritdoc} */
    protected function configure()
    {
        $this
            ->setName('sw:es:analyze')
            ->setDescription('Helper tool to test own analyzers.')
            ->addArgument('shopId', InputOption::VALUE_REQUIRED, '', '1')
            ->addArgument('type', InputOption::VALUE_REQUIRED, 'Mapping type of the elasticsearch index (e.g. product, property)')
            ->addArgument('analyzer', InputOption::VALUE_REQUIRED)
            ->addArgument('query', InputOption::VALUE_REQUIRED)
        ;
    }

    /** * {@inheritdoc} */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        
$this->assertEquals($command$ret, '->setDefinition() implements a fluent interface');
        $this->assertEquals($definition$command->getDefinition(), '->setDefinition() sets the current InputDefinition instance');
        $command->setDefinition([new InputArgument('foo')new InputOption('bar')]);
        $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
        $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument');
        $command->setDefinition(new InputDefinition());
    }

    public function testAddArgument()
    {
        $command = new \TestCommand();
        $ret = $command->addArgument('foo');
        $this->assertEquals($command$ret, '->addArgument() implements a fluent interface');
        $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command');
    }

    public function testAddArgumentFull()
    {
        $command = new \TestCommand();
        $command->addArgument('foo', InputArgument::OPTIONAL, 'Description', 'default', ['a', 'b']);
        $argument = $command->getDefinition()->getArgument('foo');
        $this->assertSame('Description', $argument->getDescription());
        $this->assertSame('default', $argument->getDefault());
        
$decoratedService = $definition->getDecoratedService();
            if (null === $decoratedService) {
                $def->setDecoratedService($decoratedService);
            } else {
                $def->setDecoratedService($decoratedService[0]$decoratedService[1]$decoratedService[2]$decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
            }
        }

        // merge arguments         foreach ($definition->getArguments() as $k => $v) {
            if (is_numeric($k)) {
                $def->addArgument($v);
            } elseif (str_starts_with($k, 'index_')) {
                $def->replaceArgument((int) substr($k, \strlen('index_'))$v);
            } else {
                $def->setArgument($k$v);
            }
        }

        // merge properties         foreach ($definition->getProperties() as $k => $v) {
            $def->setProperty($k$v);
        }

        


    /** * {@inheritdoc} */
    protected function configure()
    {
        $this
            ->setName('sw:media:optimize')
            ->setHelp('The <info>%command.name%</info> optimizes your uploaded media using external tools. You can check the availability using the <info>--info</info> option.')
            ->setDescription('Optimize uploaded media without quality loss.')
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to your media folder', null)
            ->addOption('info', 'i', InputOption::VALUE_NONE, 'Display available tools')
            ->addOption('skip-scan', null, InputOption::VALUE_NONE, 'Skips the initial filesystem scan.')
            ->addOption('modified', 'm', InputOption::VALUE_REQUIRED, 'Limits the files modify date to the provided time string.')
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force optimization of files on remote file system adapters without asking first')
        ;
    }

    /** * {@inheritdoc} */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigSetCommand extends ShopwareCommand
{
    protected function configure(): void
    {
        $this
            ->setName('sw:config:set')
            ->addOption('shopId', null, InputOption::VALUE_OPTIONAL, 'If provided, the configuration will be set for the specified shop. Otherwise, it will be set for the default shop.')
            ->addOption('decode', 'd', InputOption::VALUE_NONE, 'If provided, the input value will be interpreted as JSON. Use this option to provide values as boolean, integer or float.')
            ->addArgument('name', InputArgument::REQUIRED, 'The name of the configuration element.')
            ->addArgument('value', InputArgument::REQUIRED, 'The new value for the specified configuration element.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $modelManager = $this->container->get('models');
        $shopRepository = $modelManager->getRepository(Shop::class);

        $shopId = (int) $input->getOption('shopId');
        $name = (string) $input->getArgument('name');
        
$this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');

        $def->setDecoratedService('foo', 'foo');
    }

    public function testArguments()
    {
        $def = new Definition('stdClass');
        $this->assertSame($def$def->setArguments(['foo']), '->setArguments() implements a fluent interface');
        $this->assertEquals(['foo']$def->getArguments(), '->getArguments() returns the arguments');
        $this->assertSame($def$def->addArgument('bar'), '->addArgument() implements a fluent interface');
        $this->assertEquals(['foo', 'bar']$def->getArguments(), '->addArgument() adds an argument');
    }

    public function testMethodCalls()
    {
        $def = new Definition('stdClass');
        $this->assertSame($def$def->setMethodCalls([['foo', ['foo']]]), '->setMethodCalls() implements a fluent interface');
        $this->assertEquals([['foo', ['foo']]]$def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
        $this->assertSame($def$def->addMethodCall('bar', ['bar']), '->addMethodCall() implements a fluent interface');
        $this->assertEquals([['foo', ['foo']]['bar', ['bar']]]$def->getMethodCalls(), '->addMethodCall() adds a method to call');
        $this->assertSame($def$def->addMethodCall('foobar', ['foobar'], true), '->addMethodCall() implements a fluent interface with third parameter');
        
Home | Imprint | This part of the site doesn't use cookies.