GrumPHP\Util\Composer::ensureProjectBinDirInSystemPath PHP Method

ensureProjectBinDirInSystemPath() public static method

To make sure this application works the same in CLI and Composer modus, we'll have to ensure that the bin path is always prefixed.
public static ensureProjectBinDirInSystemPath ( string $binDir )
$binDir string
    public static function ensureProjectBinDirInSystemPath($binDir)
    {
        $pathStr = 'PATH';
        if (!isset($_SERVER[$pathStr]) && isset($_SERVER['Path'])) {
            $pathStr = 'Path';
        }
        if (!is_dir($binDir)) {
            return;
        }
        // add the bin dir to the PATH to make local binaries of deps usable in scripts
        $binDir = realpath($binDir);
        $hasBindDirInPath = preg_match('{(^|' . PATH_SEPARATOR . ')' . preg_quote($binDir) . '($|' . PATH_SEPARATOR . ')}', $_SERVER[$pathStr]);
        if (!$hasBindDirInPath && isset($_SERVER[$pathStr])) {
            $_SERVER[$pathStr] = $binDir . PATH_SEPARATOR . getenv($pathStr);
            putenv($pathStr . '=' . $_SERVER[$pathStr]);
        }
    }

Usage Example

Example #1
0
 /**
  * @return Helper\ComposerHelper
  */
 protected function initializeComposerHelper()
 {
     if ($this->composerHelper) {
         return $this->composerHelper;
     }
     try {
         $composerFile = getcwd() . DIRECTORY_SEPARATOR . 'composer.json';
         $configuration = Composer::loadConfiguration();
         Composer::ensureProjectBinDirInSystemPath($configuration->get('bin-dir'));
         $rootPackage = Composer::loadRootPackageFromJson($composerFile, $configuration);
     } catch (RuntimeException $e) {
         $configuration = null;
         $rootPackage = null;
     }
     return $this->composerHelper = new Helper\ComposerHelper($configuration, $rootPackage);
 }