Skip to content

Commit

Permalink
Added CLI utils
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Sep 28, 2016
1 parent c463820 commit 7c10d8a
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All Notable changes to `adespresso/feature-bundle` will be documented in this fi
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [Unreleased]
### Added

* Added `features:create <parent> <name> [--enabled] [--role=<REQUIRED_ROLE>]` console command to create a feature from CLI
* Added `features:disable <parent> <name>` console command to disable a feature from CLI
* Added `features:enable <parent> <name> [--role=<REQUIRED_ROLE>]` console command to enable a feature from CLI

### Changed

* Updated symfony dependencies to require at least v2.8
Expand Down
94 changes: 94 additions & 0 deletions Command/CreateFeatureCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Ae\FeatureBundle\Command;

use Ae\FeatureBundle\Entity\FeatureManager;
use Doctrine\ORM\EntityManager;
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;

/**
* Command used to create a feature.
*
* @author Emanuele Minotto <emanuele@adespresso.com>
*/
class CreateFeatureCommand extends Command
{
/**
* @var FeatureManager
*/
private $featureManager;

/**
* @var EntityManager
*/
private $entityManager;

/**
* @param FeatureManager $featureManager
* @param EntityManager $entityManager
*/
public function __construct(
FeatureManager $featureManager,
EntityManager $entityManager
) {
parent::__construct();

$this->featureManager = $featureManager;
$this->entityManager = $entityManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('features:create')
->setDescription('Create a new feature')
->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
->addArgument('name', InputArgument::REQUIRED, 'Feature name')
->addOption(
'enabled',
null,
InputOption::VALUE_NONE,
'The feature will be created as enabled'
)
->addOption(
'role',
null,
InputOption::VALUE_REQUIRED,
'The feature will be only for a role'
);
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$parent = $input->getArgument('parent');

$output->write(sprintf(
'Creating <info>%s</info>.<info>%s</info>... ',
$parent,
$name
));

$feature = $this->featureManager->findOrCreate($name, $parent);

$feature->setEnabled($input->getOption('enabled'));
$feature->setRole($input->getOption('role'));

$this->entityManager->persist($feature);
$this->entityManager->flush();

$this->featureManager->emptyCache($name, $parent);

$output->writeln('OK');
}
}
80 changes: 80 additions & 0 deletions Command/DisableFeatureCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Ae\FeatureBundle\Command;

use Ae\FeatureBundle\Entity\FeatureManager;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command used to disable an existing feature.
*
* @author Emanuele Minotto <emanuele@adespresso.com>
*/
class DisableFeatureCommand extends Command
{
/**
* @var FeatureManager
*/
private $featureManager;

/**
* @var EntityManager
*/
private $entityManager;

/**
* @param FeatureManager $featureManager
* @param EntityManager $entityManager
*/
public function __construct(
FeatureManager $featureManager,
EntityManager $entityManager
) {
parent::__construct();

$this->featureManager = $featureManager;
$this->entityManager = $entityManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('features:disable')
->setDescription('Disable an existing feature')
->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
->addArgument('name', InputArgument::REQUIRED, 'Feature name');
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$parent = $input->getArgument('parent');

$output->write(sprintf(
'Disabling <info>%s</info>.<info>%s</info>... ',
$parent,
$name
));

$feature = $this->featureManager->find($name, $parent);

$feature->setEnabled(false);

$this->entityManager->persist($feature);
$this->entityManager->flush();

$this->featureManager->emptyCache($name, $parent);

$output->writeln('OK');
}
}
91 changes: 91 additions & 0 deletions Command/EnableFeatureCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Ae\FeatureBundle\Command;

use Ae\FeatureBundle\Entity\FeatureManager;
use Doctrine\ORM\EntityManager;
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;

/**
* Command used to enable an existing feature.
*
* @author Emanuele Minotto <emanuele@adespresso.com>
*/
class EnableFeatureCommand extends Command
{
/**
* @var FeatureManager
*/
private $featureManager;

/**
* @var EntityManager
*/
private $entityManager;

/**
* @param FeatureManager $featureManager
* @param EntityManager $entityManager
*/
public function __construct(
FeatureManager $featureManager,
EntityManager $entityManager
) {
parent::__construct();

$this->featureManager = $featureManager;
$this->entityManager = $entityManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('features:enable')
->setDescription('Enable an existing feature')
->addArgument('parent', InputArgument::REQUIRED, 'Parent feature')
->addArgument('name', InputArgument::REQUIRED, 'Feature name')
->addOption(
'role',
null,
InputOption::VALUE_REQUIRED,
'The feature will be only for a role'
);
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$parent = $input->getArgument('parent');

$output->write(sprintf(
'Enabling <info>%s</info>.<info>%s</info>... ',
$parent,
$name
));

$feature = $this->featureManager->find($name, $parent);

$feature->setEnabled(true);

if (null !== $input->getOption('role')) {
$feature->setRole($input->getOption('role'));
}

$this->entityManager->persist($feature);
$this->entityManager->flush();

$this->featureManager->emptyCache($name, $parent);

$output->writeln('OK');
}
}
15 changes: 15 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@
<argument id="ae_feature.feature" type="service"/>
<tag name="twig.extension"/>
</service>
<service class="Ae\FeatureBundle\Command\CreateFeatureCommand" id="ae_feature.command.create_feature">
<argument id="ae_feature.manager" type="service"/>
<argument id="doctrine.orm.entity_manager" type="service"/>
<tag name="console.command" />
</service>
<service class="Ae\FeatureBundle\Command\DisableFeatureCommand" id="ae_feature.command.disable_feature">
<argument id="ae_feature.manager" type="service"/>
<argument id="doctrine.orm.entity_manager" type="service"/>
<tag name="console.command" />
</service>
<service class="Ae\FeatureBundle\Command\EnableFeatureCommand" id="ae_feature.command.enable_feature">
<argument id="ae_feature.manager" type="service"/>
<argument id="doctrine.orm.entity_manager" type="service"/>
<tag name="console.command" />
</service>
<!-- deprecated services, to be removed from v2.0.0 -->
<service id="cw_feature.manager" parent="ae_feature.manager">
<deprecated>The "%service_id%" service is deprecated since 1.1 and will be removed in 2.0.</deprecated>
Expand Down
Loading

0 comments on commit 7c10d8a

Please sign in to comment.