Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Myrza committed Aug 16, 2017
1 parent b4afe2c commit e3d6b31
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
9 changes: 9 additions & 0 deletions .gitignore
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

31 changes: 0 additions & 31 deletions Commands/ReadYamlCommand.php

This file was deleted.

61 changes: 61 additions & 0 deletions Commands/UpdateYaml.php
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;
}
}
2 changes: 1 addition & 1 deletion box.json
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",
Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Symfony\Component\Console\Input\ArgvInput;

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/Commands/ReadYamlCommand.php';
require_once __DIR__.'/Commands/UpdateYaml.php';


$console = new Application();


$console->addCommands(
[
new ReadYamlCommand()
new UpdateYaml()
]
);

Expand Down
Binary file added yaml.phar
Binary file not shown.

0 comments on commit e3d6b31

Please sign in to comment.