8000 Merge pull request #586 from tucksaun/feat/console-path · symfony-cli/symfony-cli@6925d21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6925d21

Browse files
authored
Merge pull request #586 from tucksaun/feat/console-path
feat: allow to override the path to the Symfony Console
2 parents 5575c46 + a2d7850 commit 6925d21

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

commands/completion_posix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func autocompleteSymfonyConsoleWrapper(context *console.Context, words complete.
5050
// Composer does not support those options yet, so we only use them for Symfony Console
5151
args = append(args, "-a1", fmt.Sprintf("-s%s", console.GuessShell()))
5252

53-
if executor, err := php.SymfonyConsoleExecutor(args); err == nil {
53+
if executor, err := php.SymfonyConsoleExecutor(terminal.Logger, args); err == nil {
5454
os.Exit(executor.Execute(false))
5555
}
5656

envs/dotenv.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ func LoadDotEnv(vars map[string]string, scriptDir string) map[string]string {
5050
return vars
5151
}
5252

53+
// LookupEnv allows one to lookup for a single environment variable in the same
54+
// way os.LookupEnv would. It automatically let the environment variable take
55+
// over if defined.
56+
func LookupEnv(dotEnvDir, key string) (string, bool) {
57+
// first check if the user defined it in its environment
58+
if value, isUserDefined := os.LookupEnv(key); isUserDefined {
59+
return value, isUserDefined
60+
}
61+
62+
dotEnvEnv := lookupDotEnv(dotEnvDir)
63+
if value, isDefined := dotEnvEnv[key]; isDefined {
64+
return value, isDefined
65+
}
66+
67+
return "", false
68+
}
69+
5370
// algorithm is here: https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/3.3/config/bootstrap.php
5471
func lookupDotEnv(dir string) map[string]string {
5572
var err error

local/php/symfony.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,34 @@ package php
2121

2222
import (
2323
"os"
24-
2524
"path/filepath"
2625

2726
"github.com/pkg/errors"
27+
"github.com/rs/zerolog"
28+
"github.com/symfony-cli/symfony-cli/envs"
2829
)
2930

3031
// SymfonyConsoleExecutor returns an Executor prepared to run Symfony Console.
3132
// It returns an error if no console binary is found.
32-
func SymfonyConsoleExecutor(args []string) (*Executor, error) {
33+
func SymfonyConsoleExecutor(logger zerolog.Logger, args []string) (*Executor, error) {
3334
dir, err := os.Getwd()
3435
if err != nil {
3536
return nil, errors.WithStack(err)
3637
}
3738

3839
for {
39-
for _, consolePath := range []string{"bin/console", "app/console"} {
40+
consolePaths := []string{"bin/console", "app/console"}
41+
if consolePath, isConsolePathSpecified := envs.LookupEnv(dir, "SYMFONY_CONSOLE_PATH"); isConsolePathSpecified {
42+
consolePaths = []string{consolePath}
43+
}
44+
45+
for _, consolePath := range consolePaths {
46+
logger.Debug().Str("consolePath", consolePath).Str("directory", dir).Msgf("Looking for Symfony console")
4047
consolePath = filepath.Join(dir, consolePath)
4148
if _, err := os.Stat(consolePath); err == nil {
4249
return &Executor{
4350
BinName: "php",
51+
Logger: logger,
4452
Args: append([]string{"php", consolePath}, args...),
4553
}, nil
4654
}

main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ func main() {
7474
}
7575
// called via "symfony console"?
7676
if len(args) >= 2 && args[1] == "console" {
77-
if executor, err := php.SymfonyConsoleExecutor(args[2:]); err == nil {
78-
executor.Logger = terminal.Logger
77+
if executor, err := php.SymfonyConsoleExecutor(terminal.Logger, args[2:]); err == nil {
7978
executor.ExtraEnv = getCliExtraEnv()
8079
os.Exit(executor.Execute(false))
8180
39CE }

0 commit comments

Comments
 (0)
0