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

use Exception;
use Illuminate\Support\Str;
use Rocketeer\Traits\HasLocator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
 * Handles interaction between the User provided informations
 * and the various Rocketeer components
 *
 * @author <NAME> <<EMAIL>>
 */
class Rocketeer
{
	use HasLocator;

	/**
	 * The Rocketeer version
	 *
	 * @var string
	 */
	const VERSION = '2.0.0';

	/**
	 * Returns what stage Rocketeer thinks he's in
	 *
	 * @param string      $application
	 * @param string|null $path
	 *
	 * @return string|false
	 */
	public static function getDetectedStage($application = 'application', $path = null)
	{
		$current = $path ?: realpath(__DIR__);
		preg_match('/'.$application.'\/([a-zA-Z0-9_-]+)\/releases\/([0-9]{14})/', $current, $matches);

		return isset($matches[1]) ? $matches[1] : false;
	}

	//////////////////////////////////////////////////////////////////////
	//////////////////////////// CONFIGURATION ///////////////////////////
	//////////////////////////////////////////////////////////////////////

	/**
	 * Get the name of the application to deploy
	 *
	 * @return string
	 */
	public function getApplicationName()
	{
		return $this->config->get('rocketeer::application_name');
	}

	/**
	 * Get an option from Rocketeer's config file
	 *
	 * @param  string $option
	 *
	 * @return mixed
	 */
	public function getOption($option)
	{
		$original = $this->config->get('rocketeer::'.$option);

		if ($contextual = $this->getContextualOption($option, 'stages', $original)) {
			return $contextual;
		}

		if ($contextual = $this->getContextualOption($option, 'connections', $original)) {
			return $contextual;
		}

		return $original;
	}

	/**
	 * Get a contextual option
	 *
	 * @param  string            $option
	 * @param  string            $type [stage,connection]
	 * @param  string|array|null $original
	 *
	 * @return string|array|\Closure
	 */
	protected function getContextualOption($option, $type, $original = null)
	{
		// Switch context
		switch ($type) {
			case 'stages':
				$contextual = sprintf('rocketeer::on.stages.%s.%s', $this->connections->getStage(), $option);
				break;

			case 'connections':
				$contextual = sprintf('rocketeer::on.connections.%s.%s', $this->connections->getConnection(), $option);
				break;

			default:
				$contextual = sprintf('rocketeer::%s', $option);
				break;
		}

		// Merge with defaults
		$value = $this->config->get($contextual);
		if (is_array($value) and $original) {
			$value = array_replace($original, $value);
		}

		return $value;
	}

	/**
	 * Merge the various contextual configurations defined in userland
	 */
	public function mergeContextualConfigurations()
	{
		$finder  = new Finder();
		$storage = $this->app['path.rocketeer.config'];
		$files   = $finder->in($storage.'/{stages,connections}/*')->notName('config.php')->files();
		$files   = iterator_to_array($files);

		foreach ($files as $file) {
			$contents = include $file->getPathname();
			$handle   = $this->computeHandleFromPath($file);

			$this->config->set($handle, $contents);
		}
	}

	/**
	 * @param SplFileInfo $file
	 *
	 * @return string
	 */
	protected function computeHandleFromPath(SplFileInfo $file)
	{
		// Get realpath
		$handle = $file->getRealpath();

		// Format appropriately
		$handle = str_replace($this->app['path.rocketeer.config'].DIRECTORY_SEPARATOR, null, $handle);
		$handle = str_replace('.php', null, $handle);
		$handle = str_replace(DIRECTORY_SEPARATOR, '.', $handle);

		return sprintf('rocketeer::on.%s', $handle);
	}

	////////////////////////////////////////////////////////////////////
	//////////////////////////////// PATHS /////////////////////////////
	////////////////////////////////////////////////////////////////////

	/**
	 * Get a configured path
	 *
	 * @param string $path
	 *
	 * @return string
	 */
	public function getPath($path)
	{
		return $this->getOption('paths.'.$path);
	}

	/**
	 * Replace patterns in a folder path
	 *
	 * @param  string $path
	 *
	 * @return string
	 */
	public function replacePatterns($path)<commit_msg>Use DS<commit_after><?php
/*
 * This file is part of Rocketeer
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Rocketeer;

use Exception;
use Illuminate\Support\Str;
use Rocketeer\Traits\HasLocator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
 * Handles interaction between the User provided informations
 * and the various Rocketeer components
 *
 * @author <NAME> <<EMAIL>>
 */
class Rocketeer
{
	use HasLocator;

	/**
	 * The Rocketeer version
	 *
	 * @var string
	 */
	const VERSION = '2.0.0';

	/**
	 * Returns what stage Rocketeer thinks he's in
	 *
	 * @param string      $application
	 * @param string|null $path
	 *
	 * @return string|false
	 */
	public static function getDetectedStage($application = 'application', $path = null)
	{
		$current = $path ?: realpath(__DIR__);
		preg_match('/'.$application.'\/([a-zA-Z0-9_-]+)\/releases\/([0-9]{14})/', $current, $matches);

		return isset($matches[1]) ? $matches[1] : false;
	}

	//////////////////////////////////////////////////////////////////////
	//////////////////////////// CONFIGURATION ///////////////////////////
	//////////////////////////////////////////////////////////////////////

	/**
	 * Get the name of the application to deploy
	 *
	 * @return string
	 */
	public function getApplicationName()
	{
		return $this->config->get('rocketeer::application_name');
	}

	/**
	 * Get an option from Rocketeer's config file
	 *
	 * @param  string $option
	 *
	 * @return mixed
	 */
	public function getOption($option)
	{
		$original = $this->config->get('rocketeer::'.$option);

		if ($contextual = $this->getContextualOption($option, 'stages', $original)) {
			return $contextual;
		}

		if ($contextual = $this->getContextualOption($option, 'connections', $original)) {
			return $contextual;
		}

		return $original;
	}

	/**
	 * Get a contextual option
	 *
	 * @param  string            $option
	 * @param  string            $type [stage,connection]
	 * @param  string|array|null $original
	 *
	 * @return string|array|\Closure
	 */
	protected function getContextualOption($option, $type, $original = null)
	{
		// Switch context
		switch ($type) {
			case 'stages':
				$contextual = sprintf('rocketeer::on.stages.%s.%s', $this->connections->getStage(), $option);
				break;

			case 'connections':
				$contextual = sprintf('rocketeer::on.connections.%s.%s', $this->connections->getConnection(), $option);
				break;

			default:
				$contextual = sprintf('rocketeer::%s', $option);
				break;
		}

		// Merge with defaults
		$value = $this->config->get($contextual);
		if (is_array($value) and $original) {
			$value = array_replace($original, $value);
		}

		return $value;
	}

	/**
	 * Merge the various contextual configurations defined in userland
	 */
	public function mergeContextualConfigurations()
	{
		$finder  = new Finder();
		$storage = $this->app['path.rocketeer.config'];
		$files   = $finder->in($storage.'/{stages,connections}/*')->notName('config.php')->files();
		$files   = iterator_to_array($files);

		foreach ($files as $file) {
			$contents = include $file->getPathname();
			$handle   = $this->computeHandleFromPath($file);

			$this->config->set($handle, $contents);
		}
	}

	/**
	 * @param SplFileInfo $file
	 *
	 * @return string
	 */
	protected function computeHandleFromPath(SplFileInfo $file)
	{
		// Get realpath
		$handle = $file->getRealpath();

		// Format appropriately
		$handle = str_replace($this->app['path.rocketeer.config'].DS, null, $handle);
		$handle = str_replace('.php', null, $handle);
		$handle = str_replace(DS, '.', $handle);

		return sprintf('rocketeer::on.%s', $handle);
	}

	////////////////////////////////////////////////////////////////////
	//////////////////////////////// PATHS /////////////////////////////
	////////////////////////////////////////////////////////////////////

	/**
	 * Get a configured path
	 *
	 * @param string $path
	 *
	 * @return string
	 */
	public function getPath($path)
	{
		return $this->getOption('paths.'.$path);
	}

	/**
	 * Replace patterns in a folder path
	 *
	 * @param  string $path
	 *
	 * @return string
	 */
	public function replacePatterns($path)
{'fraction_non_alphanumeric': 0.23655164401068898, 'fraction_numerical': 0.0025560590217265015, 'mean_word_length': 2.4432, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 61, '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': '6059566', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2520, 'n_words': 594}