Skip to content

Commit 3890629

Browse files
committed
User config and profiling
- Enable setting expansion for timings - Enableable timing for query / traversal - Config is automatically based with dist config
1 parent edd05d2 commit 3890629

File tree

10 files changed

+192
-22
lines changed

10 files changed

+192
-22
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ dev-master
77
- [references] Show UUIDs when listing reference properties
88
- [import/export] Renamed session import and export to `session:import` &
99
`session:export`
10+
- [config] Added user config for general settings
11+
- [config] Enable / disable showing execution times and set decimal expansion
1012
- [transport] Added transport layer for experimental Jackalope FS implementation
1113
- [misc] Wildcard (single asterisk) support in paths
1214
- [node] Added wilcard support to applicable node commands, including "node:list", "node:remove" and "node:property:show"

spec/PHPCR/Shell/Config/ConfigManagerSpec.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public function it_should_be_able_to_parse_a_config_file_and_return_the_config_a
3434
putenv('PHPCRSH_HOME=' . $dir);
3535
$filesystem->exists(Argument::any())->willReturn(true);
3636

37-
$this->getConfig('alias')->shouldReturn(array(
38-
'foobar' => 'barfoo',
39-
'barfoo' => 'foobar',
40-
));
41-
37+
$this->getConfig('alias')->offsetGet('foobar')->shouldReturn('barfoo');
38+
$this->getConfig('alias')->offsetGet('barfoo')->shouldReturn('foobar');
4239
}
4340
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace spec\PHPCR\Shell\Config;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ConfigSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('PHPCR\Shell\Config\Config');
13+
}
14+
15+
function let()
16+
{
17+
$this->beConstructedWith(array(
18+
'foo' => 'bar',
19+
'bar' => array(
20+
'boo' => 'baz'
21+
),
22+
));
23+
}
24+
25+
function it_should_be_able_to_access_data_values()
26+
{
27+
$this['foo']->shouldReturn('bar');
28+
}
29+
30+
function it_should_be_able_to_access_nested_config()
31+
{
32+
$this['bar']['boo']->shouldReturn('baz');
33+
}
34+
}

spec/PHPCR/Shell/Console/Helper/ResultFormatterHelperSpec.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
use PhpSpec\ObjectBehavior;
66
use PHPCR\Shell\Console\Helper\TextHelper;
77
use PHPCR\Shell\Console\Helper\TableHelper;
8+
use PHPCR\Shell\Config\Config;
89

910
class ResultFormatterHelperSpec extends ObjectBehavior
1011
{
1112
public function let(
1213
TextHelper $textHelper,
13-
TableHelper $tableHelper
14+
TableHelper $tableHelper,
15+
Config $config
1416
)
1517
{
16-
$this->beConstructedWith($textHelper, $tableHelper);
18+
$this->beConstructedWith($textHelper, $tableHelper, $config);
1719
}
1820

1921
public function it_is_initializable()

src/PHPCR/Shell/Config/Config.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace PHPCR\Shell\Config;
4+
5+
/**
6+
* Configuration profile object
7+
*/
8+
class Config implements \ArrayAccess, \Iterator
9+
{
10+
private $data;
11+
12+
public function __construct($data)
13+
{
14+
$this->data = $data;
15+
}
16+
17+
public function offsetSet($offset, $value)
18+
{
19+
throw new \InvalidArgumentException(sprintf(
20+
'Setting values not permitted on configuration objects (trying to set "%s" to "%s"',
21+
$offset, $value
22+
));
23+
}
24+
25+
public function offsetExists($offset)
26+
{
27+
return isset($this->data[$offset]);
28+
}
29+
30+
public function offsetUnset($offset)
31+
{
32+
unset($this->data[$offset]);
33+
}
34+
35+
public function offsetGet($offset)
36+
{
37+
if (isset($this->data[$offset])) {
38+
$value = $this->data[$offset];
39+
if (is_array($value)) {
40+
return new self($value);
41+
} else {
42+
return $value;
43+
}
44+
}
45+
}
46+
47+
public function current()
48+
{
49+
return current($this->data);
50+
}
51+
52+
public function key()
53+
{
54+
return key($this->data);
55+
}
56+
57+
public function next()
58+
{
59+
return next($this->data);
60+
}
61+
62+
public function rewind()
63+
{
64+
return reset($this->data);
65+
}
66+
67+
public function valid()
68+
{
69+
return current($this->data);
70+
}
71+
}

src/PHPCR/Shell/Config/ConfigManager.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\Yaml\Yaml;
66
use Symfony\Component\Console\Output\OutputInterface;
77
use Symfony\Component\Filesystem\Filesystem;
8+
use PHPCR\Shell\Config\Config;
89

910
/**
1011
* Configuration manager
@@ -20,7 +21,8 @@ class ConfigManager
2021
* @var array
2122
*/
2223
protected $configKeys = array(
23-
'alias'
24+
'alias',
25+
'phpcrsh',
2426
);
2527

2628
/**
@@ -114,11 +116,24 @@ public function loadConfig()
114116
$fullDistPath = $distConfigDir . '/' . $configKey . '.yml';
115117
$config[$configKey] = array();
116118

119+
$userConfig = array();
117120
if ($this->filesystem->exists($fullPath)) {
118-
$config[$configKey] = Yaml::parse(file_get_contents($fullPath));
119-
} elseif ($this->filesystem->exists($fullDistPath)) {
120-
$config[$configKey] = Yaml::parse(file_get_contents($fullDistPath));
121+
$userConfig = Yaml::parse(file_get_contents($fullPath));
121122
}
123+
124+
if ($this->filesystem->exists($fullDistPath)) {
125+
$distConfig = Yaml::parse(file_get_contents($fullDistPath));
126+
} else {
127+
throw new \RuntimeException(sprintf(
128+
'Could not find dist config at path (%s)',
129+
$fullDistPath
130+
));
131+
}
132+
133+
$config[$configKey] = new Config(array_merge(
134+
$distConfig,
135+
$userConfig
136+
));
122137
}
123138

124139
$this->cachedConfig = $config;
@@ -142,6 +157,11 @@ public function getConfig($type)
142157
return $this->cachedConfig[$type];
143158
}
144159

160+
public function getPhpcrshConfig()
161+
{
162+
return $this->getConfig('phpcrsh');
163+
}
164+
145165
/**
146166
* Initialize a configuration files
147167
*/
@@ -167,6 +187,7 @@ public function initConfig(OutputInterface $output = null, $noInteraction = fals
167187

168188
$configFilenames = array(
169189
'alias.yml',
190+
'phpcrsh.yml',
170191
);
171192

172193
foreach ($configFilenames as $configFilename) {

src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class NodeListCommand extends BasePhpcrCommand
1717
protected $formatter;
1818
protected $textHelper;
1919
protected $maxLevel;
20+
protected $time;
21+
protected $nbNodes;
2022

2123
protected function configure()
2224
{
@@ -54,6 +56,10 @@ public function execute(InputInterface $input, OutputInterface $output)
5456
$this->showChildren = $input->getOption('children');
5557
$this->showProperties = $input->getOption('properties');
5658
$this->showTemplate = $input->getOption('template');
59+
$this->time = 0;
60+
$this->nbNodes = 0;
61+
62+
$config = $this->get('config.config.phpcrsh');
5763

5864
$session = $this->get('phpcr.session');
5965
$path = $input->getArgument('path');
@@ -70,7 +76,10 @@ public function execute(InputInterface $input, OutputInterface $output)
7076
$filter = substr($filter, 1);
7177
}
7278

79+
80+
$start = microtime(true);
7381
$nodes = $session->findNodes($parentPath);
82+
$this->time = microtime(true) - $start;
7483
}
7584

7685
if (!$this->showChildren && !$this->showProperties) {
@@ -88,10 +97,18 @@ public function execute(InputInterface $input, OutputInterface $output)
8897
}
8998
}
9099

100+
if ($config['show_execution_time_list']) {
101+
$output->writeln(sprintf(
102+
'%s nodes in set (%s sec)',
103+
$this->nbNodes,
104+
number_format($this->time, $config['execution_time_expansion']))
105+
);
106+
}
91107
}
92108

93109
private function renderNode($currentNode, $table, $spacers = array(), $filter = null)
94110
{
111+
$this->nbNodes++;
95112
if ($this->showChildren) {
96113
$this->renderChildren($currentNode, $table, $spacers, $filter);
97114
}
@@ -103,7 +120,9 @@ private function renderNode($currentNode, $table, $spacers = array(), $filter =
103120

104121
private function renderChildren($currentNode, $table, $spacers, $filter = null)
105122
{
123+
$start = microtime(true);
106124
$children = $currentNode->getNodes($filter ? : null);
125+
$this->time += microtime(true) - $start;
107126

108127
$nodeType = $currentNode->getPrimaryNodeType();
109128
$childNodeDefinitions = $nodeType->getDeclaredChildNodeDefinitions();

src/PHPCR/Shell/Console/Helper/ResultFormatterHelper.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPCR\PropertyType;
99
use PHPCR\NodeInterface;
1010
use PHPCR\PropertyInterface;
11+
use PHPCR\Shell\Config\Config;
1112

1213
/**
1314
* Provide methods for formatting PHPCR objects
@@ -18,11 +19,13 @@ class ResultFormatterHelper extends Helper
1819
{
1920
protected $textHelper;
2021
protected $tableHelper;
22+
protected $config;
2123

22-
public function __construct(TextHelper $textHelper, TableHelper $tableHelper)
24+
public function __construct(TextHelper $textHelper, TableHelper $tableHelper, Config $config)
2325
{
2426
$this->textHelper = $textHelper;
2527
$this->tableHelper = $tableHelper;
28+
$this->config = $config;
2629
}
2730

2831
/**
@@ -74,7 +77,14 @@ public function formatQueryResult(QueryResultInterface $result, OutputInterface
7477
}
7578

7679
$table->render($output);
77-
$output->writeln(sprintf('%s rows in set (%s sec)', count($result->getRows()), number_format($elapsed, 2)));
80+
81+
if (true === $this->config['execution_time']['query']) {
82+
$output->writeln(sprintf(
83+
'%s rows in set (%s sec)',
84+
count($result->getRows()),
85+
number_format($elapsed, $this->config['execution_time_expansion']))
86+
);
87+
}
7888
}
7989

8090
public function normalizeValue($value)

src/PHPCR/Shell/DependencyInjection/Container.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function registerHelpers()
4444
$this->register('helper.node', 'PHPCR\Shell\Console\Helper\NodeHelper');
4545
$this->register('helper.result_formatter', 'PHPCR\Shell\Console\Helper\ResultFormatterHelper')
4646
->addArgument(new Reference('helper.text'))
47-
->addArgument(new Reference('helper.table'));
47+
->addArgument(new Reference('helper.table'))
48+
->addArgument(new Reference('config.config.phpcrsh'));
4849
$this->register('helper.table', 'PHPCR\Shell\Console\Helper\TableHelper');
4950
}
5051

@@ -56,6 +57,9 @@ public function registerConfig()
5657
$this->register('config.profile', 'PHPCR\Shell\Config\Profile');
5758
$this->register('config.profile_loader', 'PHPCR\Shell\Config\ProfileLoader')
5859
->addArgument(new Reference('config.manager'));
60+
$this->register('config.config.phpcrsh', 'PHPCR\Shell\Config\Config')
61+
->setFactoryService('config.manager')
62+
->setFactoryMethod('getPhpcrshConfig');
5963
}
6064

6165
public function registerPhpcr()
@@ -84,13 +88,8 @@ public function registerPhpcr()
8488
$repositoryDefinition = $this->register('phpcr.repository');
8589
$sessionDefinition = $this->register('phpcr.session');
8690

87-
if (method_exists($repositoryDefinition, 'setFactory')) {
88-
$repositoryDefinition->setFactory(array(new Reference('phpcr.session_manager'), 'getRepository'));
89-
$sessionDefinition->setFactory(array(new Reference('phpcr.session_manager'), 'getSession'));
90-
} else {
91-
$repositoryDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getRepository');
92-
$sessionDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getSession');
93-
}
91+
$repositoryDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getRepository');
92+
$sessionDefinition->setFactoryService('phpcr.session_manager')->setFactoryMethod('getSession');
9493
}
9594

9695
public function registerEvent()
@@ -123,7 +122,6 @@ public function registerEvent()
123122
)
124123
->addArgument(new Reference('config.manager'))
125124
->addTag('event.subscriber');
126-
127125
}
128126

129127
$this->register(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# PHPCRSH configuration
2+
#
3+
# This is a copy of the default configuration
4+
#
5+
# You may delete any or all of the keys as it will be merged
6+
# on top of the default configuration.
7+
#
8+
9+
# Amount of decimal expansion when showing timing information
10+
execution_time_expansion: 6
11+
12+
# Show execution time for queries
13+
show_execution_time_query: true
14+
15+
# Show execution time for node list operations (i.e. node:list)
16+
show_execution_time_list: true

0 commit comments

Comments
 (0)