<commit_before><?php namespace Cerbero\Workflow\Scaffolding;

use \Illuminate\Filesystem\Filesystem;
use Cerbero\Workflow\Scaffolding\CompilerInterface as Compiler;
use Cerbero\Workflow\WorkflowDataTransfer as Workflow;
use Cerbero\Workflow\FileCreationException;

/**
 * Scaffolding generator.
 *
 * @author	<NAME>
 */
class Generator implements GeneratorInterface
{

	/**
	 * @author	<NAME>
	 * @var		Illuminate\Filesystem\Filesystem	$file	Laravel filesystem.
	 */
	protected $file;

	/**
	 * @author	<NAME>
	 * @var		Cerbero\Workflow\Scaffolding\CompilerInterface	$compiler	Template files compiler.
	 */
	protected $compiler;

	/**
	 * @author	<NAME>
	 * @var		Cerbero\Workflow\WorkflowDataTransfer	$workflow	Workflow data transfer.
	 */
	protected $workflow;

	/**
	 * @author	<NAME>
	 * @var		string	$directory	The main directory.
	 */
	protected $directory;
	
	/**
	 * Set the dependencies.
	 *
	 * @author	<NAME>
	 * @param	Illuminate\Filesystem\Filesystem	$file
	 * @param	Cerbero\Workflow\Scaffolding\CompilerInterface	$compiler
	 * @return	void
	 */
	public function __construct(Filesystem $file, Compiler $compiler)
	{
		$this->file = $file;

		$this->compiler = $compiler;
	}

	/**
	 * Generate the scaffolding.
	 *
	 * @author	<NAME>
	 * @param	Cerbero\Workflow\WorkflowDataTransfer	$workflow
	 * @return	boolean
	 */
	public function generate(Workflow $workflow)
	{
		$this->workflow = $workflow;

		$this->createMainDirectory();

		$this->createWorkflow();

		$this->appendBindings();
	}

	/**
	 * Create the workflow structure.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function createWorkflow()
	{
		$name = $this->workflow->name;

		$this->createDirectory("{$name}");

		$this->createFileFromTemplate('interface', "{$name}/{$name}Interface.php");

		$this->createFileFromTemplate('class', "{$name}/{$name}.php");

		$this->createDecorators($this->workflow->decorators);
	}

	/**
	 * Create the directory to put workflows in.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function createMainDirectory()
	{
		$this->directory = app_path($this->workflow->folder);

		if( ! $this->file->isDirectory($this->directory))
		{
			$this->createDirectory();

			$this->createFileFromTemplate('empty', "bindings.php");
		}
	}

	/**
	 * Create a directory.
	 *
	 * @author	<NAME>
	 * @param	string|null	$directory
	 * @return	void
	 */
	protected function createDirectory($directory = null)
	{
		$path = "{$this->directory}/{$directory}";

		$success = $this->file->makeDirectory($path);

		if( ! $success) throw new FileCreationException($path);
	}

	/**
	 * Create a file from a template.
	 *
	 * @author	<NAME>
	 * @param	string	$template
	 * @param	string	$file
	 * @return	void
	 */
	protected function createFileFromTemplate($template, $file)
	{
		$path = "{$this->directory}/{$file}";

		$content = $this->compiler->compile($template, $this->workflow);

		$success = $this->file->put($path, $content);

		if( ! $success) throw new FileCreationException($path);
	}

	/**
	 * Create the decorators.
	 *
	 * @author	<NAME>
	 * @param	array	$decorators
	 * @return	void
	 */
	protected function createDecorators(array $decorators = array())
	{
		$folder = "{$this->workflow->name}/decorators";

		$this->createDirectory($folder);

		foreach ($decorators as $decorator)
		{
			$this->workflow->decorator = $decorator;

			$this->createFileFromTemplate('decorator', "{$folder}/{$decorator}.php");
		}
	}

	/**
	 * Append the bindings to register.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function appendBindings()
	{
		$content = $this->compiler->compile('binding', $this->workflow);

		$success = $this->file->append("{$this->directory}/bindings.php", $content);

		if( ! $success) throw new \Exception('Unable to register the bindings');
	}

}<commit_msg>Added method to generate decorators depending on their own names<commit_after><?php namespace Cerbero\Workflow\Scaffolding;

use \Illuminate\Filesystem\Filesystem;
use Cerbero\Workflow\Scaffolding\CompilerInterface as Compiler;
use Cerbero\Workflow\WorkflowDataTransfer as Workflow;
use Cerbero\Workflow\FileCreationException;

/**
 * Scaffolding generator.
 *
 * @author	<NAME>
 */
class Generator implements GeneratorInterface
{

	/**
	 * @author	<NAME>
	 * @var		Illuminate\Filesystem\Filesystem	$file	Laravel filesystem.
	 */
	protected $file;

	/**
	 * @author	<NAME>
	 * @var		Cerbero\Workflow\Scaffolding\CompilerInterface	$compiler	Template files compiler.
	 */
	protected $compiler;

	/**
	 * @author	<NAME>
	 * @var		Cerbero\Workflow\WorkflowDataTransfer	$workflow	Workflow data transfer.
	 */
	protected $workflow;

	/**
	 * @author	<NAME>
	 * @var		string	$directory	The main directory.
	 */
	protected $directory;
	
	/**
	 * Set the dependencies.
	 *
	 * @author	<NAME>
	 * @param	Illuminate\Filesystem\Filesystem	$file
	 * @param	Cerbero\Workflow\Scaffolding\CompilerInterface	$compiler
	 * @return	void
	 */
	public function __construct(Filesystem $file, Compiler $compiler)
	{
		$this->file = $file;

		$this->compiler = $compiler;
	}

	/**
	 * Generate the scaffolding.
	 *
	 * @author	<NAME>
	 * @param	Cerbero\Workflow\WorkflowDataTransfer	$workflow
	 * @return	boolean
	 */
	public function generate(Workflow $workflow)
	{
		$this->workflow = $workflow;

		$this->createMainDirectory();

		$this->createWorkflow();

		$this->appendBindings();
	}

	/**
	 * Create the workflow structure.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function createWorkflow()
	{
		$name = $this->workflow->name;

		$this->createDirectory("{$name}");

		$this->createFileFromTemplate('interface', "{$name}/{$name}Interface.php");

		$this->createFileFromTemplate('class', "{$name}/{$name}.php");

		$this->createDecorators($this->workflow->decorators);
	}

	/**
	 * Create the directory to put workflows in.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function createMainDirectory()
	{
		$this->directory = app_path($this->workflow->folder);

		if( ! $this->file->isDirectory($this->directory))
		{
			$this->createDirectory();

			$this->createFileFromTemplate('empty', "bindings.php");
		}
	}

	/**
	 * Create a directory.
	 *
	 * @author	<NAME>
	 * @param	string|null	$directory
	 * @return	void
	 */
	protected function createDirectory($directory = null)
	{
		$path = "{$this->directory}/{$directory}";

		$success = $this->file->makeDirectory($path);

		if( ! $success) throw new FileCreationException($path);
	}

	/**
	 * Create a file from a template.
	 *
	 * @author	<NAME>
	 * @param	string	$template
	 * @param	string	$file
	 * @return	void
	 */
	protected function createFileFromTemplate($template, $file)
	{
		$path = "{$this->directory}/{$file}";

		$content = $this->compiler->compile($template, $this->workflow);

		$success = $this->file->put($path, $content);

		if( ! $success) throw new FileCreationException($path);
	}

	/**
	 * Create the decorators.
	 *
	 * @author	<NAME>
	 * @param	array	$decorators
	 * @return	void
	 */
	protected function createDecorators(array $decorators = array())
	{
		$folder = "{$this->workflow->name}/decorators";

		$this->createDirectory($folder);

		foreach ($decorators as $decorator)
		{
			$this->workflow->decorator = $decorator;

			$this->createDecoratorByName($decorator, $folder);
		}
	}

	/**
	 * Create a decorator depending on its name.
	 *
	 * @author	<NAME>
	 * @param	string	$name
	 * @param	string	$folder
	 * @return	void
	 */
	protected function createDecoratorByName($name, $folder)
	{
		$path = "{$folder}/{$name}.php";

		$template = str_contains(strtolower($name), 'validat') ? 'validator' : 'decorator';

		$this->createFileFromTemplate($template, $path);
	}

	/**
	 * Append the bindings to register.
	 *
	 * @author	<NAME>
	 * @return	void
	 */
	protected function appendBindings()
	{
		$content = $this->compiler->compile('binding', $this->workflow);

		$success = $this->file->append("{$this->directory}/bindings.php", $content);

		if( ! $success) throw new \Exception('Unable to register the bindings');
	}

}
{'fraction_non_alphanumeric': 0.16190594985655482, 'fraction_numerical': 0.0, 'mean_word_length': 3.9647058823529413, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 117, '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': '3471', 'n_tokens_mistral': 2874, 'n_tokens_neox': 2604, 'n_words': 545}