Skip to content

Commit

Permalink
feat: only load available commands in maintenance mode
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Apr 24, 2023
1 parent f4f6431 commit 22c6c28
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 128 deletions.
1 change: 0 additions & 1 deletion core/Command/Log/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace OC\Core\Command\Log;

use OCP\IConfig;

use Stecman\Component\Symfony\Console\BashCompletion\Completion;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\ShellPathCompletion;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
Expand Down
1 change: 0 additions & 1 deletion core/Command/Maintenance/Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace OC\Core\Command\Maintenance;

use OCP\IConfig;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down
217 changes: 111 additions & 106 deletions core/register_command.php

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions lib/composer/composer/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ public function unregister()
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
$includeFile = self::$includeFile;
$includeFile($file);
(self::$includeFile)($file);

return true;
}
Expand Down Expand Up @@ -561,10 +560,7 @@ private function findFileWithExtension($class, $ext)
return false;
}

/**
* @return void
*/
private static function initializeIncludeClosure()
private static function initializeIncludeClosure(): void
{
if (self::$includeFile !== null) {
return;
Expand All @@ -578,8 +574,8 @@ private static function initializeIncludeClosure()
* @param string $file
* @return void
*/
self::$includeFile = \Closure::bind(static function($file) {
self::$includeFile = static function($file) {
include $file;
}, null, null);
};
}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => $baseDir . '/lib/private/Config.php',
'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php',
'OC\\Console\\IUnavailableInMaintenanceMode' => $baseDir . '/lib/private/Console/IUnavailableInMaintenanceMode.php',
'OC\\Console\\TimestampFormatter' => $baseDir . '/lib/private/Console/TimestampFormatter.php',
'OC\\ContactsManager' => $baseDir . '/lib/private/ContactsManager.php',
'OC\\Contacts\\ContactsMenu\\ActionFactory' => $baseDir . '/lib/private/Contacts/ContactsMenu/ActionFactory.php',
Expand Down
6 changes: 3 additions & 3 deletions lib/composer/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public static function getLoader()
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
$requireFile = static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

require $file;
}
}, null, null);
};
foreach ($filesToLoad as $fileIdentifier => $file) {
$requireFile($fileIdentifier, $file);
($requireFile)($fileIdentifier, $file);
}

return $loader;
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php',
'OC\\Console\\IUnavailableInMaintenanceMode' => __DIR__ . '/../../..' . '/lib/private/Console/IUnavailableInMaintenanceMode.php',
'OC\\Console\\TimestampFormatter' => __DIR__ . '/../../..' . '/lib/private/Console/TimestampFormatter.php',
'OC\\ContactsManager' => __DIR__ . '/../../..' . '/lib/private/ContactsManager.php',
'OC\\Contacts\\ContactsMenu\\ActionFactory' => __DIR__ . '/../../..' . '/lib/private/Contacts/ContactsMenu/ActionFactory.php',
Expand Down
14 changes: 7 additions & 7 deletions lib/composer/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php return array(
'root' => array(
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'f5e7a1fbc40d93be93c9f4891a90fdcd3a1eedd4',
'type' => 'library',
'install_path' => __DIR__ . '/../../../',
'aliases' => array(),
'reference' => NULL,
'name' => '__root__',
'dev' => false,
),
'versions' => array(
'__root__' => array(
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'f5e7a1fbc40d93be93c9f4891a90fdcd3a1eedd4',
'type' => 'library',
'install_path' => __DIR__ . '/../../../',
'aliases' => array(),
'reference' => NULL,
'dev_requirement' => false,
),
),
Expand Down
25 changes: 23 additions & 2 deletions lib/private/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
use OC\MemoryInfo;
use OC\NeedsUpdateException;
use OC_App;
use OCP\AppFramework\QueryException;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\Console\ConsoleEvent;
use OCP\IConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use Stecman\Component\Symfony\Console\BashCompletion\Completion;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
Expand All @@ -59,6 +62,8 @@ class Application {
/** @var MemoryInfo */
private $memoryInfo;

private bool $maintenanceMode;

public function __construct(IConfig $config,
EventDispatcherInterface $dispatcher,
IRequest $request,
Expand All @@ -71,6 +76,22 @@ public function __construct(IConfig $config,
$this->request = $request;
$this->logger = $logger;
$this->memoryInfo = $memoryInfo;
$this->maintenanceMode = $this->config->getSystemValueBool('maintenance');
}

/**
* Adds a command object.
*
* If a command with the same name already exists, it will be overridden.
* If the command is not enabled it will not be added.
*
* @return Command|null The registered command if enabled or null
*/
public function add(Command $command) {
if ($this->maintenanceMode && in_array(IUnavailableInMaintenanceMode::class, class_implements($command, false))) {
return null;
}
return $this->application->add($command);
}

/**
Expand Down Expand Up @@ -231,7 +252,7 @@ private function loadCommandsFromInfoXml($commands) {
}
}

$this->application->add($c);
$this->add($c);
}
}
}
33 changes: 33 additions & 0 deletions lib/private/Console/IUnavailableInMaintenanceMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);
/**
* @copyright Daniel Kesselberg <[email protected]>
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Console;

/**
* Implement if a command is unavailable in maintenance mode.
*
* A core command is a command registered in core/register_command.php.
*
* @since 27.0.0
*/
interface IUnavailableInMaintenanceMode {
}

0 comments on commit 22c6c28

Please sign in to comment.