Commit 7d499d34 authored by Nicolas Widart's avatar Nicolas Widart

Squashed 'Modules/Core/' changes from 450a7ec9..fe00a0a

fe00a0a Fixing the saveTranslated method for cleaned data array
9d34552 Merge pull request #6 from nWidart-Modules/add-db-credentials-to-laravel-config
3fdbaf5 Add the database credentials to laravel config
4f3a6fd Merge pull request #5 from nWidart-Modules/new-database-setup
95ac2fd Writing the database informations
e9ead32 Merge pull request #4 from nWidart-Modules/publish-core-assets
ec6d1f9 Publishing assets in the install command
08d92cc Merge pull request #3 from nWidart-Modules/adding-install-command
ba18ca5 Adding a platform install command

git-subtree-dir: Modules/Core
git-subtree-split: fe00a0a369325f12c9db1cf471789cd660c07d2c
parent 450a7ec9
<?php namespace Modules\Core\Console;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Hash;
use Modules\User\Repositories\RoleRepository;
use Modules\User\Repositories\UserRepository;
class InstallCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'platform:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the Platform CMS';
/**
* @var UserRepository
*/
private $user;
/**
* @var RoleRepository
*/
private $role;
/**
* @var Filesystem
*/
private $finder;
/**
* Create a new command instance.
*
* @param UserRepository $user
* @param RoleRepository $role
* @param Filesystem $finder
* @return \Modules\Core\Console\InstallCommand
*/
public function __construct($user, $role, Filesystem $finder)
{
parent::__construct();
$this->user = $user;
$this->role = $role;
$this->finder = $finder;
}
/**
* Execute the actions
*
* @return mixed
*/
public function fire()
{
$this->info('Starting the installation process...');
$this->configureDatabase();
$this->runMigrations();
$this->runSeeds();
$this->createFirstUser();
$this->publishAssets();
$this->blockMessage('Success!', 'Platform ready! You can now login with your username and password at /backend');
}
/**
* Create the first user that'll have admin access
*/
private function createFirstUser()
{
$this->line('Creating an Admin user account...');
$firstname = $this->ask('Enter your first name');
$lastname = $this->ask('Enter your last name');
$email = $this->ask('Enter your email address');
$password = $this->secret('Enter a password');
$userInfo = [
'first_name' => $firstname,
'last_name' => $lastname,
'email' => $email,
'password' => Hash::make($password),
];
$this->user->createWithRoles($userInfo, ['admin']);
$this->info('Admin account created!');
}
/**
* Run the migrations
*/
private function runMigrations()
{
$this->call('migrate', ['--package' => 'cartalyst/sentinel']);
$this->info('Application migrated!');
}
/**
* Run the seeds
*/
private function runSeeds()
{
$this->call('module:seed', ['module' => 'User']);
$this->info('Application seeded!');
}
/**
* Symfony style block messages
* @param $title
* @param $message
* @param string $style
*/
protected function blockMessage($title, $message, $style = 'info')
{
$formatter = $this->getHelperSet()->get('formatter');
$errorMessages = [$title, $message];
$formattedBlock = $formatter->formatBlock($errorMessages, $style, true);
$this->line($formattedBlock);
}
/**
* Publish the CMS assets
*/
private function publishAssets()
{
$this->call('module:publish', ['module' => 'Core']);
}
/**
* Configuring the database information
*/
private function configureDatabase()
{
// Ask for credentials
$databaseName = $this->ask('Enter your database name');
$databaseUsername = $this->ask('Enter your database username');
$databasePassword = $this->secret('Enter your database password');
$this->setLaravelConfiguration($databaseName, $databaseUsername, $databasePassword);
$this->configureEnvironmentFile($databaseName, $databaseUsername, $databasePassword);
}
/**
* Writing the environment file
* @param $databaseName
* @param $databaseUsername
* @param $databasePassword
*/
private function configureEnvironmentFile($databaseName, $databaseUsername, $databasePassword)
{
$environmentFile = $this->finder->get('.env.example');
$search = [
"DB_USERNAME=homestead",
"DB_PASSWORD=homestead"
];
$replace = [
"DB_USERNAME=$databaseUsername",
"DB_PASSWORD=$databasePassword" . PHP_EOL
];
$newEnvironmentFile = str_replace($search, $replace, $environmentFile);
$newEnvironmentFile .= "DB_NAME=$databaseName";
// Write the new environment file
$this->finder->put('.env', $newEnvironmentFile);
// Delete the old environment file
$this->finder->delete('env.example');
$this->info('Environment file written');
}
/**
* Set DB credentials to laravel config
* @param $databaseName
* @param $databaseUsername
* @param $databasePassword
*/
private function setLaravelConfiguration($databaseName, $databaseUsername, $databasePassword)
{
$this->laravel['config']['database.connections.mysql.database'] = $databaseName;
$this->laravel['config']['database.connections.mysql.username'] = $databaseUsername;
$this->laravel['config']['database.connections.mysql.password'] = $databasePassword;
}
}
......@@ -13,9 +13,9 @@ class Helper
*/
public static function saveTranslated($model, $data)
{
foreach ($data as $key => $value) {
foreach ($data as $lang => $value) {
if (is_array($value)) {
foreach ($value as $lang => $input) {
foreach ($value as $key => $input) {
$model->translate($lang)->$key = $input;
}
}
......
......@@ -2,6 +2,7 @@
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Modules\Core\Console\InstallCommand;
use Modules\User\Events\RegisterSidebarMenuItemEvent;
class CoreServiceProvider extends ServiceProvider
......@@ -41,6 +42,7 @@ class CoreServiceProvider extends ServiceProvider
$this->app->booted(function ($app) {
$this->registerFilters($app['router']);
});
$this->registerCommands();
}
/**
......@@ -87,4 +89,30 @@ class CoreServiceProvider extends ServiceProvider
}
}
}
/**
* Register the console commands
*/
private function registerCommands()
{
$this->registerInstallCommand();
}
/**
* Register the installation command
*/
private function registerInstallCommand()
{
$this->app->bindShared('command.platform.install', function($app) {
return new InstallCommand(
$app['Modules\User\Repositories\UserRepository'],
$app['Modules\User\Repositories\RoleRepository'],
$app['files']
);
});
$this->commands(
'command.platform.install'
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment