Skip to content
Snippets Groups Projects
Handler.php 3.89 KiB
Newer Older
  • Learn to ignore specific revisions
  • jurgenhaas's avatar
    jurgenhaas committed
    <?php
    
    namespace LakeDrops\Behat4Drupal;
    
    
    use LakeDrops\Component\Composer\BaseHandler;
    
    jurgenhaas's avatar
    jurgenhaas committed
    use LakeDrops\Component\Dotenv\Dotenv;
    
    use LakeDrops\Docker4Drupal\Handler as D4DHandler;
    
    jurgenhaas's avatar
    jurgenhaas committed
    use Symfony\Component\Filesystem\Filesystem;
    use Symfony\Component\Yaml\Yaml;
    
    /**
     * Class Handler.
     *
     * @package LakeDrops\Behat4Drupal
     */
    
    class Handler extends BaseHandler {
    
    jurgenhaas's avatar
    jurgenhaas committed
      /**
       * Post project create event to execute the scaffolding.
       *
       * @param bool $overwrite
       *   Whether to overwrite existing config files.
    
       *
       * @throws \Twig_Error_Loader
       * @throws \Twig_Error_Runtime
       * @throws \Twig_Error_Syntax
    
    jurgenhaas's avatar
    jurgenhaas committed
       */
    
      public function configureProject($overwrite = FALSE) {
    
    jurgenhaas's avatar
    jurgenhaas committed
    
        // We only do the fancy stuff for developers.
    
        if (!$this->isDevMode()) {
    
    jurgenhaas's avatar
    jurgenhaas committed
          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);
    
              /** @noinspection SlowArrayOperationsInLoopInspection */
    
    jurgenhaas's avatar
    jurgenhaas committed
              $yaml = array_merge_recursive($yaml, $options[$filename]);
              $rendered = Yaml::dump($yaml, 9, 2);
            }
            if ($fs->exists($file)) {
    
              if (md5_file($file) === md5($rendered)) {
    
    jurgenhaas's avatar
    jurgenhaas committed
                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);
        }
    
    
        $this->git('ignore tests/output');
    
    jurgenhaas's avatar
    jurgenhaas committed
      }
    
      /**
       * 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): array {
    
    jurgenhaas's avatar
    jurgenhaas committed
        return [
          'behat.yml' => [
            'dest' => $projectRoot . '/tests/behat',
            'add2yaml' => TRUE,
          ],
          'anonymous.feature' => [
            'dest' => $projectRoot . '/tests/behat/features/basic',
    
            'custom' => TRUE,
    
    jurgenhaas's avatar
    jurgenhaas committed
          ],
          '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(): array {
    
        $handler = new D4DHandler($this->composer, $this->io);
        $projectname = $handler->getOptions('projectname');
        $webserver = $handler->getOptions('webserver');
    
    
    jurgenhaas's avatar
    jurgenhaas committed
        $env = new Dotenv('behat4drupal', $this->io);
        $extra = $this->composer->getPackage()->getExtra() + ['behat4drupal' => []];
    
        $options = $extra['behat4drupal'] + [
    
    jurgenhaas's avatar
    jurgenhaas committed
          'projectname' => $projectname,
        ];
    
    
        $options['baseurl'] = empty($options['global']) ?
          $webserver['type'] :
          $projectname . '.docker.localhost:8000';
    
    
        return $env->replaceEnvironmentVariables($options);