2323
2424class NodeListCommand extends BasePhpcrCommand
2525{
26+ protected $ sortOptions = array ('none ' , 'asc ' , 'desc ' );
27+
2628 protected $ formatter ;
2729 protected $ textHelper ;
2830 protected $ maxLevel ;
@@ -38,6 +40,10 @@ protected function configure()
3840 $ this ->addOption ('properties ' , null , InputOption::VALUE_NONE , 'List only the properties of this node ' );
3941 $ this ->addOption ('level ' , 'L ' , InputOption::VALUE_REQUIRED , 'Depth of tree to show ' );
4042 $ this ->addOption ('template ' , 't ' , InputOption::VALUE_NONE , 'Show template nodes and properties ' );
43+ $ this ->addOption ('sort ' , 's ' , InputOption::VALUE_REQUIRED , sprintf (
44+ 'Sort properties, one of: <comment>%s</comment> ' ,
45+ implode ('</comment>, <comment> ' , $ this ->sortOptions )
46+ ), 'asc ' );
4147 $ this ->setHelp (<<<HERE
4248List both or one of the children and properties of this node.
4349
@@ -61,6 +67,15 @@ public function execute(InputInterface $input, OutputInterface $output)
6167 $ this ->formatter = $ this ->get ('helper.result_formatter ' );
6268 $ this ->textHelper = $ this ->get ('helper.text ' );
6369 $ this ->maxLevel = $ input ->getOption ('level ' );
70+ $ this ->sort = strtolower ($ input ->getOption ('sort ' ));
71+
72+ if (!in_array ($ this ->sort , $ this ->sortOptions )) {
73+ throw new \InvalidArgumentException (sprintf (
74+ 'Sort must be one of "%s". "%s" given ' ,
75+ implode ('", " ' , $ this ->sortOptions ),
76+ $ this ->sort
77+ ));
78+ }
6479
6580 $ this ->showChildren = $ input ->getOption ('children ' );
6681 $ this ->showProperties = $ input ->getOption ('properties ' );
@@ -198,7 +213,8 @@ private function renderChildren($currentNode, $table, $spacers, $filter = null)
198213
199214 private function renderProperties ($ currentNode , $ table , $ spacers , $ filter = null )
200215 {
201- $ properties = $ currentNode ->getProperties ($ filter ?: null );
216+ $ properties = (array ) $ currentNode ->getProperties ($ filter ?: null );
217+ $ properties = $ this ->sort ($ properties );
202218
203219 try {
204220 $ primaryItem = $ currentNode ->getPrimaryItem ();
@@ -248,4 +264,19 @@ private function renderProperties($currentNode, $table, $spacers, $filter = null
248264 }
249265 }
250266 }
267+
268+ private function sort ($ array )
269+ {
270+ switch ($ this ->sort ) {
271+ case 'asc ' :
272+ ksort ($ array );
273+ return $ array ;
274+ case 'desc ' :
275+ ksort ($ array );
276+ $ array = array_reverse ($ array );
277+ return $ array ;
278+ }
279+
280+ return $ array ;
281+ }
251282}
0 commit comments