diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d7989f5..93c842da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ dev-master - [references] Show UUIDs when listing reference properties - [import/export] Renamed session import and export to `session:import` & `session:export` +- [config] Added user config for general settings +- [config] Enable / disable showing execution times and set decimal expansion - [transport] Added transport layer for experimental Jackalope FS implementation - [misc] Wildcard (single asterisk) support in paths - [node] Added wilcard support to applicable node commands, including "node:list", "node:remove" and "node:property:show" diff --git a/spec/PHPCR/Shell/Config/ConfigManagerSpec.php b/spec/PHPCR/Shell/Config/ConfigManagerSpec.php index 6530ad30..ba530a5f 100644 --- a/spec/PHPCR/Shell/Config/ConfigManagerSpec.php +++ b/spec/PHPCR/Shell/Config/ConfigManagerSpec.php @@ -34,10 +34,7 @@ public function it_should_be_able_to_parse_a_config_file_and_return_the_config_a putenv('PHPCRSH_HOME=' . $dir); $filesystem->exists(Argument::any())->willReturn(true); - $this->getConfig('alias')->shouldReturn(array( - 'foobar' => 'barfoo', - 'barfoo' => 'foobar', - )); - + $this->getConfig('alias')->offsetGet('foobar')->shouldReturn('barfoo'); + $this->getConfig('alias')->offsetGet('barfoo')->shouldReturn('foobar'); } } diff --git a/spec/PHPCR/Shell/Config/ConfigSpec.php b/spec/PHPCR/Shell/Config/ConfigSpec.php new file mode 100644 index 00000000..028d7b45 --- /dev/null +++ b/spec/PHPCR/Shell/Config/ConfigSpec.php @@ -0,0 +1,34 @@ +shouldHaveType('PHPCR\Shell\Config\Config'); + } + + function let() + { + $this->beConstructedWith(array( + 'foo' => 'bar', + 'bar' => array( + 'boo' => 'baz' + ), + )); + } + + function it_should_be_able_to_access_data_values() + { + $this['foo']->shouldReturn('bar'); + } + + function it_should_be_able_to_access_nested_config() + { + $this['bar']['boo']->shouldReturn('baz'); + } +} diff --git a/spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php b/spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php index 8497642c..849b54b0 100644 --- a/spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php +++ b/spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php @@ -5,15 +5,17 @@ use PhpSpec\ObjectBehavior; use PHPCR\Shell\Console\Helper\TextHelper; use PHPCR\Shell\Console\Helper\TableHelper; +use PHPCR\Shell\Config\Config; class ResultFormatterHelperSpec extends ObjectBehavior { public function let( TextHelper $textHelper, - TableHelper $tableHelper + TableHelper $tableHelper, + Config $config ) { - $this->beConstructedWith($textHelper, $tableHelper); + $this->beConstructedWith($textHelper, $tableHelper, $config); } public function it_is_initializable() diff --git a/src/PHPCR/Shell/Config/Config.php b/src/PHPCR/Shell/Config/Config.php new file mode 100644 index 00000000..77329229 --- /dev/null +++ b/src/PHPCR/Shell/Config/Config.php @@ -0,0 +1,71 @@ +data = $data; + } + + public function offsetSet($offset, $value) + { + throw new \InvalidArgumentException(sprintf( + 'Setting values not permitted on configuration objects (trying to set "%s" to "%s"', + $offset, $value + )); + } + + public function offsetExists($offset) + { + return isset($this->data[$offset]); + } + + public function offsetUnset($offset) + { + unset($this->data[$offset]); + } + + public function offsetGet($offset) + { + if (isset($this->data[$offset])) { + $value = $this->data[$offset]; + if (is_array($value)) { + return new self($value); + } else { + return $value; + } + } + } + + public function current() + { + return current($this->data); + } + + public function key() + { + return key($this->data); + } + + public function next() + { + return next($this->data); + } + + public function rewind() + { + return reset($this->data); + } + + public function valid() + { + return current($this->data); + } +} diff --git a/src/PHPCR/Shell/Config/ConfigManager.php b/src/PHPCR/Shell/Config/ConfigManager.php index 480dc273..e4301057 100644 --- a/src/PHPCR/Shell/Config/ConfigManager.php +++ b/src/PHPCR/Shell/Config/ConfigManager.php @@ -5,6 +5,7 @@ use Symfony\Component\Yaml\Yaml; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Filesystem\Filesystem; +use PHPCR\Shell\Config\Config; /** * Configuration manager @@ -20,7 +21,8 @@ class ConfigManager * @var array */ protected $configKeys = array( - 'alias' + 'alias', + 'phpcrsh', ); /** @@ -114,11 +116,24 @@ public function loadConfig() $fullDistPath = $distConfigDir . '/' . $configKey . '.yml'; $config[$configKey] = array(); + $userConfig = array(); if ($this->filesystem->exists($fullPath)) { - $config[$configKey] = Yaml::parse(file_get_contents($fullPath)); - } elseif ($this->filesystem->exists($fullDistPath)) { - $config[$configKey] = Yaml::parse(file_get_contents($fullDistPath)); + $userConfig = Yaml::parse(file_get_contents($fullPath)); } + + if ($this->filesystem->exists($fullDistPath)) { + $distConfig = Yaml::parse(file_get_contents($fullDistPath)); + } else { + throw new \RuntimeException(sprintf( + 'Could not find dist config at path (%s)', + $fullDistPath + )); + } + + $config[$configKey] = new Config(array_merge( + $distConfig, + $userConfig + )); } $this->cachedConfig = $config; @@ -142,6 +157,11 @@ public function getConfig($type) return $this->cachedConfig[$type]; } + public function getPhpcrshConfig() + { + return $this->getConfig('phpcrsh'); + } + /** * Initialize a configuration files */ @@ -167,6 +187,7 @@ public function initConfig(OutputInterface $output = null, $noInteraction = fals $configFilenames = array( 'alias.yml', + 'phpcrsh.yml', ); foreach ($configFilenames as $configFilename) { diff --git a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php index f6537a0b..78ee9983 100644 --- a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php +++ b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php @@ -17,6 +17,8 @@ class NodeListCommand extends BasePhpcrCommand protected $formatter; protected $textHelper; protected $maxLevel; + protected $time; + protected $nbNodes; protected function configure() { @@ -54,6 +56,10 @@ public function execute(InputInterface $input, OutputInterface $output) $this->showChildren = $input->getOption('children'); $this->showProperties = $input->getOption('properties'); $this->showTemplate = $input->getOption('template'); + $this->time = 0; + $this->nbNodes = 0; + + $config = $this->get('config.config.phpcrsh'); $session = $this->get('phpcr.session'); $path = $input->getArgument('path'); @@ -70,7 +76,10 @@ public function execute(InputInterface $input, OutputInterface $output) $filter = substr($filter, 1); } + + $start = microtime(true); $nodes = $session->findNodes($parentPath); + $this->time = microtime(true) - $start; } if (!$this->showChildren && !$this->showProperties) { @@ -88,10 +97,18 @@ public function execute(InputInterface $input, OutputInterface $output) } } + if ($config['show_execution_time_list']) { + $output->writeln(sprintf( + '%s nodes in set (%s sec)', + $this->nbNodes, + number_format($this->time, $config['execution_time_expansion'])) + ); + } } private function renderNode($currentNode, $table, $spacers = array(), $filter = null) { + $this->nbNodes++; if ($this->showChildren) { $this->renderChildren($currentNode, $table, $spacers, $filter); } @@ -103,7 +120,9 @@ private function renderNode($currentNode, $table, $spacers = array(), $filter = private function renderChildren($currentNode, $table, $spacers, $filter = null) { + $start = microtime(true); $children = $currentNode->getNodes($filter ? : null); + $this->time += microtime(true) - $start; $nodeType = $currentNode->getPrimaryNodeType(); $childNodeDefinitions = $nodeType->getDeclaredChildNodeDefinitions(); diff --git a/src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php b/src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php index d45eb67b..7d0c602b 100644 --- a/src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php +++ b/src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php @@ -8,6 +8,7 @@ use PHPCR\PropertyType; use PHPCR\NodeInterface; use PHPCR\PropertyInterface; +use PHPCR\Shell\Config\Config; /** * Provide methods for formatting PHPCR objects @@ -18,11 +19,13 @@ class ResultFormatterHelper extends Helper { protected $textHelper; protected $tableHelper; + protected $config; - public function __construct(TextHelper $textHelper, TableHelper $tableHelper) + public function __construct(TextHelper $textHelper, TableHelper $tableHelper, Config $config) { $this->textHelper = $textHelper; $this->tableHelper = $tableHelper; + $this->config = $config; } /** @@ -74,7 +77,14 @@ public function formatQueryResult(QueryResultInterface $result, OutputInterface } $table->render($output); - $output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2))); + + if (true === $this->config['execution_time']['query']) { + $output->writeln(sprintf( + '%s rows in set (%s sec)', + count($result->getRows()), + number_format($elapsed, $this->config['execution_time_expansion'])) + ); + } } public function normalizeValue($value) diff --git a/src/PHPCR/Shell/DependencyInjection/Container.php b/src/PHPCR/Shell/DependencyInjection/Container.php index 1a856d6d..dd1d7625 100644 --- a/src/PHPCR/Shell/DependencyInjection/Container.php +++ b/src/PHPCR/Shell/DependencyInjection/Container.php @@ -44,7 +44,8 @@ public function registerHelpers() $this->register('helper.node', 'PHPCR\Shell\Console\Helper\NodeHelper'); $this->register('helper.result_formatter', 'PHPCR\Shell\Console\Helper\ResultFormatterHelper') ->addArgument(new Reference('helper.text')) - ->addArgument(new Reference('helper.table')); + ->addArgument(new Reference('helper.table')) + ->addArgument(new Reference('config.config.phpcrsh')); $this->register('helper.table', 'PHPCR\Shell\Console\Helper\TableHelper'); } @@ -56,6 +57,9 @@ public function registerConfig() $this->register('config.profile', 'PHPCR\Shell\Config\Profile'); $this->register('config.profile_loader', 'PHPCR\Shell\Config\ProfileLoader') ->addArgument(new Reference('config.manager')); + $this->register('config.config.phpcrsh', 'PHPCR\Shell\Config\Config') + ->setFactoryService('config.manager') + ->setFactoryMethod('getPhpcrshConfig'); } public function registerPhpcr() @@ -84,13 +88,8 @@ public function registerPhpcr() $repositoryDefinition = $this->register('phpcr.repository'); $sessionDefinition = $this->register('phpcr.session'); - if (method_exists($repositoryDefinition, 'setFactory')) { - $repositoryDefinition->setFactory(array(new Reference('phpcr.session_manager'), 'getRepository')); - $sessionDefinition->setFactory(array(new Reference('phpcr.session_manager'), 'getSession')); - } else { - $repositoryDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getRepository'); - $sessionDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getSession'); - } + $repositoryDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getRepository'); + $sessionDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getSession'); } public function registerEvent() @@ -123,7 +122,6 @@ public function registerEvent() ) ->addArgument(new Reference('config.manager')) ->addTag('event.subscriber'); - } $this->register( diff --git a/src/PHPCR/Shell/Resources/config.dist/phpcrsh.yml b/src/PHPCR/Shell/Resources/config.dist/phpcrsh.yml new file mode 100644 index 00000000..8b2d8f20 --- /dev/null +++ b/src/PHPCR/Shell/Resources/config.dist/phpcrsh.yml @@ -0,0 +1,16 @@ +# PHPCRSH configuration +# +# This is a copy of the default configuration +# +# You may delete any or all of the keys as it will be merged +# on top of the default configuration. +# + +# Amount of decimal expansion when showing timing information +execution_time_expansion: 6 + +# Show execution time for queries +show_execution_time_query: true + +# Show execution time for node list operations (i.e. node:list) +show_execution_time_list: true