Skip to content
Snippets Groups Projects
Handler.php 4.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • jurgenhaas's avatar
    jurgenhaas committed
    <?php
    
    namespace LakeDrops\Behat4Drupal;
    
    use Composer\Composer;
    use Composer\IO\IOInterface;
    use Composer\Script\Event;
    use LakeDrops\Component\Dotenv\Dotenv;
    use Symfony\Component\Filesystem\Filesystem;
    use Symfony\Component\Yaml\Yaml;
    
    /**
     * Class Handler.
     *
     * @package LakeDrops\Behat4Drupal
     */
    class Handler {
    
      /**
       * The composer object running this session.
       *
       * @var \Composer\Composer
       */
      protected $composer;
    
      /**
       * The input-output object for the composer session.
       *
       * @var \Composer\IO\IOInterface
       */
      protected $io;
    
      /**
       * Handler constructor.
       *
       * @param \Composer\Composer $composer
       *   The composer object.
       * @param \Composer\IO\IOInterface $io
       *   The input-output object.
       */
      public function __construct(Composer $composer, IOInterface $io) {
        $this->composer = $composer;
        $this->io = $io;
      }
    
      /**
       * Post project create event to execute the scaffolding.
       *
       * @param \Composer\Script\Event $event
       *   The event that triggered the plugin.
       * @param bool $overwrite
       *   Whether to overwrite existing config files.
       */
      public function configureProject(Event $event, $overwrite = FALSE) {
    
        // We only do the fancy stuff for developers.
        if (!$event->isDevMode()) {
          return;
        }
    
        $options = $this->getOptions();
        $fs = new Filesystem();
        $installationManager = $this->composer->getInstallationManager();
    
        // Directory where the root project is being created.
        $projectRoot = getcwd();
        // Directory where this plugin is being installed.
        $pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/behat4drupal'));
    
        // Provide all the required files.
        $twig_loader = new \Twig_Loader_Array([]);
        $twig = new \Twig_Environment($twig_loader);
        foreach ($this->getFiles($projectRoot) as $template => $def) {
          if (!$fs->exists($def['dest'])) {
            $fs->mkdir($def['dest']);
          }
          $twig_loader->setTemplate($template, $template);
          $filename = $twig->render($template, $options);
          $file = $def['dest'] . '/' . $filename;
          if (($overwrite && empty($def['custom'])) || !$fs->exists($file)) {
            $twig_loader->setTemplate($filename, file_get_contents($pluginRoot . '/templates/' . $template . '.twig'));
            $rendered = $twig->render($filename, $options);
            if (!empty($def['add2yaml']) && isset($options[$filename])) {
              $yaml = Yaml::parse($rendered);
              $yaml = array_merge_recursive($yaml, $options[$filename]);
              $rendered = Yaml::dump($yaml, 9, 2);
            }
            if ($fs->exists($file)) {
              if (md5_file($file) == md5($rendered)) {
                continue;
              }
              $orig_file = $file . '.orig';
              if ($fs->exists($orig_file)) {
                $fs->remove($orig_file);
              }
              $fs->rename($file, $orig_file);
            }
            file_put_contents($file, $rendered);
          }
          $fs->chmod($file, 0664);
        }
      }
    
      /**
       * List of files and settings on how to handle them.
       *
       * @param string $projectRoot
       *   Name of the project's root directory.
       *
       * @return array
       *   List of files.
       */
      protected function getFiles($projectRoot) {
        return [
          'behat.yml' => [
            'dest' => $projectRoot . '/tests/behat',
            'add2yaml' => TRUE,
          ],
          'anonymous.feature' => [
            'dest' => $projectRoot . '/tests/behat/features/basic',
          ],
          'FeatureContext.php' => [
            'dest' => $projectRoot . '/tests/behat/bootstrap/context',
            'custom' => TRUE,
          ],
        ];
      }
    
      /**
       * Retrieve configuration for this package.
       *
       * @return array
       *   The settings from the extra configuration.
       */
      protected function getOptions() {
        $projectname = str_replace([' ', '-', '_', '.'], '', basename(getcwd()));
        $env = new Dotenv('behat4drupal', $this->io);
        $env->put('COMPOSE_PROJECT_NAME', $projectname);
        $extra = $this->composer->getPackage()->getExtra() + ['behat4drupal' => []];
        return $extra['behat4drupal'] + [
          'projectname' => $projectname,
        ];
      }
    
    }