Artisan command to upgrade to 4.1

Submitted by stygian - 10 years ago

This is a laravel command i wrote to handle the upgrade to 4.1 It covers everything in the upgrade doc except for handling missing method. It should be easily addable if you need it. Make sure to add the following to start/artisan.php Artisan::add(new UpgradeCommand); Artisan::resolve('UpgradeCommand'); Once finished, run the following php artisan auth:reminders-controller

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class UpgradeCommand extends Command {

    /**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'upgrade';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'This command will help with upgrading your current version of laravel to the latest.';

	/**
	 * The order of upgrades starting at 4.0
	 *
	 * @var string[]
	 */
	protected $versions = array('4.0', '4.1');

	/**
	 * The current stable version 
	 *
	 * @var string
	 */
	protected $stableVersion = '4.0';

	/**
	 * The current dev version 
	 *
	 * @var string
	 */
	protected $devVersion = '4.1';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
		// First, get the current version of the site.
		$laravel        = app();
		$pregMatch      = preg_match('/([0-9]+\.[0-9]+)/', $laravel::VERSION, $currentVersion);
		$currentVersion = $currentVersion[0];

		// See if we are upgrading to a specific version
		$newVersion = $this->argument('version');

		// Convert helpers
		if ($newVersion == 'stable') {
			$newVersion = $this->stableVersion;
		}
		if ($newVersion == 'dev') {
			$newVersion = $this->devVersion;
		}

		// Check for newer version
		if ($newVersion < $currentVersion) {
			$this->error('Your laravel version ('. $currentVersion .') is newer than the requested version ('. $newVersion .').');
			return false;
		}

		// If we are already on the requested version, finish up now
		if (strpos($currentVersion, $newVersion) !== false) {
			$this->info('Current verion ('. $currentVersion .') matches requested version ('. $newVersion .').');
			return false;
		}

		// Otherwise, lets upgrade
		$upgrades = array_intersect($this->versions, range($currentVersion, $newVersion, .05));

		$this->info('Upgrading to '. $newVersion .' from '. $currentVersion .'.');

		foreach ($upgrades as $upgrade) {
			$this->upgradeTo($upgrade);
		}
	}

	protected function upgradeTo($upgrade)
	{
		switch ($upgrade) {
			case '4.1':
				$this->info('Performing upgrade to 4.1...');
				$this->upgradeToFourOne();
				$this->info('Completed upgrade to 4.1');
			break;
		}
	}

	protected function upgradeToFourOne()
	{
		// Update the composer.json file
		$this->info('Updating composer.json...');

		$composerFile = base_path() .'/composer.json';

		$composerLines = file($composerFile);

		foreach ($composerLines as $key => $line) {
			if (strpos($line, 'laravel/framework')) {
				$composerLines[$key] = str_replace('4.0', '4.1', $line);
			}
			if (stripos($line, 'awareness')) {
				unset($composerLines[$key]);
			}
			if (strpos($line, 'keywords')) {
				$composerLines[$key] = $line ."\t\"license\": \"MIT\",\n";
			}
		}

		File::delete($composerFile);
		File::put($composerFile, implode($composerLines));

		// Update bootstrap start file
		$this->info('Updating bootstrap/start.php...');

		$bootstrapFile = base_path() .'/bootstrap/start.php';

		$bootstrapLines = file($bootstrapFile);

		foreach ($bootstrapLines as $key => $line) {
			if (strpos($line, 'redirectIfTrailingSlash')) {
				unset($bootstrapLines[$key]);
			}
		}

		File::delete($bootstrapFile);
		File::put($bootstrapFile, implode($bootstrapLines));

		// Run composer update
		$this->info('Running composer update...');
		shell_exec('composer update');

		// Get new artisan and index files
		$this->info('Grabbing new artisan and index.php files...');
		shell_exec('wget -N https://raw.github.com/laravel/laravel/develop/artisan');
		shell_exec('wget -N --directory-prefix=public/ https://raw.github.com/laravel/laravel/develop/public/index.php');

		// Update the app config
		$this->info('Updating app/config/app.php');

		$appConfigFile = app_path() .'/config/app.php';

		$appConfigLines = file($appConfigFile);

		foreach ($appConfigLines as $key => $line) {
			if (strpos($line, 'Illuminate\Workbench\WorkbenchServiceProvider')) {
				$appConfigLines[$key] = $line ."\t\t'Illuminate\Remote\RemoteServiceProvider',\n";
			}
			if (strpos($line, 'Illuminate\Routing\Controllers\Controller')) {
				$appConfigLines[$key] = str_replace('Illuminate\Routing\Controllers\Controller', 'Illuminate\Routing\Controller', $line);
			}
			if (strpos($line, 'Illuminate\Support\Facades\Session')) {
				$appConfigLines[$key] = $line ."\t\t'SSH'                         => 'Illuminate\Support\Facades\SSH',\n";
			}
		}

		File::delete($appConfigFile);
		File::put($appConfigFile, implode($appConfigLines));

		// Update the database config
		$this->info('Updating app/config/database.php');

		$databaseConfigFile = app_path() .'/config/database.php';

		$databaseConfigLines = file($databaseConfigFile);

		foreach ($databaseConfigLines as $key => $line) {
			if (strpos($line, 'cluster')) {
				$databaseConfigLines[$key] = str_replace('true', 'false', $line);
			}
		}

		File::delete($databaseConfigFile);
		File::put($databaseConfigFile, implode($databaseConfigLines));

		// Update the queue config
		$this->info('Updating app/config/queue.php...');
		shell_exec('wget -N --directory-prefix=app/config/ https://raw.github.com/laravel/laravel/develop/app/config/queue.php');

		// Update the session config
		$this->info('Updating app/config/session.php');

		$sessionConfigFile = app_path() .'/config/session.php';

		$sessionConfigLines = file($sessionConfigFile);

		$sessionConfigLines = array_slice($sessionConfigLines, 0, -13);

		foreach ($sessionConfigLines as $key => $line) {
			if (strpos($line, '\'lifetime\'')) {
				$sessionConfigLines[$key] = $line ."\t'expire_on_close' => false,\n";
			}
			if (strpos($line, '\'domain\'')) {
				$sessionConfigLines[$key] = $line ."\t/*\n\t|--------------------------------------------------------------------------\n\t| HTTPS Only Cookies
	|--------------------------------------------------------------------------\n\t|\n\t| By setting this option to true, session cookies will only be sent back\n\t| to the server if the browser has a HTTPS connection. This will keep\n\t| the cookie from being sent to you if it can not be done securely.\n\t|\n\t*/\n\n\t'secure' => false,\n";
			}
		}

		$sessionConfigLines[] = "\n);";

		File::delete($sessionConfigFile);
		File::put($sessionConfigFile, implode($sessionConfigLines));

		// Add the new remote config
		$this->info('Adding app/config/remote.php...');
		shell_exec('wget -N --directory-prefix=app/config/ https://raw.github.com/laravel/laravel/develop/app/config/remote.php');

		// Update the reminders lang file
		$this->info('Updating app/lang/en/reminders.php...');
		shell_exec('wget -N --directory-prefix=app/lang/en/ https://raw.github.com/laravel/laravel/develop/app/lang/en/reminders.php');
	}

	/**
	 * Get the console command arguments.
	 *
	 * @return array
	 */
	protected function getArguments()
	{
		return array(
			array('version', InputArgument::OPTIONAL, 'Specify a the version you want to upgrade to.  (dev and stable are viable options)'),
		);
	}

	/**
	 * Get the console command options.
	 *
	 * @return array
	 */
	protected function getOptions()
	{
		return array(
			// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
		);
	}

}