-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
378 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Maidmaid\Flag\Command\DebugCommand; | ||
use Symfony\Component\Console\Application; | ||
|
||
require file_exists(__DIR__.'/../vendor/autoload.php') | ||
? __DIR__.'/../vendor/autoload.php' | ||
: __DIR__.'/../../../autoload.php' | ||
; | ||
|
||
$app = new Application(); | ||
$app->add(new DebugCommand()); | ||
$app->setDefaultCommand('debug:flag', true); | ||
$app->run(); |
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,152 @@ | ||
<?php | ||
|
||
namespace Maidmaid\Flag\Demo; | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Symfony\Component\Console\Logger\ConsoleLogger; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
use Maidmaid\Flag\Flag; | ||
|
||
$logger = new ConsoleLogger(new ConsoleOutput(ConsoleOutput::VERBOSITY_DEBUG)); | ||
|
||
echo "\nOverview\n"; | ||
class Yaml | ||
{ | ||
const DUMP_OBJECT = 1; | ||
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2; | ||
const PARSE_OBJECT = 4; | ||
const PARSE_OBJECT_FOR_MAP = 8; | ||
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16; | ||
const PARSE_DATETIME = 32; | ||
const DUMP_OBJECT_AS_MAP = 64; | ||
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128; | ||
const PARSE_CONSTANT = 256; | ||
} | ||
$flag = Flag::create(Yaml::class); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(Yaml::DUMP_OBJECT) | ||
->add(Yaml::PARSE_DATETIME) | ||
->remove(Yaml::DUMP_OBJECT) | ||
; | ||
var_dump( | ||
$flag->has(Yaml::DUMP_OBJECT), | ||
$flag->has(Yaml::PARSE_DATETIME), | ||
$flag->get() | ||
); | ||
$flag->set(100); | ||
foreach ($flag as $k => $v) { | ||
echo "$k => $v "; | ||
} | ||
|
||
echo "\n\nExample\n"; | ||
class Color | ||
{ | ||
const RED = 1; | ||
const GREEN = 2; | ||
const YELLOW = 3; | ||
public $flag; | ||
|
||
public function __construct($logger) | ||
{ | ||
$this->flag = Flag::create(self::class); | ||
$this->flag->setLogger($logger); | ||
} | ||
} | ||
(new Color($logger))->flag | ||
->add(Color::RED) | ||
->add(Color::GREEN) | ||
->remove(Color::GREEN) | ||
; | ||
|
||
echo "\nPrefix\n"; | ||
class Caster | ||
{ | ||
const EXCLUDE_VERBOSE = 1; | ||
const EXCLUDE_VIRTUAL = 2; | ||
const EXCLUDE_DYNAMIC = 4; | ||
const EXCLUDE_PUBLIC = 8; | ||
const EXCLUDE_PROTECTED = 16; | ||
const EXCLUDE_PRIVATE = 32; | ||
const EXCLUDE_NULL = 64; | ||
const EXCLUDE_EMPTY = 128; | ||
const EXCLUDE_NOT_IMPORTANT = 256; | ||
const EXCLUDE_STRICT = 512; | ||
} | ||
$flag = Flag::create(Caster::class, 'EXCLUDE_'); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(Caster::EXCLUDE_EMPTY) | ||
->add(Caster::EXCLUDE_PRIVATE) | ||
->add(Caster::EXCLUDE_NOT_IMPORTANT) | ||
->remove(Caster::EXCLUDE_NOT_IMPORTANT) | ||
; | ||
|
||
echo "\nHierarchical\n"; | ||
class Output | ||
{ | ||
const VERBOSITY_QUIET = 16; | ||
const VERBOSITY_NORMAL = 32; | ||
const VERBOSITY_VERBOSE = 64; | ||
const VERBOSITY_VERY_VERBOSE = 128; | ||
const VERBOSITY_DEBUG = 256; | ||
} | ||
$flag = Flag::create(Output::class, 'VERBOSITY_', true); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(Output::VERBOSITY_VERBOSE) | ||
->add(Output::VERBOSITY_DEBUG) | ||
->remove(Output::VERBOSITY_DEBUG) | ||
; | ||
|
||
echo "\nGlobal space\n"; | ||
$flag = Flag::create(null, 'E_'); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(E_ALL) | ||
->set(0) | ||
->add(E_USER_ERROR) | ||
->add(E_USER_DEPRECATED) | ||
->remove(E_USER_DEPRECATED) | ||
; | ||
|
||
echo "\nBinarizedFlag\n"; | ||
class Request | ||
{ | ||
const METHOD_HEAD = 'HEAD'; | ||
const METHOD_GET = 'GET'; | ||
const METHOD_POST = 'POST'; | ||
const METHOD_PUT = 'PUT'; | ||
const METHOD_PATCH = 'PATCH'; | ||
const METHOD_DELETE = 'DELETE'; | ||
const METHOD_PURGE = 'PURGE'; | ||
const METHOD_OPTIONS = 'OPTIONS'; | ||
const METHOD_TRACE = 'TRACE'; | ||
const METHOD_CONNECT = 'CONNECT'; | ||
} | ||
$flag = Flag::create(Request::class, 'METHOD_'); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(Request::METHOD_GET) | ||
->add(Request::METHOD_POST) | ||
->add(Request::METHOD_PUT) | ||
->remove(Request::METHOD_PUT) | ||
; | ||
|
||
echo "\nStandalone\n"; | ||
$flag = new Flag(); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add(8) | ||
->add(32) | ||
->remove(32) | ||
; | ||
echo "\n"; | ||
$flag = Flag::create(); | ||
$flag->setLogger($logger); | ||
$flag | ||
->add('a') | ||
->add('b') | ||
->remove('b') | ||
; |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,118 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the maidmaid/flag package. | ||
* | ||
* (c) Dany Maillard <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Maidmaid\Flag\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Maidmaid\Flag\Flag; | ||
|
||
class DebugCommand extends Command | ||
{ | ||
private $legends = array(); | ||
private $max; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('debug:flag') | ||
->addArgument('bitfield', InputArgument::OPTIONAL, '', 0) | ||
->addOption('from', 'f', InputOption::VALUE_REQUIRED, 'Class where searching flags is made') | ||
->addOption('prefix', 'p', InputOption::VALUE_REQUIRED, 'Prefix flags that filter search result') | ||
->addOption('hierarchical', 'l', InputOption::VALUE_NONE, 'Defines flags as hierarchical') | ||
->setHelp(<<<EOF | ||
Examples of <info>%command.name%</info> command: | ||
<info>php %command.full_name% --prefix E_</info> | ||
<info>php %command.full_name% --prefix E_ 8</info> | ||
<info>php %command.full_name% --from Symfony\\\\Component\\\\Console\\\\Output\\\\Output --prefix VERBOSITY_ --hierarchical 64</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$bitfield = (int) $input->getArgument('bitfield'); | ||
$from = $input->getOption('from'); | ||
$prefix = $input->getOption('prefix') ? $input->getOption('prefix') : ''; | ||
$hierarchical = $input->getOption('hierarchical'); | ||
|
||
$flag = Flag::create($from, $prefix, $hierarchical, $bitfield); | ||
|
||
$flags = array(); | ||
$flagged = array(); | ||
$iterator = $flag->getIterator(false); | ||
$iterator->ksort(); | ||
foreach ($iterator as $v => $f) { | ||
// Check if it's a top flag | ||
if (((int) ($r = log($v, 2))) == $r) { | ||
$flags[$r] = $f; | ||
$flagged[$r] = $flag->has($v) ? '<bg=green;fg=black;options=bold>1</>' : 0; | ||
} | ||
} | ||
$this->max = max(array_keys($flags)); | ||
|
||
$headers = array(); | ||
$bitfield = array(); | ||
for ($i = $this->max; $i >= 0; --$i) { | ||
$headers[] = $i; | ||
$bitfield[] = isset($flagged[$i]) ? $flagged[$i] : 0; | ||
} | ||
|
||
$i = -1; | ||
foreach ($flags as $x => $flag) { | ||
$this->legend($this->max - $x, ++$i, $flag, $flagged[$x]); | ||
} | ||
|
||
$rows = $this->legends; | ||
array_unshift($rows, $bitfield); | ||
|
||
$table = new Table($output); | ||
$table->setColumnWidths(array_fill(0, count($headers), 2)); | ||
$table->setHeaders($headers); | ||
$table->setRows($rows); | ||
$table->setStyle('compact'); | ||
|
||
$output->writeln(''); | ||
$table->render(); | ||
} | ||
|
||
private function legend($x, $y, $name, $highlight = false) | ||
{ | ||
$format = $highlight ? '<info>%s</info>' : '%s'; | ||
|
||
while (!isset($this->legends[$y])) { | ||
$this->legends[] = array_fill(0, $this->max + 1, ''); | ||
} | ||
|
||
$this->legends[$y][$x] = sprintf($format, '└─'); | ||
$this->legends[$y][$this->max + 1] = sprintf($format, $name); | ||
|
||
// h | ||
for ($i = $x + 1; $i <= $this->max; ++$i) { | ||
$this->legends[$y][$i] = sprintf($format, '──'); | ||
} | ||
|
||
// v | ||
for ($i = $y - 1; $i >= 0; --$i) { | ||
$this->legends[$i][$x] = sprintf($format, '│'); | ||
} | ||
} | ||
} |
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.