@@ -24,6 +24,7 @@ class Tree
2424 protected $ hash ;
2525 protected $ isInitialized = false ;
2626 protected $ entries ;
27+ protected $ entriesByType ;
2728
2829 public function __construct (Repository $ repository , $ hash )
2930 {
@@ -47,31 +48,68 @@ protected function initialize()
4748 $ parser ->parse ($ output );
4849
4950 $ this ->entries = [];
51+ $ this ->entriesByType = [
52+ 'blob ' => [],
53+ 'tree ' => [],
54+ 'commit ' => [],
55+ ];
5056
5157 foreach ($ parser ->entries as $ entry ) {
5258 list ($ mode , $ type , $ hash , $ name ) = $ entry ;
5359 if ($ type == 'blob ' ) {
54- $ this -> entries [ $ name ] = [$ mode , $ this ->repository ->getBlob ($ hash )];
60+ $ treeEntry = [$ mode , $ this ->repository ->getBlob ($ hash )];
5561 } elseif ($ type == 'tree ' ) {
56- $ this -> entries [ $ name ] = [$ mode , $ this ->repository ->getTree ($ hash )];
62+ $ treeEntry = [$ mode , $ this ->repository ->getTree ($ hash )];
5763 } else {
58- $ this -> entries [ $ name ] = [$ mode , new CommitReference ($ hash )];
64+ $ treeEntry = [$ mode , new CommitReference ($ hash )];
5965 }
66+ $ this ->entries [$ name ] = $ treeEntry ;
67+ $ this ->entriesByType [$ type ][$ name ] = $ treeEntry ;
6068 }
6169
6270 $ this ->isInitialized = true ;
6371 }
6472
6573 /**
66- * @return array An associative array name => $object
74+ * @return array<string, array{string, CommitReference|Tree|Blob}> An associative array name => $object
6775 */
68- public function getEntries ()
76+ public function getEntries (): array
6977 {
7078 $ this ->initialize ();
7179
7280 return $ this ->entries ;
7381 }
7482
83+ /**
84+ * @return array<string, array{string, CommitReference}> An associative array of name => [mode, commit reference]
85+ */
86+ public function getCommitReferenceEntries (): array
87+ {
88+ $ this ->initialize ();
89+
90+ return $ this ->entriesByType ['commit ' ];
91+ }
92+
93+ /**
94+ * @return array<string, array{string, Tree}> An associative array of name => [mode, tree]
95+ */
96+ public function getTreeEntries (): array
97+ {
98+ $ this ->initialize ();
99+
100+ return $ this ->entriesByType ['tree ' ];
101+ }
102+
103+ /**
104+ * @return array<string, array{string, Blob}> An associative array of name => [mode, blob]
105+ */
106+ public function getBlobEntries (): array
107+ {
108+ $ this ->initialize ();
109+
110+ return $ this ->entriesByType ['blob ' ];
111+ }
112+
75113 public function getEntry ($ name )
76114 {
77115 $ this ->initialize ();
0 commit comments