<commit_before><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Drupal\AppConsole\Generator;

use Drupal\AppConsole\Utils\Utils;

class Generator
{
  private $skeletonDirs;

  /**
   * Sets an array of directories to look for templates.
   *
   * The directories must be sorted from the most specific to the most
   * directory.
   *
   * @param array $skeletonDirs An array of skeleton dirs
   */
  public function setSkeletonDirs($skeletonDirs)
  {
    $this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
  }

  protected function render($template, $parameters)
  {
    $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($this->skeletonDirs), array(
      'debug'            => true,
      'cache'            => false,
      'strict_variables' => true,
      'autoescape'       => false,
    ));

    $twig->addFunction($this->getServiceAsParamater());
    $twig->addFunction($this->getServiceAsParamaterKeys());
    $twig->addFunction($this->getArgumentsFromRoute());

    return $twig->render($template, $parameters);
  }

  protected function renderFile($template, $target, $parameters, $flag=null)
  {
    if (!is_dir(dirname($target))) {
        mkdir(dirname($target), 0777, true);
    }

    return file_put_contents($target, $this->render($template, $parameters), $flag);
  }

  public function camelCaseToUnderscore($camel_case)
  {
    return Utils::camelCaseToUnderscore($camel_case);
  }

  public function getServiceAsParamater()
  {
      $servicesAsParameters = new \Twig_SimpleFunction('servicesAsParameters', function ($services) {
        $parameters = [];
        foreach ($services as $service) {
          $parameters[] = sprintf('%s $%s', $service['short'], $service['machine_name']);
        }

        return $parameters;
      });

      return $servicesAsParameters;
  }

  public function getServiceAsParamaterKeys()
  {
    $servicesAsParametersKeys = new \Twig_SimpleFunction('servicesAsParametersKeys', function ($services) {
      $parameters = [];
      foreach ($services as $service) {
        $parameters[] = sprintf('"@%s"', $service['name']);
      }

      return $parameters;
    });

    return $servicesAsParametersKeys;
  }

  public function getArgumentsFromRoute()
  {
    $argumentsFromRoute = new \Twig_SimpleFunction('argumentsFromRoute', function ($route){
      $parameters = array_filter(explode("/", $route), function($value){
        return (strpos($value, "}") > 0) ? : false;
      });
      $parameters = array_map(function ($value){
        return "$".substr($value, 1, -1);
      }, $parameters);

      return $parameters;
    });

    return $argumentsFromRoute;
  }
}
<commit_msg>Rename function getServiceAsParamater to getServicesAsParameters<commit_after><?php

/*
 * This file is part of the Symfony package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Drupal\AppConsole\Generator;

use Drupal\AppConsole\Utils\Utils;

class Generator
{
  private $skeletonDirs;

  /**
   * Sets an array of directories to look for templates.
   *
   * The directories must be sorted from the most specific to the most
   * directory.
   *
   * @param array $skeletonDirs An array of skeleton dirs
   */
  public function setSkeletonDirs($skeletonDirs)
  {
    $this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
  }

  protected function render($template, $parameters)
  {
    $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($this->skeletonDirs), array(
      'debug'            => true,
      'cache'            => false,
      'strict_variables' => true,
      'autoescape'       => false,
    ));

    $twig->addFunction($this->getServicesAsParameters());
    $twig->addFunction($this->getServiceAsParamaterKeys());
    $twig->addFunction($this->getArgumentsFromRoute());

    return $twig->render($template, $parameters);
  }

  protected function renderFile($template, $target, $parameters, $flag=null)
  {
    if (!is_dir(dirname($target))) {
        mkdir(dirname($target), 0777, true);
    }

    return file_put_contents($target, $this->render($template, $parameters), $flag);
  }

  public function camelCaseToUnderscore($camel_case)
  {
    return Utils::camelCaseToUnderscore($camel_case);
  }

  public function getServicesAsParameters()
  {
      $servicesAsParameters = new \Twig_SimpleFunction('servicesAsParameters', function ($services) {
        $parameters = [];
        foreach ($services as $service) {
          $parameters[] = sprintf('%s $%s', $service['short'], $service['machine_name']);
        }

        return $parameters;
      });

      return $servicesAsParameters;
  }

  public function getServiceAsParamaterKeys()
  {
    $servicesAsParametersKeys = new \Twig_SimpleFunction('servicesAsParametersKeys', function ($services) {
      $parameters = [];
      foreach ($services as $service) {
        $parameters[] = sprintf('"@%s"', $service['name']);
      }

      return $parameters;
    });

    return $servicesAsParametersKeys;
  }

  public function getArgumentsFromRoute()
  {
    $argumentsFromRoute = new \Twig_SimpleFunction('argumentsFromRoute', function ($route){
      $parameters = array_filter(explode("/", $route), function($value){
        return (strpos($value, "}") > 0) ? : false;
      });
      $parameters = array_map(function ($value){
        return "$".substr($value, 1, -1);
      }, $parameters);

      return $parameters;
    });

    return $argumentsFromRoute;
  }
}

{'fraction_non_alphanumeric': 0.1370953782239917, 'fraction_numerical': 0.0024234031504240957, 'mean_word_length': 3.4275862068965517, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5295872', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1686, 'n_words': 384}