Skip to content

Commit

Permalink
Use bound Closure instead of "$self = $this" ... fn use ($self)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 10, 2024
1 parent 04ae6b2 commit 13699c3
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/ExtensionInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa

$this->rcubeVersionCheck($package);

$self = $this;
$postInstall = function() use ($self, $package) {
$config_file = $self->rcubeConfigFile();
$package_name = $self->getPackageName($package);
$package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$postInstall = function() use ($package) {
$config_file = $this->rcubeConfigFile();
$package_name = $this->getPackageName($package);
$package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$extra = $package->getExtra();

if (is_writeable($config_file) && php_sapi_name() == 'cli' && $this->confirmInstall($package_name)) {
$self->rcubeAlterConfig($package_name);
$this->rcubeAlterConfig($package_name);
}

// copy config.inc.php.dist -> config.inc.php
if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist')) {
$config_exists = false;
$alt_config_file = $self->rcubeConfigFile($package_name . '.inc.php');
$alt_config_file = $this->rcubeConfigFile($package_name . '.inc.php');

if (is_file($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php')) {
$config_exists = true;
Expand All @@ -84,15 +83,15 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa
}

if (!$config_exists && is_writeable($package_dir)) {
$self->io->write("<info>Creating package config file</info>");
$this->io->write("<info>Creating package config file</info>");
copy($package_dir . DIRECTORY_SEPARATOR . 'config.inc.php.dist', $package_dir . DIRECTORY_SEPARATOR . 'config.inc.php');
}
}

// initialize database schema
if (!empty($extra['roundcube']['sql-dir'])) {
if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) {
$self->io->write("<info>Running database initialization script for $package_name</info>");
$this->io->write("<info>Running database initialization script for $package_name</info>");

$roundcube_version = self::versionNormalize(RCMAIL_VERSION);
if (self::versionCompare($roundcube_version, '1.2.0', '>=')) {
Expand All @@ -106,7 +105,7 @@ public function install(InstalledRepositoryInterface $repo, PackageInterface $pa

// run post-install script
if (!empty($extra['roundcube']['post-install-script'])) {
$self->rcubeRunScript($extra['roundcube']['post-install-script'], $package);
$this->rcubeRunScript($extra['roundcube']['post-install-script'], $package);
}
};

Expand Down Expand Up @@ -134,29 +133,28 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini

$this->rcubeVersionCheck($target);

$self = $this;
$extra = $target->getExtra();
$fs = new Filesystem();

// backup persistent files e.g. config.inc.php
$package_name = $self->getPackageName($initial);
$package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$package_name = $this->getPackageName($initial);
$package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$temp_dir = $package_dir . '-' . sprintf('%010d%010d', mt_rand(), mt_rand());

// make a backup of existing files (for restoring persistent files)
$fs->copy($package_dir, $temp_dir);

$postUpdate = function() use ($self, $target, $extra, $fs, $temp_dir) {
$package_name = $self->getPackageName($target);
$package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$postUpdate = function() use ($target, $extra, $fs, $temp_dir) {
$package_name = $this->getPackageName($target);
$package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;

// restore persistent files
$persistent_files = !empty($extra['roundcube']['persistent-files']) ? $extra['roundcube']['persistent-files'] : ['config.inc.php'];
foreach ($persistent_files as $file) {
$path = $temp_dir . DIRECTORY_SEPARATOR . $file;
if (is_readable($path)) {
if ($fs->copy($path, $package_dir . DIRECTORY_SEPARATOR . $file)) {
$self->io->write("<info>Restored $package_name/$file</info>");
$this->io->write("<info>Restored $package_name/$file</info>");
}
else {
throw new \Exception("Restoring " . $file . " failed.");
Expand All @@ -169,7 +167,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
// update database schema
if (!empty($extra['roundcube']['sql-dir'])) {
if ($sqldir = realpath($package_dir . DIRECTORY_SEPARATOR . $extra['roundcube']['sql-dir'])) {
$self->io->write("<info>Updating database schema for $package_name</info>");
$this->io->write("<info>Updating database schema for $package_name</info>");

$roundcube_version = self::versionNormalize(RCMAIL_VERSION);
if (self::versionCompare($roundcube_version, '1.2.0', '>=')) {
Expand All @@ -183,7 +181,7 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini

// run post-update script
if (!empty($extra['roundcube']['post-update-script'])) {
$self->rcubeRunScript($extra['roundcube']['post-update-script'], $target);
$this->rcubeRunScript($extra['roundcube']['post-update-script'], $target);
}
};

Expand All @@ -209,27 +207,26 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
}
require_once INSTALL_PATH . 'program/include/iniset.php';

$self = $this;
$config = $self->composer->getConfig()->get('roundcube');
$config = $this->composer->getConfig()->get('roundcube');

$postUninstall = function() use ($self, $package, $config) {
$postUninstall = function() use ($package, $config) {
// post-uninstall: deactivate package
$package_name = $self->getPackageName($package);
$package_dir = $self->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;
$package_name = $this->getPackageName($package);
$package_dir = $this->getVendorDir() . DIRECTORY_SEPARATOR . $package_name;

$self->rcubeAlterConfig($package_name, false);
$this->rcubeAlterConfig($package_name, false);

// run post-uninstall script
$extra = $package->getExtra();
if (!empty($extra['roundcube']['post-uninstall-script'])) {
$self->rcubeRunScript($extra['roundcube']['post-uninstall-script'], $package);
$this->rcubeRunScript($extra['roundcube']['post-uninstall-script'], $package);
}

// remove package folder
if (!empty($config['uninstall-remove-folder'])) {
$fs = new Filesystem();
$fs->remove($package_dir);
$self->io->write("<info>Removed $package_name files</info>");
$this->io->write("<info>Removed $package_name files</info>");
}
};

Expand Down

0 comments on commit 13699c3

Please sign in to comment.