Skip to content

Commit

Permalink
commands as services
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Dec 15, 2017
1 parent b388f7b commit 5cc936f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 18 deletions.
23 changes: 17 additions & 6 deletions Command/MappingDebugClassCommand.php
Expand Up @@ -2,13 +2,23 @@

namespace Vich\UploaderBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MappingDebugClassCommand extends ContainerAwareCommand
class MappingDebugClassCommand extends Command
{
protected static $defaultName = 'vich:mapping:debug-class';

private $metadataReader;

public function __construct(MetadataReader $metadataReader)
{
parent::__construct();
$this->metadataReader = $metadataReader;
}

protected function configure()
{
$this
Expand All @@ -18,22 +28,23 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$metadataReader = $this->getContainer()->get('vich_uploader.metadata_reader');
$fqcn = $input->getArgument('fqcn');

if (!$metadataReader->isUploadable($fqcn)) {
if (!$this->metadataReader->isUploadable($fqcn)) {
$output->writeln(sprintf('<error>"%s" is not uploadable.</error>', $fqcn));

return 1;
}

$uploadableFields = $metadataReader->getUploadableFields($fqcn);
$uploadableFields = $this->metadataReader->getUploadableFields($fqcn);

$output->writeln(sprintf('Introspecting class <info>%s</info>:', $fqcn));
foreach ($uploadableFields as $data) {
$output->writeln(sprintf('Found field "<comment>%s</comment>", storing file name in <comment>"%s</comment>" and using mapping "<comment>%s</comment>"', $data['propertyName'], $data['fileNameProperty'], $data['mapping']));
}

return 0;
}
}
19 changes: 14 additions & 5 deletions Command/MappingDebugCommand.php
Expand Up @@ -2,14 +2,24 @@

namespace Vich\UploaderBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vich\UploaderBundle\Exception\MappingNotFoundException;

class MappingDebugCommand extends ContainerAwareCommand
class MappingDebugCommand extends Command
{
protected static $defaultName = 'vich:mapping:debug';

private $mappings;

public function __construct(array $mappings)
{
parent::__construct();
$this->mappings = $mappings;
}

protected function configure()
{
$this
Expand All @@ -21,16 +31,15 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$mappings = $this->getContainer()->getParameter('vich_uploader.mappings');
$mapping = $input->getArgument('mapping');

if (!isset($mappings[$mapping])) {
if (!isset($this->mappings[$mapping])) {
throw new MappingNotFoundException(sprintf('Mapping "%s" does not exist.', $mapping));
}

$output->writeln(sprintf('Debug information for mapping <info>%s</info>', $mapping));

foreach ($mappings[$mapping] as $key => $value) {
foreach ($this->mappings[$mapping] as $key => $value) {
$output->writeln(sprintf('<comment>%s</comment>: %s', $key, var_export($value, true)));
}
}
Expand Down
18 changes: 14 additions & 4 deletions Command/MappingListClassesCommand.php
Expand Up @@ -2,12 +2,23 @@

namespace Vich\UploaderBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vich\UploaderBundle\Metadata\MetadataReader;

class MappingListClassesCommand extends ContainerAwareCommand
class MappingListClassesCommand extends Command
{
protected static $defaultName = 'vich:mapping:list-classes';

private $metadataReader;

public function __construct(MetadataReader $metadataReader)
{
parent::__construct();
$this->metadataReader = $metadataReader;
}

protected function configure()
{
$this
Expand All @@ -20,8 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Looking for uploadable classes.');

$metadataReader = $this->getContainer()->get('vich_uploader.metadata_reader');
$uploadableClasses = $metadataReader->getUploadableClasses();
$uploadableClasses = $this->metadataReader->getUploadableClasses();

foreach ($uploadableClasses as $class) {
$output->writeln(sprintf('Found <comment>%s</comment>', $class));
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/VichUploaderExtension.php
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;

/**
Expand Down
26 changes: 26 additions & 0 deletions Resources/config/command.xml
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="vich_uploader.command.mapping_debug_class" class="Vich\UploaderBundle\Command\MappingDebugClassCommand" public="false">
<argument type="service" id="vich_uploader.metadata_reader" />
<tag name="console.command" command="vich:mapping:debug-class" />
</service>

<service id="vich_uploader.command.mapping_debug" class="Vich\UploaderBundle\Command\MappingDebugCommand" public="false">
<argument>%vich_uploader.mappings%</argument>
<tag name="console.command" command="vich:mapping:debug" />
</service>

<service id="vich_uploader.command.mapping_list_classes" class="Vich\UploaderBundle\Command\MappingListClassesCommand" public="false">
<argument type="service" id="vich_uploader.metadata_reader" />
<tag name="console.command" command="vich:mapping:list-classes" />
</service>

</services>

</container>
6 changes: 5 additions & 1 deletion composer.json
Expand Up @@ -24,9 +24,12 @@
"behat/transliterator": "^1.2",
"doctrine/annotations": "^1.3",
"jms/metadata": "^1.6",
"symfony/config": "^2.8|^3.3|^4.0",
"symfony/dependency-injection": "^2.8|^3.3|^4.0",
"symfony/framework-bundle": "^2.8.18|^3.3|^4.0",
"symfony/event-dispatcher": "^2.8.8|^3.3|^4.0",
"symfony/form": "^2.8.8|^3.3|^4.0",
"symfony/http-foundation": "^2.8.8|^3.3|^4.0",
"symfony/http-kernel": "^2.8.8|^3.3|^4.0",
"symfony/property-access": "^2.8|^3.3|^4.0",
"symfony/templating": "^2.8|^3.3|^4.0"
},
Expand All @@ -43,6 +46,7 @@
"symfony/browser-kit": "^2.8|^3.3|^4.0",
"symfony/css-selector": "^2.8|^3.3|^4.0",
"symfony/dom-crawler": "^2.8|^3.3|^4.0",
"symfony/framework-bundle": "^2.8.18|^3.3|^4.0",
"symfony/phpunit-bridge": "^3.3",
"symfony/security-csrf": "^2.8|^3.3|^4.0",
"symfony/twig-bridge": "^2.8.10|^3.3|^4.0",
Expand Down
2 changes: 1 addition & 1 deletion runTests.sh
@@ -1,6 +1,6 @@
#!/bin/bash

SUPPORTED_SYMFONY_VERSIONS=('~2.8.0' '~3.2.0' '~3.3.0')
SUPPORTED_SYMFONY_VERSIONS=('~2.8.0' '~3.3.0' '~3.4.0' '~4.0.0')
GREEN='\033[0;32m'
NC='\033[0m'

Expand Down

0 comments on commit 5cc936f

Please sign in to comment.