diff --git a/CHANGELOG.md b/CHANGELOG.md
index c049d141..97257f5b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ dev-master
- [profile:show] Added command to display current profile
- [query:update] Introduced `expr()` function to allow setting poperty values using expression language.
+- [node:list] Sort properties by default, added option to specify
### Bug fixes
diff --git a/features/all/phpcr_node_list.feature b/features/all/phpcr_node_list.feature
index 32b9c173..986a1cd7 100644
--- a/features/all/phpcr_node_list.feature
+++ b/features/all/phpcr_node_list.feature
@@ -143,3 +143,19 @@ Feature: List properites and chidren of current nodeA
"""
5 nodes in set
"""
+
+ Scenario: List with sort order ascending
+ When I execute the "node:list --sort=asc" command
+ Then the command should not fail
+
+ Scenario: List with sort order descending
+ When I execute the "node:list --sort=desc" command
+ Then the command should not fail
+
+ Scenario: List with no sorting
+ When I execute the "node:list --sort=none" command
+ Then the command should not fail
+
+ Scenario: List with invalid sorting
+ When I execute the "node:list --sort=foobar" command
+ Then the command should fail
diff --git a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
index f3b30128..2ec5bb1a 100644
--- a/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
+++ b/src/PHPCR/Shell/Console/Command/Phpcr/NodeListCommand.php
@@ -23,6 +23,8 @@
class NodeListCommand extends BasePhpcrCommand
{
+ protected $sortOptions = array('none', 'asc', 'desc');
+
protected $formatter;
protected $textHelper;
protected $maxLevel;
@@ -38,6 +40,10 @@ protected function configure()
$this->addOption('properties', null, InputOption::VALUE_NONE, 'List only the properties of this node');
$this->addOption('level', 'L', InputOption::VALUE_REQUIRED, 'Depth of tree to show');
$this->addOption('template', 't', InputOption::VALUE_NONE, 'Show template nodes and properties');
+ $this->addOption('sort', 's', InputOption::VALUE_REQUIRED, sprintf(
+ 'Sort properties, one of: %s',
+ implode(', ', $this->sortOptions)
+ ), 'asc');
$this->setHelp(<<formatter = $this->get('helper.result_formatter');
$this->textHelper = $this->get('helper.text');
$this->maxLevel = $input->getOption('level');
+ $this->sort = strtolower($input->getOption('sort'));
+
+ if (!in_array($this->sort, $this->sortOptions)) {
+ throw new \InvalidArgumentException(sprintf(
+ 'Sort must be one of "%s". "%s" given',
+ implode('", "', $this->sortOptions),
+ $this->sort
+ ));
+ }
$this->showChildren = $input->getOption('children');
$this->showProperties = $input->getOption('properties');
@@ -198,7 +213,8 @@ private function renderChildren($currentNode, $table, $spacers, $filter = null)
private function renderProperties($currentNode, $table, $spacers, $filter = null)
{
- $properties = $currentNode->getProperties($filter ?: null);
+ $properties = (array) $currentNode->getProperties($filter ?: null);
+ $properties = $this->sort($properties);
try {
$primaryItem = $currentNode->getPrimaryItem();
@@ -248,4 +264,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
}
}
}
+
+ private function sort($array)
+ {
+ switch ($this->sort) {
+ case 'asc':
+ ksort($array);
+ return $array;
+ case 'desc':
+ ksort($array);
+ $array = array_reverse($array);
+ return $array;
+ }
+
+ return $array;
+ }
}