Skip to content

Commit

Permalink
Merge pull request #257 from tucksaun/fix/256
Browse files Browse the repository at this point in the history
Fix Composer script detection when it uses an absolute shebang path
  • Loading branch information
fabpot committed Jan 23, 2023
2 parents 30918aa + 7cfe5a2 commit 75f91e3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
16 changes: 9 additions & 7 deletions local/php/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package php

import (
"bufio"
"bytes"
"crypto/sha512"
"encoding/hex"
Expand Down Expand Up @@ -71,7 +72,7 @@ func Composer(dir string, args, env []string, stdout, stderr, logger io.Writer,
composerBin = "composer2"
}
path, err := e.findComposer(composerBin)
if err != nil || !isComposerPHPScript(path) {
if err != nil || !isPHPScript(path) {
fmt.Fprintln(logger, " WARNING: Unable to find Composer, downloading one. It is recommended to install Composer yourself at https://getcomposer.org/download/")
// we don't store it under bin/ to avoid it being found by findComposer as we want to only use it as a fallback
binDir := filepath.Join(util.GetHomeDir(), "composer")
Expand All @@ -95,19 +96,20 @@ func Composer(dir string, args, env []string, stdout, stderr, logger io.Writer,
return ComposerResult{}
}

// isComposerPHPScript checks that the composer file is indeed a phar/PHP script (not a .bat file)
func isComposerPHPScript(path string) bool {
// isPHPScript checks that the composer file is indeed a phar/PHP script (not a .bat file)
func isPHPScript(path string) bool {
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
magicPrefix := []byte("#!/usr/bin/env php")
byteSlice := make([]byte, len(magicPrefix))
if _, err := file.Read(byteSlice); err != nil {
reader := bufio.NewReader(file)
byteSlice, _, err := reader.ReadLine()
if err != nil {
return false
}
return bytes.Equal(byteSlice, magicPrefix)

return bytes.HasPrefix(byteSlice, []byte("#!/")) && bytes.HasSuffix(byteSlice, []byte("php"))
}

func composerVersion() int {
Expand Down
27 changes: 27 additions & 0 deletions local/php/composer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package php

import (
"path/filepath"

. "gopkg.in/check.v1"
)

type ComposerSuite struct{}

var _ = Suite(&ComposerSuite{})

func (s *ComposerSuite) TestIsComposerPHPScript(c *C) {
dir, err := filepath.Abs("testdata/php_scripts")
c.Assert(err, IsNil)

c.Assert(isPHPScript(filepath.Join(dir, "unknown")), Equals, false)
c.Assert(isPHPScript(filepath.Join(dir, "invalid")), Equals, false)

for _, validScripts := range []string{
"usual-one",
"debian-style",
"custom-one",
} {
c.Assert(isPHPScript(filepath.Join(dir, validScripts)), Equals, true)
}
}
4 changes: 4 additions & 0 deletions local/php/testdata/php_scripts/custom-one
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/opt/homebrew/Cellar/php/8.2.1_1/bin/php
<?php

echo 'Hello World!';
4 changes: 4 additions & 0 deletions local/php/testdata/php_scripts/debian-style
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/php
<?php

echo 'Hello World!';
3 changes: 3 additions & 0 deletions local/php/testdata/php_scripts/invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo 'Hello World!'
4 changes: 4 additions & 0 deletions local/php/testdata/php_scripts/usual-one
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php

echo 'Hello World!';

0 comments on commit 75f91e3

Please sign in to comment.