Skip to content

User config and profiling #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 2 additions & 5 deletions spec/PHPCR/Shell/Config/ConfigManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
34 changes: 34 additions & 0 deletions spec/PHPCR/Shell/Config/ConfigSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace spec\PHPCR\Shell\Config;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ConfigSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->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');
}
}
6 changes: 4 additions & 2 deletions spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
71 changes: 71 additions & 0 deletions src/PHPCR/Shell/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace PHPCR\Shell\Config;

/**
* Configuration profile object
*/
class Config implements \ArrayAccess, \Iterator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it make sense to use OptionsResolver instead of this class? the cool thing about it is that you get validation and defaults handling for free.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would still need this class to represent the instance of the configuration, but yeah we could integrate the OptionResolver

tbh the current config system is still a bit messy/strange and could use a general refactor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
private $data;

public function __construct($data)
{
$this->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);
}
}
29 changes: 25 additions & 4 deletions src/PHPCR/Shell/Config/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +21,8 @@ class ConfigManager
* @var array
*/
protected $configKeys = array(
'alias'
'alias',
'phpcrsh',
);

/**
Expand Down Expand Up @@ -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;
Expand All @@ -142,6 +157,11 @@ public function getConfig($type)
return $this->cachedConfig[$type];
}

public function getPhpcrshConfig()
{
return $this->getConfig('phpcrsh');
}

/**
* Initialize a configuration files
*/
Expand All @@ -167,6 +187,7 @@ public function initConfig(OutputInterface $output = null, $noInteraction = fals

$configFilenames = array(
'alias.yml',
'phpcrsh.yml',
);

foreach ($configFilenames as $configFilename) {
Expand Down
19 changes: 19 additions & 0 deletions src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class NodeListCommand extends BasePhpcrCommand
protected $formatter;
protected $textHelper;
protected $maxLevel;
protected $time;
protected $nbNodes;

protected function configure()
{
Expand Down Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably use the stopwatch component if I do much more of this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would leave it as is for now and add StopWatch if you have more cases.


$nodeType = $currentNode->getPrimaryNodeType();
$childNodeDefinitions = $nodeType->getDeclaredChildNodeDefinitions();
Expand Down
14 changes: 12 additions & 2 deletions src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPCR\PropertyType;
use PHPCR\NodeInterface;
use PHPCR\PropertyInterface;
use PHPCR\Shell\Config\Config;

/**
* Provide methods for formatting PHPCR objects
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions src/PHPCR/Shell/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -123,7 +122,6 @@ public function registerEvent()
)
->addArgument(new Reference('config.manager'))
->addTag('event.subscriber');

}

$this->register(
Expand Down
16 changes: 16 additions & 0 deletions src/PHPCR/Shell/Resources/config.dist/phpcrsh.yml
Original file line number Diff line number Diff line change
@@ -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