-
Notifications
You must be signed in to change notification settings - Fork 1
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
Volodymyr Myrza
committed
Aug 16, 2017
1 parent
b4afe2c
commit e3d6b31
Showing
6 changed files
with
73 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
### Composer template | ||
composer.phar | ||
/vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
|
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
<?php | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class UpdateYaml extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this->setName('update'); | ||
$this->addArgument('file'); | ||
$this->addArgument('node-path'); | ||
$this->addArgument('value'); | ||
$this->addOption('inline', 'l', InputOption::VALUE_OPTIONAL, 'The level where you switch to inline YAML', 4); | ||
$this->addOption('indent', 'i', InputOption::VALUE_OPTIONAL, | ||
'The amount of spaces to use for indentation of nested nodes', 2); | ||
$this->addOption('value-type', 't', InputOption::VALUE_OPTIONAL, | ||
'The typecast for provided value (settype($type) or callable name of function used if exists)', 'mixed'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$file = $input->getArgument('file'); | ||
$content = Yaml::parse(file_get_contents($file)); | ||
$type = $input->getOption('value-type'); | ||
$value = $input->getArgument('value'); | ||
if (strtolower($type) !== 'mixed') { | ||
if (function_exists($type)) { | ||
$value = $type($value); | ||
} else { | ||
settype($value, $type); | ||
} | ||
} | ||
self::set($content, $input->getArgument('node-path'), $value); | ||
echo Yaml::dump($content, $input->getOption('inline'), $input->getOption('indent')); | ||
} | ||
|
||
public static function set(&$array, $key, $value) | ||
{ | ||
if (is_null($key)) { | ||
return $array = $value; | ||
} | ||
$keys = explode('.', $key); | ||
while (count($keys) > 1) { | ||
$key = array_shift($keys); | ||
// If the key doesn't exist at this depth, we will just create an empty array | ||
// to hold the next value, allowing us to create the arrays to hold final | ||
// values at the correct depth. Then we'll keep digging into the array. | ||
if (!isset($array[$key]) || !is_array($array[$key])) { | ||
$array[$key] = []; | ||
} | ||
$array = &$array[$key]; | ||
} | ||
$array[array_shift($keys)] = $value; | ||
|
||
return $array; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"main":"index.php", | ||
"output": "example.phar", | ||
"output": "yaml.phar", | ||
"finder": [ | ||
{ | ||
"in": "./Commands", | ||
|
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