Add FixPermissionsCommand. Closes #397

This commit is contained in:
James Brooks
2015-01-17 19:44:39 +00:00
parent dedb7b0e3f
commit 600445dbeb
3 changed files with 125 additions and 1 deletions

View File

@@ -64,7 +64,7 @@
"scripts": {
"post-install-cmd": [
"php artisan optimize",
"chmod -R 777 app/storage public",
"php artisan cachet:chmod",
"php artisan cachet:one-click-deploy"
],
"post-update-cmd": [

View File

@@ -0,0 +1,108 @@
<?php
namespace CachetHQ\Cachet\Console\Commands;
use DirectoryIterator;
use Illuminate\Console\Command;
class FixPermissionsCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'cachet:chmod';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fixes file and directory permissions. Ensures SQLite database is writeable.';
/**
* Path to the storage directory.
*
* @var string
*/
protected $storageDirectory;
/**
* Path of the database directory.
*
* @var string
*/
protected $databaseDirectory;
/**
* Path of the SQLite database file.
*
* @var string
*/
protected $databasePath;
/**
* Which database connection are we using?
*
* @var string
*/
protected $databaseDefault;
/**
* Create a new fix permissions command instance.
*
* @param string $storageDirectory
* @param string $databaseDirectory
* @param string $databasePath
* @param string $databaseDefault
*
* @return void
*/
public function __construct($storageDirectory, $databaseDirectory, $databasePath, $databaseDefault)
{
$this->storageDirectory = $storageDirectory;
$this->databaseDirectory = $databaseDirectory;
$this->databasePath = $databasePath;
$this->databaseDefault = $databaseDefault;
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$this->recursiveChmod($this->storageDirectory);
if ($this->databaseDefault === 'sqlite') {
chmod($this->databaseDirectory, 755);
chmod($this->databasePath, 755);
}
}
/**
* Recursively sets a paths file permissions.
*
* @param string $path
* @param string $mode
*
* @return void
*/
protected function recursiveChmod($path, $mode = '0755')
{
$dir = new DirectoryIterator($path);
foreach ($dir as $item) {
if (!$item->isDot()) {
chmod($item->getPathname(), $mode);
}
if ($item->isDir() && !$item->isDot()) {
$this->recursiveChmod($item->getPathname());
}
}
}
}

View File

@@ -2,6 +2,7 @@
namespace CachetHQ\Cachet\Providers;
use CachetHQ\Cachet\Console\Commands\FixPermissionsCommand;
use CachetHQ\Cachet\Console\Commands\OneClickDeployCommand;
use Illuminate\Support\ServiceProvider;
@@ -14,6 +15,7 @@ class ConsoleServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->commands('CachetHQ\Cachet\Console\Commands\FixPermissionsCommand');
$this->commands('CachetHQ\Cachet\Console\Commands\OneClickDeployCommand');
}
@@ -24,6 +26,20 @@ class ConsoleServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->singleton('CachetHQ\Cachet\Console\Commands\FixPermissionsCommand', function ($app) {
$storageDirectory = $app->path['storage'];
$databaseDirectory = $app->path['database'];
$databasePath = $app->config->get('database.connections.sqlite.database');
$databaseDefault = $app->config->get('database.default');
return new FixPermissionsCommand(
$storageDirectory,
$databaseDirectory,
$databasePath,
$databaseDefault
);
});
$this->app->singleton('CachetHQ\Cachet\Console\Commands\OneClickDeployCommand', function ($app) {
return new OneClickDeployCommand($app->environment('heroku'));
});