Skip to content
Snippets Groups Projects
Utils.php 5.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php /** @noinspection UnusedConstructorDependenciesInspection */
    
    jurgenhaas's avatar
    jurgenhaas committed
    
    namespace LakeDrops\Component\Composer;
    
    use Composer\Composer;
    use Composer\Json\JsonFile;
    
    jurgenhaas's avatar
    jurgenhaas committed
    use Exception;
    
    jurgenhaas's avatar
    jurgenhaas committed
    
    /**
     * Manages composer.json files.
     */
    final class Utils {
    
      /**
       * The composer object running this session.
       *
       * @var \Composer\Composer
       */
    
    jurgenhaas's avatar
    jurgenhaas committed
    
      /**
    
    jurgenhaas's avatar
    jurgenhaas committed
       *
       * @var array
       */
    
    jurgenhaas's avatar
    jurgenhaas committed
    
      /**
    
       * The path of the directory of the composer.json file.
    
    jurgenhaas's avatar
    jurgenhaas committed
       *
       * @var string
       */
    
    jurgenhaas's avatar
    jurgenhaas committed
    
      /**
       * The full filename of the composer.json file.
       *
       * @var string
       */
    
      private $composerJsonPath;
    
      /**
       * @var string[]
       */
      private static $ignoredGitPatterns;
    
    jurgenhaas's avatar
    jurgenhaas committed
    
    
      /**
       * @var string
       */
      private static $lfsGitPatterns;
    
    
    jurgenhaas's avatar
    jurgenhaas committed
      /**
       * Utils constructor.
       *
       * @param \Composer\Composer $composer
       *   The composer object running this session.
    
       * @param string|null $sourcePath
       *   The path name if not the project's root directory.
    
    jurgenhaas's avatar
    jurgenhaas committed
       */
    
      public function __construct(Composer $composer, $sourcePath = NULL) {
    
    jurgenhaas's avatar
    jurgenhaas committed
        $this->composer = $composer;
    
        $this->sourcePath = $sourcePath ?? getcwd() . $composer->getPackage()->getTargetDir();
    
    jurgenhaas's avatar
    jurgenhaas committed
        $this->composerJsonPath = $this->sourcePath . '/composer.json';
        $this->read();
      }
    
      /**
       * Read the current content of the project's composer.json file.
       *
       * @return $this
       */
    
    jurgenhaas's avatar
    jurgenhaas committed
        $this->content = [];
        if (file_exists($this->composerJsonPath)) {
          $jsonFile = new JsonFile($this->composerJsonPath);
          $this->content = $jsonFile->read();
        }
        return $this;
      }
    
      /**
       * Write the config to composer.json file.
       *
       * @return $this
       */
    
    jurgenhaas's avatar
    jurgenhaas committed
        $jsonFile = new JsonFile($this->composerJsonPath);
        if (file_exists($this->composerJsonPath)) {
          $content = $jsonFile->read();
    
          if (md5(json_encode($content)) === md5(json_encode($this->content))) {
    
    jurgenhaas's avatar
    jurgenhaas committed
            // Nothing has changed.
            return $this;
          }
        }
    
        try {
          $jsonFile->write($this->content);
        }
    
    jurgenhaas's avatar
    jurgenhaas committed
        catch (Exception $ex) {
    
          // Let's ignore this, if composer.json is read-only there is a general problem.
        }
    
    jurgenhaas's avatar
    jurgenhaas committed
        return $this;
      }
    
      /**
       * Get settings for [$key].
       *
       * @param string $key
       *   The key for the section.
       *
       * @return array|string
       *   The settings of the key.
       */
      public function getSection($key) {
    
    jurgenhaas's avatar
    jurgenhaas committed
      }
    
      /**
       * Get settings for [$key1][$key2].
       *
       * @param string $key1
       *   The key for the section.
       * @param string $key2
       *   The key for the sub-section.
       *
       * @return array|string
       *   The settings of the key.
       */
      public function getSubSection($key1, $key2) {
        $section = $this->getSection($key1);
    
    jurgenhaas's avatar
    jurgenhaas committed
      }
    
    
      /**
       * Get settings for [$key1][$key2][$key3].
       *
       * @param string $key1
       *   The key for the section.
       * @param string $key2
       *   The key for the sub-section.
       * @param string $key3
       *   The key for the sub-sub-section.
       *
       * @return array|string
       *   The settings of the key.
       */
      public function getSubSubSection($key1, $key2, $key3) {
        $subSection = $this->getSubSection($key1, $key2);
    
    jurgenhaas's avatar
    jurgenhaas committed
      /**
       * Set settings for [$key].
       *
       * @param string $key
       *   The key for the section.
       * @param array|string $value
       *   The value for the section.
       *
       * @return $this
       */
    
      public function setSection($key, $value): self {
    
    jurgenhaas's avatar
    jurgenhaas committed
        $this->content[$key] = $value;
        return $this;
      }
    
      /**
       * Set settings for [$key1][$key2].
       *
       * @param string $key1
       *   The key for the section.
       * @param string $key2
       *   The key for the sub-section.
       * @param array|string $value
       *   The value for the sub-section.
       *
       * @return $this
       */
    
      public function setSubSection($key1, $key2, $value): self {
    
    jurgenhaas's avatar
    jurgenhaas committed
        $this->content[$key1][$key2] = $value;
        return $this;
      }
    
    
      /**
       * Make sure that git init was called.
       */
      protected static function gitInit() {
        if (!file_exists('.git')) {
          passthru(escapeshellcmd('git init'));
        }
      }
    
    
      /**
       * @param string $command
       */
      public static function git($command) {
    
        passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command));
      }
    
      /**
       * @param string $pattern
       */
      public static function gitIgnore($pattern) {
        if (self::$ignoredGitPatterns === NULL) {
          if (file_exists('.gitignore')) {
            self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore'));
          }
          else {
            self::$ignoredGitPatterns = [];
          }
        }
    
    
    jurgenhaas's avatar
    jurgenhaas committed
        if (in_array($pattern, self::$ignoredGitPatterns, TRUE)) {
    
          return;
        }
    
        self::$ignoredGitPatterns[] = $pattern;
        self::git('ignore ' . $pattern);
      }
    
    
      /**
       * @param string $pattern
       */
      public static function gitLFS($pattern) {
        if (self::$lfsGitPatterns === NULL) {
    
          $output = [];
          exec('git lfs track', $output);
          self::$lfsGitPatterns = implode(PHP_EOL, $output);
        }
    
        if (strpos(self::$lfsGitPatterns, $pattern)) {
          return;
        }
    
        self::$lfsGitPatterns = $pattern . PHP_EOL;
        self::git('lfs track ' . $pattern);
      }
    
    
    jurgenhaas's avatar
    jurgenhaas committed
    }