7
7
8
8
class ComposerCompletionCommand extends CompletionCommand
9
9
{
10
+ /**
11
+ * Whether or not composer is being run using the global command
12
+ * @var boolean
13
+ */
14
+ protected $ isGlobal = false ;
15
+
10
16
protected function runCompletion ()
11
17
{
12
18
$ context = $ this ->handler ->getContext ();
@@ -24,6 +30,22 @@ protected function runCompletion()
24
30
$ context ->setCommandLine (
25
31
preg_replace_callback ('/( global| g)( |$)/ ' , $ replace , $ context ->getCommandLine (), 1 )
26
32
);
33
+
34
+ $ this ->isGlobal = true ;
35
+ }
36
+
37
+ // Complete for composer.json in current directory
38
+ if ($ this ->isGlobal ) {
39
+ $ composerFile = getcwd () . '/composer.json ' ;
40
+ } else {
41
+ $ workingDir = getenv ('COMPOSER_CWD ' );
42
+ $ composerFile = $ workingDir . '/composer.json ' ;
43
+ }
44
+
45
+ if (file_exists ($ composerFile )) {
46
+ $ this ->addProjectLocalCompletions (
47
+ json_decode (file_get_contents ($ composerFile ), true )
48
+ );
27
49
}
28
50
29
51
// Complete for `help` command's `command` argument
@@ -47,4 +69,41 @@ function() use ($application) {
47
69
48
70
return $ this ->handler ->runCompletion ();
49
71
}
72
+
73
+ /**
74
+ * Setup completions that require a composer.json file to work
75
+ * @param array $config - parsed composer.json
76
+ */
77
+ protected function addProjectLocalCompletions ($ config )
78
+ {
79
+ $ packages = $ this ->getRequiredPackages ($ config );
80
+
81
+ $ completeRequiredPackages = function () use ($ packages ) {
82
+ return $ packages ;
83
+ };
84
+
85
+ // Complete for `remove` and `update` commands `packages` argument
86
+ $ this ->handler ->addHandler (new Completion ('remove ' , 'packages ' , Completion::TYPE_ARGUMENT , $ completeRequiredPackages ));
87
+ $ this ->handler ->addHandler (new Completion ('update ' , 'packages ' , Completion::TYPE_ARGUMENT , $ completeRequiredPackages ));
88
+ }
89
+
90
+ /**
91
+ * Get a list of package names that are required in a composer.json config
92
+ * @param array $config
93
+ * @return array
94
+ */
95
+ protected function getRequiredPackages ($ config )
96
+ {
97
+ $ packages = array ();
98
+
99
+ if (isset ($ config ['require ' ])) {
100
+ $ packages = array_merge ($ packages , array_keys ($ config ['require ' ]));
101
+ }
102
+
103
+ if (isset ($ config ['require-dev ' ])) {
104
+ $ packages = array_merge ($ packages , array_keys ($ config ['require-dev ' ]));
105
+ }
106
+
107
+ return $ packages ;
108
+ }
50
109
}
0 commit comments