<commit_before><?php
namespace Caffeinated\Modules\Console\Commands;

use Caffeinated\Modules\Modules;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ModuleMigrateResetCommand extends Command
{
    use ConfirmableTrait;

	/**
	 * @var string $name The console command name.
	 */
	protected $name = 'module:migrate:reset';

	/**
	 * @var string $description The console command description.
	 */
	protected $description = 'Rollback all database migrations for a specific or all modules';

	/**
	 * @var \Caffeinated\Modules\Modules
	 */
	protected $module;

	/**
	 * @var \Illuminate\Database\Migrations\Migrator
	 */
	protected $migrator;

	/**
	 * @var \Illuminate\Filesystem\Filesystem
	 */
	protected $files;

	/**
	 * Create a new command instance.
	 *
	 * @param \Caffeinated\Modules\Modules             $module
	 * @param \Illuminate\Filesystem\Filesystem        $files
	 * @param \Illuminate\Database\Migrations\Migrator $migrator
	 */
	public function __construct(Modules $module, Filesystem $files, Migrator $migrator)
	{
		parent::__construct();

		$this->module   = $module;
		$this->files    = $files;
		$this->migrator = $migrator;
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        if (! $this->confirmToProceed()) return null;

		$module = $this->argument('module');

		if (! empty($module)) {
			if ($this->module->isEnabled($module)) {
				return $this->reset($module);
			} elseif ($this->option('force')) {
				return $this->reset($module);
			}
		} else {
			if ($this->option('force')) {
				$modules = $this->module->all();
				$modules = $modules->reverse();
			} else {
				$modules = $this->module->enabled()->reverse();
			}

			foreach ($modules as $module) {
				$this->reset($module['slug']);
			}
		}
	}

	/**
	 * Run the migration reset for the specified module.
	 *
	 * Migrations should be reset in the reverse order that they were
	 * migrated up as. This ensures the database is properly reversed
	 * without conflict.
	 *
	 * @param  string $slug
	 * @return mixed
	 */
	protected function reset($slug)
	{
		$this->migrator->setconnection($this->input->getOption('database'));

		$pretend       = $this->input->getOption('pretend');
		$migrationPath = $this->getMigrationPath($slug);
		$migrations    = array_reverse($this->migrator->getMigrationFiles($migrationPath));

		if (count($migrations) == 0) {
			return $this->error('Nothing to rollback.');
		}

		foreach ($migrations as $migration) {
			$this->info('Migration: '.$migration);
			$this->runDown($slug, $migration, $pretend);
		}
	}

	/**
	 * Run "down" a migration instance.
	 *
	 * @param  string $slug
	 * @param  object $migration
	 * @param  bool   $pretend
	 * @return void
	 */
	protected function runDown($slug, $migration, $pretend)
	{
		$migrationPath = $this->getMigrationPath($slug);
		$file          = (string) $migrationPath.'/'.$migration.'.php';
		$classFile     = implode('_', array_slice(explode('_', str_replace('.php', '', $file)), 4));
		$class         = studly_case($classFile);
		$table         = $this->laravel['config']['database.migrations'];

		include ($file);

		$instance = new $class;
		$instance->down();

		$this->laravel['db']->table($table)
			->where('migration', $migration)
			->delete();
	}

	/**
	 * Get the console command parameters.
	 *
	 * @param  string $slug<commit_msg>Use the basename function to extract filename without file extension. explode cause wrong result with directory has "_" character.<commit_after><?php
namespace Caffeinated\Modules\Console\Commands;

use Caffeinated\Modules\Modules;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class ModuleMigrateResetCommand extends Command
{
    use ConfirmableTrait;

	/**
	 * @var string $name The console command name.
	 */
	protected $name = 'module:migrate:reset';

	/**
	 * @var string $description The console command description.
	 */
	protected $description = 'Rollback all database migrations for a specific or all modules';

	/**
	 * @var \Caffeinated\Modules\Modules
	 */
	protected $module;

	/**
	 * @var \Illuminate\Database\Migrations\Migrator
	 */
	protected $migrator;

	/**
	 * @var \Illuminate\Filesystem\Filesystem
	 */
	protected $files;

	/**
	 * Create a new command instance.
	 *
	 * @param \Caffeinated\Modules\Modules             $module
	 * @param \Illuminate\Filesystem\Filesystem        $files
	 * @param \Illuminate\Database\Migrations\Migrator $migrator
	 */
	public function __construct(Modules $module, Filesystem $files, Migrator $migrator)
	{
		parent::__construct();

		$this->module   = $module;
		$this->files    = $files;
		$this->migrator = $migrator;
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        if (! $this->confirmToProceed()) return null;

		$module = $this->argument('module');

		if (! empty($module)) {
			if ($this->module->isEnabled($module)) {
				return $this->reset($module);
			} elseif ($this->option('force')) {
				return $this->reset($module);
			}
		} else {
			if ($this->option('force')) {
				$modules = $this->module->all();
				$modules = $modules->reverse();
			} else {
				$modules = $this->module->enabled()->reverse();
			}

			foreach ($modules as $module) {
				$this->reset($module['slug']);
			}
		}
	}

	/**
	 * Run the migration reset for the specified module.
	 *
	 * Migrations should be reset in the reverse order that they were
	 * migrated up as. This ensures the database is properly reversed
	 * without conflict.
	 *
	 * @param  string $slug
	 * @return mixed
	 */
	protected function reset($slug)
	{
		$this->migrator->setconnection($this->input->getOption('database'));

		$pretend       = $this->input->getOption('pretend');
		$migrationPath = $this->getMigrationPath($slug);
		$migrations    = array_reverse($this->migrator->getMigrationFiles($migrationPath));

		if (count($migrations) == 0) {
			return $this->error('Nothing to rollback.');
		}

		foreach ($migrations as $migration) {
			$this->info('Migration: '.$migration);
			$this->runDown($slug, $migration, $pretend);
		}
	}

	/**
	 * Run "down" a migration instance.
	 *
	 * @param  string $slug
	 * @param  object $migration
	 * @param  bool   $pretend
	 * @return void
	 */
	protected function runDown($slug, $migration, $pretend)
	{
		$migrationPath = $this->getMigrationPath($slug);
		$file          = (string) $migrationPath.'/'.$migration.'.php';
		$classFile     = implode('_', array_slice(explode('_', basename($file, '.php')), 4));
		$class         = studly_case($classFile);
		$table         = $this->laravel['config']['database.migrations'];

		include ($file);

		$instance = new $class;
		$instance->down();

		$this->laravel['db']->table($table)
			->where('migration', $migration)
			->delete();
	}

	/**
	 * Get the console command parameters.
	 *
	 * @param  string $slug
{'fraction_non_alphanumeric': 0.15850976079186144, 'fraction_numerical': 0.0005499037668408029, 'mean_word_length': 3.5870113493064313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '6265610', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2308, 'n_words': 514}