Skip to content

Commit

Permalink
Add an install script.
Browse files Browse the repository at this point in the history
This install script takes care of copying the default config file into
its proper place, and changing permissions on the tmp dir if required.
This will help ease use of the app skeleton. It also gives a place for
application developers to provide their own install script if they want.
  • Loading branch information
markstory committed Sep 6, 2013
1 parent cf11244 commit 2b2ee2f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
83 changes: 83 additions & 0 deletions App/App/Console/Installer.php
@@ -0,0 +1,83 @@
<?php
/**
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Console;

use Composer\Script\Event;

/**
* Provides installation hooks for when this application is installed via
* composer. Customize this class to suit your needs.
*/
class Installer {

/**
* Does some routine installation tasks so people don't have to.
*
* @param Composer\Script\Event $event
*/
public static function postInstall(Event $event) {
$io = $event->getIO();

$rootDir = dirname(dirname(__DIR__));
static::createAppConfig($rootDir, $io);
static::setTmpPermissions($rootDir, $io);

}

/**
* Create the Config/app.php file if it does not exist.
*
* @param string $dir The application's root directory.
* @param Composer\IO\IOInterface IO interface to write to console.
* @return void
*/
public static function createAppConfig($dir, $io) {
$appConfig = $dir . '/App/Config/app.php';
$defaultConfig = $dir . '/App/Config/app.php.default';
if (!file_exists($appConfig)) {
copy($defaultConfig, $appConfig);
$io->write('Created `Config/app.php` file');
}
}

/**
* Set globally writable permissions on the tmp directory.
*
* This is not the most secure default, but it gets people up and running quickly.
*
* @param string $dir The application's root directory.
* @param Composer\IO\IOInterface IO interface to write to console.
* @return void
*/
public static function setTmpPermissions($dir, $io) {
$worldWritable = bindec('0110000000');

// Get current permissions in decimal format so we can bitmask it.
$currentPerms = octdec(substr(sprintf('%o', fileperms($dir . '/tmp')), -4));

if (($currentPerms & $worldWritable) != $worldWritable) {
$io->write('Attempting to set permissions on tmp/');
$result = chmod('./tmp', $currentPerms | $worldWritable);
if ($result) {
$io->write('Permissions set on tmp/');
} else {
$io->write('Failed to set permissions on tmp/ you must do it yourself.');
}
}
}

}
7 changes: 6 additions & 1 deletion App/composer.json
@@ -1,7 +1,8 @@
{
"name": "cakephp/app",
"name": "cakephp/cakephp-app",
"description": "CakePHP skeleton app",
"homepage": "http://cakephp.org",
"type": "project",
"license": "MIT",
"require": {
"cakephp/cakephp": "dev-3.0",
Expand All @@ -12,7 +13,11 @@
},
"autoload": {
"psr-0": {
"": ".",
"App\\": "."
}
},
"scripts": {
"post-install-cmd": "App\\Console\\Installer::postInstall"
}
}

0 comments on commit 2b2ee2f

Please sign in to comment.