-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from totten/master-cmd-ref
Simplify `Command` boilerplate for typical use-cases
- Loading branch information
Showing
57 changed files
with
591 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace Civi\Cv\Command; | ||
|
||
use Civi\Cv\Util\BootTrait; | ||
use Civi\Cv\Util\OptionCallbackTrait; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* `CvCommand` is a Symfony `Command` with support for bootstrapping CiviCRM/CMS. | ||
* | ||
* - From end-user POV, the command accepts options like --user, --level, --hostname. | ||
* - From dev POV, the command allows you to implement `execute()` method without needing to | ||
* explicitly boot Civi. | ||
* - From dev POV, you may fine-tune command by changing the $bootOptions / getBootOptions(). | ||
*/ | ||
class CvCommand extends Command { | ||
|
||
use OptionCallbackTrait; | ||
use BootTrait; | ||
|
||
public function mergeApplicationDefinition($mergeArgs = TRUE) { | ||
parent::mergeApplicationDefinition($mergeArgs); | ||
$this->mergeBootDefinition($this->getDefinition()); | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @param \Symfony\Component\Console\Output\OutputInterface $output | ||
*/ | ||
protected function initialize(InputInterface $input, OutputInterface $output) { | ||
$this->autoboot($input, $output); | ||
parent::initialize($input, $output); | ||
$this->runOptionCallbacks($input, $output); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Civi\Cv\Util; | ||
|
||
class OptionalOption { | ||
|
||
/** | ||
* Parse an option's data. This is for options where the default behavior | ||
* (of total omission) differs from the activated behavior | ||
* (of an active but unspecified option). | ||
* | ||
* Example, suppose we want these interpretations: | ||
* cv en ==> Means "--refresh=auto"; see $omittedDefault | ||
* cv en -r ==> Means "--refresh=yes"; see $activeDefault | ||
* cv en -r=yes ==> Means "--refresh=yes" | ||
* cv en -r=no ==> Means "--refresh=no" | ||
* | ||
* @param \CvDeps\Symfony\Component\Console\Input\InputInterface|\Symfony\Component\Console\Input\InputInterface $input | ||
* @param array $rawNames | ||
* Ex: array('-r', '--refresh'). | ||
* @param string $omittedDefault | ||
* Value to use if option is completely omitted. | ||
* @param string $activeDefault | ||
* Value to use if option is activated without data. | ||
* @return string | ||
*/ | ||
public static function parse($input, $rawNames, $omittedDefault, $activeDefault) { | ||
$value = NULL; | ||
foreach ($rawNames as $rawName) { | ||
if ($input->hasParameterOption($rawName)) { | ||
if (NULL === $input->getParameterOption($rawName)) { | ||
return $activeDefault; | ||
} | ||
else { | ||
return $input->getParameterOption($rawName); | ||
} | ||
} | ||
} | ||
return $omittedDefault; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.