]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add a public replacement for OC::$server->get 31900/head
authorCarl Schwan <carl@carlschwan.eu>
Fri, 8 Apr 2022 12:16:21 +0000 (14:16 +0200)
committerCarl Schwan <carl@carlschwan.eu>
Tue, 10 May 2022 16:51:12 +0000 (18:51 +0200)
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
apps/files/lib/App.php
apps/files/lib/Collaboration/Resources/Listener.php
apps/files/list.php
apps/files_external/lib/Lib/Storage/AmazonS3.php
build/psalm-baseline-ocp.xml
build/psalm-baseline.xml
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/public/AppFramework/IAppContainer.php
lib/public/IServerContainer.php
lib/public/Server.php [new file with mode: 0644]

index 386e5a3243a1bae7ec61ab6a43da04bd2d600c55..e172f0ae8266a955caf5b4e414e31ae8583233c0 100644 (file)
@@ -6,6 +6,7 @@
  * @author Joas Schilling <coding@schilljs.com>
  * @author Morris Jobke <hey@morrisjobke.de>
  * @author Vincent Petry <vincent@nextcloud.com>
+ * @author Carl Schwan <carl@carlschwan.eu>
  *
  * @license AGPL-3.0
  *
  */
 namespace OCA\Files;
 
+use OC\NavigationManager;
+use OCP\App\IAppManager;
+use OCP\IConfig;
+use OCP\IGroupManager;
+use OCP\INavigationManager;
+use OCP\IURLGenerator;
+use OCP\IUserSession;
+use OCP\L10N\IFactory;
+use OCP\Server;
+
 class App {
-       /**
-        * @var \OCP\INavigationManager
-        */
-       private static $navigationManager;
+       private static ?INavigationManager $navigationManager = null;
 
        /**
         * Returns the app's navigation manager
-        *
-        * @return \OCP\INavigationManager
         */
-       public static function getNavigationManager() {
+       public static function getNavigationManager(): INavigationManager {
                // TODO: move this into a service in the Application class
                if (self::$navigationManager === null) {
-                       self::$navigationManager = new \OC\NavigationManager(
-                               \OC::$server->getAppManager(),
-                               \OC::$server->getURLGenerator(),
-                               \OC::$server->getL10NFactory(),
-                               \OC::$server->getUserSession(),
-                               \OC::$server->getGroupManager(),
-                               \OC::$server->getConfig()
+                       self::$navigationManager = new NavigationManager(
+                               Server::get(IAppManager::class),
+                               Server::get(IUrlGenerator::class),
+                               Server::get(IFactory::class),
+                               Server::get(IUserSession::class),
+                               Server::get(IGroupManager::class),
+                               Server::get(IConfig::class)
                        );
                        self::$navigationManager->clear(false);
                }
                return self::$navigationManager;
        }
 
-       public static function extendJsConfig($settings) {
+       public static function extendJsConfig($settings): void {
                $appConfig = json_decode($settings['array']['oc_appconfig'], true);
 
-               $maxChunkSize = (int)\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 10 * 1024 * 1024);
+               $maxChunkSize = (int)Server::get(IConfig::class)->getAppValue('files', 'max_chunk_size', (string)(10 * 1024 * 1024));
                $appConfig['files'] = [
                        'max_chunk_size' => $maxChunkSize
                ];
index 5cf843165783a2f0be3b4014602d694dcbbfe27c..64dd693a4da4e4b80c2cf43d959b1e85c0ba8623 100644 (file)
@@ -25,6 +25,7 @@ declare(strict_types=1);
  */
 namespace OCA\Files\Collaboration\Resources;
 
+use OCP\Server;
 use OCP\Collaboration\Resources\IManager;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
@@ -37,9 +38,9 @@ class Listener {
 
        public static function shareModification(): void {
                /** @var IManager $resourceManager */
-               $resourceManager = \OC::$server->query(IManager::class);
+               $resourceManager = Server::get(IManager::class);
                /** @var ResourceProvider $resourceProvider */
-               $resourceProvider = \OC::$server->query(ResourceProvider::class);
+               $resourceProvider = Server::get(ResourceProvider::class);
 
                $resourceManager->invalidateAccessCacheForProvider($resourceProvider);
        }
index 09dc217139c6a56c48769b8725012a6858ab52f8..c4b93b2e145dc83bfc932aeae1b5101b84dc56a1 100644 (file)
  *
  */
 use OCP\Share\IManager;
+use OCP\Server;
+use OCP\IConfig;
+use OCP\IUserSession;
 
-$config = \OC::$server->getConfig();
-$userSession = \OC::$server->getUserSession();
+$config = Server::get(IConfig::class);
+$userSession = Server::get(IUserSession::class);
 // TODO: move this to the generated config.js
 /** @var IManager $shareManager */
-$shareManager = \OC::$server->get(IManager::class);
+$shareManager = Server::get(IManager::class);
 $publicUploadEnabled = $shareManager->shareApiLinkAllowPublicUpload() ? 'yes' : 'no';
 
 $showgridview = $config->getUserValue($userSession->getUser()->getUID(), 'files', 'show_grid', false);
index cfd78689fa4dc682bb38836654703347715283d8..d3e9605e5a1fb77189372fe6a2bd3d6e9a4b6f0d 100644 (file)
@@ -53,6 +53,7 @@ use OCP\Files\FileInfo;
 use OCP\Files\IMimeTypeDetector;
 use OCP\ICacheFactory;
 use OCP\IMemcache;
+use OCP\Server;
 
 class AmazonS3 extends \OC\Files\Storage\Common {
        use S3ConnectionTrait;
@@ -87,9 +88,9 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                $this->objectCache = new CappedMemoryCache();
                $this->directoryCache = new CappedMemoryCache();
                $this->filesCache = new CappedMemoryCache();
-               $this->mimeDetector = \OC::$server->get(IMimeTypeDetector::class);
+               $this->mimeDetector = Server::get(IMimeTypeDetector::class);
                /** @var ICacheFactory $cacheFactory */
-               $cacheFactory = \OC::$server->get(ICacheFactory::class);
+               $cacheFactory = Server::get(ICacheFactory::class);
                $this->memCache = $cacheFactory->createLocal('s3-external');
        }
 
index 87a994ea720f8f1638d81ef0cc6ceadebf6e1afd..5490f5058862acde32efb75f5e1f796e94a03095 100644 (file)
       <code>$this-&gt;request-&gt;server</code>
     </NoInterfaceProperties>
   </file>
+  <file src="lib/public/Server.php">
+    <InvalidThrow occurrences="2">
+      <code>ContainerExceptionInterface</code>
+    </InvalidThrow>
+  </file>
   <file src="lib/public/AppFramework/App.php">
     <InternalMethod occurrences="1">
       <code>new RouteConfig($this-&gt;container, $router, $routes)</code>
index 4f4e18e1f90b2a10aab6e9be323e5683d45ba65a..7152a3fbffef14c0879c140353a8d84a0de335b4 100644 (file)
       <code>$this-&gt;fileIsEncrypted</code>
     </TypeDoesNotContainType>
   </file>
-  <file src="apps/files/lib/App.php">
-    <InvalidScalarArgument occurrences="1">
-      <code>10 * 1024 * 1024</code>
-    </InvalidScalarArgument>
-  </file>
   <file src="apps/files/lib/Command/Scan.php">
     <NullArgument occurrences="1">
       <code>null</code>
       <code>is_int($expected)</code>
     </TypeDoesNotContainType>
   </file>
+  <file src="lib/public/Server.php">
+    <InvalidThrow occurrences="2">
+      <code>ContainerExceptionInterface</code>
+    </InvalidThrow>
+  </file>
   <file src="lib/public/AppFramework/ApiController.php">
     <NoInterfaceProperties occurrences="1">
       <code>$this-&gt;request-&gt;server</code>
index 997c5d5a84415d095c024b61f2bbc91ab89eb356..32a4c749a0837444d21878e3594b246c1a2d6e49 100644 (file)
@@ -515,6 +515,7 @@ return array(
     'OCP\\Security\\ITrustedDomainHelper' => $baseDir . '/lib/public/Security/ITrustedDomainHelper.php',
     'OCP\\Security\\VerificationToken\\IVerificationToken' => $baseDir . '/lib/public/Security/VerificationToken/IVerificationToken.php',
     'OCP\\Security\\VerificationToken\\InvalidTokenException' => $baseDir . '/lib/public/Security/VerificationToken/InvalidTokenException.php',
+    'OCP\\Server' => $baseDir . '/lib/public/Server.php',
     'OCP\\Session\\Exceptions\\SessionNotAvailableException' => $baseDir . '/lib/public/Session/Exceptions/SessionNotAvailableException.php',
     'OCP\\Settings\\IDelegatedSettings' => $baseDir . '/lib/public/Settings/IDelegatedSettings.php',
     'OCP\\Settings\\IIconSection' => $baseDir . '/lib/public/Settings/IIconSection.php',
index c6cfdaba6ec3ba89d6ca8f756197fb34404d1ecd..8f2b1cb648a3eea8102f987c123822a4ff762096 100644 (file)
@@ -544,6 +544,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\Security\\ITrustedDomainHelper' => __DIR__ . '/../../..' . '/lib/public/Security/ITrustedDomainHelper.php',
         'OCP\\Security\\VerificationToken\\IVerificationToken' => __DIR__ . '/../../..' . '/lib/public/Security/VerificationToken/IVerificationToken.php',
         'OCP\\Security\\VerificationToken\\InvalidTokenException' => __DIR__ . '/../../..' . '/lib/public/Security/VerificationToken/InvalidTokenException.php',
+        'OCP\\Server' => __DIR__ . '/../../..' . '/lib/public/Server.php',
         'OCP\\Session\\Exceptions\\SessionNotAvailableException' => __DIR__ . '/../../..' . '/lib/public/Session/Exceptions/SessionNotAvailableException.php',
         'OCP\\Settings\\IDelegatedSettings' => __DIR__ . '/../../..' . '/lib/public/Settings/IDelegatedSettings.php',
         'OCP\\Settings\\IIconSection' => __DIR__ . '/../../..' . '/lib/public/Settings/IIconSection.php',
index 626481de0e0920febcaaad2a95dc30fb2d1e6471..a3b821440330981227710358431069c44155f7fb 100644 (file)
@@ -38,6 +38,7 @@ use Psr\Container\ContainerInterface;
  * thus this interface won't extend it anymore once that was removed. So migrate to the ContainerInterface
  * only.
  *
+ * @deprecated 20.0.0
  * @since 6.0.0
  */
 interface IAppContainer extends ContainerInterface, IContainer {
index 024abbe2a976678fe32e2e0e9489124a166e4483..6c07a5218ba73b511cab017f1464c1ef93cc8c41 100644 (file)
@@ -55,6 +55,8 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  * thus this interface won't extend it anymore once that was removed. So migrate to the ContainerInterface
  * only.
  *
+ * @deprecated 20.0.0
+ *
  * @since 6.0.0
  */
 interface IServerContainer extends ContainerInterface, IContainer {
diff --git a/lib/public/Server.php b/lib/public/Server.php
new file mode 100644 (file)
index 0000000..be6b6a4
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Carl Schwan <carl@carlschwan.eu>
+ *
+ * @license AGPL-3.0-or-later
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCP;
+
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\NotFoundExceptionInterface;
+
+/**
+ * Class allowing to inject services into your application. You should
+ * use whenever possible dependency injections instead.
+ *
+ * ```php
+ * use OCP\Server;
+ *
+ * $tagManager = Server::get(ITagManager::class);
+ * ```
+ *
+ * @since 25.0.0
+ */
+final class Server {
+       /**
+        * @template T
+        * @param class-string<T>|string $serviceName
+        * @return T|mixed
+        * @throws ContainerExceptionInterface
+        * @throws NotFoundExceptionInterface
+        * @since 25.0.0
+        */
+       public static function get(string $serviceName) {
+               /** @psalm-suppress UndefinedClass */
+               return \OC::$server->get($serviceName);
+       }
+}