@@ -12,6 +12,8 @@ class NodeListCommand extends Command
12
12
{
13
13
protected $ formatter ;
14
14
protected $ filters ;
15
+ protected $ textHelper ;
16
+ protected $ maxLevel ;
15
17
16
18
protected function configure ()
17
19
{
@@ -21,6 +23,7 @@ protected function configure()
21
23
$ this ->addOption ('children ' , null , InputOption::VALUE_NONE , 'List only the children of this node ' );
22
24
$ this ->addOption ('properties ' , null , InputOption::VALUE_NONE , 'List only the properties of this node ' );
23
25
$ this ->addOption ('filter ' , 'f ' , InputOption::VALUE_REQUIRED |InputOption::VALUE_IS_ARRAY , 'Optional filter to apply ' );
26
+ $ this ->addOption ('level ' , 'L ' , InputOption::VALUE_REQUIRED , 'Depth of tree to show ' );
24
27
$ this ->setHelp (<<<HERE
25
28
List both or one of the children and properties of this node.
26
29
HERE
@@ -32,53 +35,76 @@ public function execute(InputInterface $input, OutputInterface $output)
32
35
$ this ->formatter = $ this ->getHelper ('result_formatter ' );
33
36
$ this ->textHelper = $ this ->getHelper ('text ' );
34
37
$ this ->filters = $ input ->getOption ('filter ' );
38
+ $ this ->maxLevel = $ input ->getOption ('level ' );
35
39
36
- $ showChildren = $ input ->getOption ('children ' );
37
- $ showProperties = $ input ->getOption ('properties ' );
40
+ $ this -> showChildren = $ input ->getOption ('children ' );
41
+ $ this -> showProperties = $ input ->getOption ('properties ' );
38
42
39
43
$ session = $ this ->getHelper ('phpcr ' )->getSession ();
40
44
41
45
$ path = $ session ->getAbsPath ($ input ->getArgument ('path ' ));
42
46
$ currentNode = $ session ->getNode ($ path );
43
47
44
- if (!$ showChildren && !$ showProperties ) {
45
- $ showChildren = true ;
46
- $ showProperties = true ;
48
+ if (!$ this -> showChildren && !$ this -> showProperties ) {
49
+ $ this -> showChildren = true ;
50
+ $ this -> showProperties = true ;
47
51
}
48
52
49
53
$ table = clone $ this ->getHelper ('table ' );
50
54
51
- if ($ showChildren ) {
52
- $ this ->renderChildren ($ currentNode , $ table );
53
- }
55
+ $ this ->renderNode ($ currentNode , $ table );
54
56
55
- if ($ showProperties ) {
56
- $ this ->renderProperties ($ currentNode , $ table );
57
+ $ table ->render ($ output );
58
+ }
59
+
60
+ private function renderNode ($ currentNode , $ table , $ spacers = array ())
61
+ {
62
+ if ($ this ->showChildren ) {
63
+ $ this ->renderChildren ($ currentNode , $ table , $ spacers );
57
64
}
58
65
59
- $ table ->render ($ output );
66
+ if ($ this ->showProperties ) {
67
+ $ this ->renderProperties ($ currentNode , $ table , $ spacers );
68
+ }
60
69
}
61
70
62
- private function renderChildren ($ currentNode , $ table )
71
+ private function renderChildren ($ currentNode , $ table, $ spacers )
63
72
{
64
73
$ children = $ currentNode ->getNodes ($ this ->filters ? : null );
65
74
75
+ $ i = 0 ;
66
76
foreach ($ children as $ child ) {
77
+ $ i ++;
78
+ $ isLast = count ($ children ) === $ i ;
79
+
67
80
$ table ->addRow (array (
68
- '<node> ' . $ this ->formatter ->formatNodeName ($ child ) . '</node> ' ,
81
+ '<node> ' . implode ( '' , $ spacers ) . $ this ->formatter ->formatNodeName ($ child ) . '</node> ' ,
69
82
$ child ->getPrimaryNodeType ()->getName (),
70
83
'' ,
71
84
));
85
+
86
+ if (count ($ spacers ) < $ this ->maxLevel ) {
87
+ $ newSpacers = $ spacers ;
88
+ if ($ isLast ) {
89
+ $ newSpacers [] = ' ' ;
90
+ } else {
91
+ $ newSpacers [] = '| ' ;
92
+ }
93
+
94
+ $ this ->renderNode ($ child , $ table , $ newSpacers );
95
+ }
72
96
}
73
97
}
74
98
75
- private function renderProperties ($ currentNode , $ table )
99
+ private function renderProperties ($ currentNode , $ table, $ spacers )
76
100
{
77
101
$ properties = $ currentNode ->getProperties ($ this ->filters ? : null );
78
102
103
+ $ i = 0 ;
79
104
foreach ($ properties as $ name => $ property ) {
105
+ $ i ++;
80
106
$ table ->addRow (array (
81
- '<property> ' . $ name . '</property> ' ,
107
+ '<property> ' . implode ( '' , $ spacers ). $ name . '</property> ' ,
82
108
'<property-type> ' . $ this ->formatter ->getPropertyTypeName ($ property ->getType ()) . '</property-type> ' ,
83
109
$ this ->textHelper ->truncate ($ this ->formatter ->formatValue ($ property ), 55 ),
84
110
));
0 commit comments