Added yii\composer\Installer::postCreateProject() and modified the syntax of calling installer methods in composer.json [skip ci]

This commit is contained in:
Qiang Xue
2014-09-02 23:17:50 -04:00
parent 9f408af930
commit f417f4353c
5 changed files with 41 additions and 67 deletions

View File

@ -20,9 +20,6 @@ use Composer\Util\Filesystem;
class Installer extends LibraryInstaller
{
const EXTRA_BOOTSTRAP = 'bootstrap';
const EXTRA_WRITABLE = 'writable';
const EXTRA_EXECUTABLE = 'executable';
const EXTRA_CONFIG = 'config';
const EXTENSION_FILE = 'yiisoft/extensions.php';
@ -223,53 +220,43 @@ EOF
rmdir($yiiDir);
}
}
public static function postCreateProject($event)
{
$params = $event->getComposer()->getPackage()->getExtra();
if (isset($params[__METHOD__]) && is_array($params[__METHOD__])) {
foreach ($params[__METHOD__] as $method => $args) {
call_user_func_array([__CLASS__, $method], (array) $args);
}
}
}
/**
* Sets the correct permission for the files and directories listed in the extra section.
* @param CommandEvent $event
* @param array $paths the paths (keys) and the corresponding permission octal strings (values)
*/
public static function setPermission($event)
public static function setPermission(array $paths)
{
$options = array_merge([
self::EXTRA_WRITABLE => [],
self::EXTRA_EXECUTABLE => [],
], $event->getComposer()->getPackage()->getExtra());
foreach ((array) $options[self::EXTRA_WRITABLE] as $path) {
echo "Setting writable: $path ...";
foreach ($paths as $path => $permission) {
echo "chmod('$path', $permission)...";
if (is_dir($path) || is_file($path)) {
chmod($path, is_file($path) ? 0666 : 0777);
echo "done\n";
chmod($path, octdec($permission));
echo "done.\n";
} else {
echo "The directory or file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path;
return;
}
}
foreach ((array) $options[self::EXTRA_EXECUTABLE] as $path) {
echo "Setting executable: $path ...";
if (is_dir($path) || is_file($path)) {
chmod($path, 0755);
echo "done\n";
} else {
echo "\n\tThe directory or file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n";
return;
echo "file not found.\n";
}
}
}
/**
* Generates a cookie validation key for every app config listed in "config" in extra section.
* @param CommandEvent $event
* You can provide one or multiple parameters as the configuration files which need to have validation key inserted.
*/
public static function generateCookieValidationKey($event)
public static function generateCookieValidationKey()
{
$extra = $event->getComposer()->getPackage()->getExtra();
if (empty($extra[self::EXTRA_CONFIG])) {
return;
}
$configs = func_get_args();
$key = self::generateRandomString();
foreach ((array) $extra[self::EXTRA_CONFIG] as $config) {
foreach ($configs as $config) {
if (is_file($config)) {
$content = preg_replace('/(("|\')cookieValidationKey("|\')\s*=>\s*)(""|\'\')/', "\\1'$key'", file_get_contents($config));
file_put_contents($config, $content);
@ -277,7 +264,7 @@ EOF
}
}
public static function generateRandomString()
protected static function generateRandomString()
{
if (!extension_loaded('mcrypt')) {
throw new \Exception('The mcrypt PHP extension is required by Yii2.');