Skip to content
Snippets Groups Projects
Utils.php 3.69 KiB
Newer Older
  • Learn to ignore specific revisions
  • jurgenhaas's avatar
    jurgenhaas committed
    <?php
    
    namespace LakeDrops\Component\Composer;
    
    use Composer\Composer;
    use Composer\Json\JsonFile;
    
    /**
     * Manages composer.json files.
     */
    final class Utils {
    
      /**
       * The composer object running this session.
       *
       * @var \Composer\Composer
       */
      protected $composer;
    
      /**
    
    jurgenhaas's avatar
    jurgenhaas committed
       *
       * @var array
       */
      protected $content;
    
      /**
    
       * The path of the directory of the composer.json file.
    
    jurgenhaas's avatar
    jurgenhaas committed
       *
       * @var string
       */
      protected $sourcePath;
    
      /**
       * The full filename of the composer.json file.
       *
       * @var string
       */
      protected $composerJsonPath;
    
      /**
       * 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 = isset($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
       */
      public function read() {
        $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
       */
      public function write() {
        $jsonFile = new JsonFile($this->composerJsonPath);
        if (file_exists($this->composerJsonPath)) {
          $content = $jsonFile->read();
          if (md5(json_encode($content)) == md5(json_encode($this->content))) {
            // Nothing has changed.
            return $this;
          }
        }
        $jsonFile->write($this->content);
        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) {
        return isset($this->content[$key]) ? $this->content[$key] : [];
      }
    
      /**
       * 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);
        return isset($section[$key2]) ? $section[$key2] : [];
      }
    
    
      /**
       * 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);
        return isset($subSection[$key3]) ? $subSection[$key3] : [];
      }
    
    
    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) {
        $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) {
        $this->content[$key1][$key2] = $value;
        return $this;
      }
    
    }