[FEATURE] Add public API to get ApplicationContext 18/23018/8
authorSteffen Müller <typo3@t3node.com>
Fri, 9 Aug 2013 22:09:57 +0000 (00:09 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Mon, 26 Aug 2013 13:42:05 +0000 (15:42 +0200)
Application Context is initialized in Bootstrap. We need a
public API to get the current context without using the bootstrap
itself.

In Flow, the ObjectManager provides this API, but in TYPO3 CMS we
do not have this kind of ObjectManager (except in extbase).

The API call is:
\TYPO3\CMS\Core\Utility\GeneralUtility::getContext();

Resolves: #50983
Releases: 6.2
Change-Id: Ic900a503b3e2bc184d2bfd43345e72180887ef18
Reviewed-on: https://review.typo3.org/23018
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php

index 27f8af5..ea152fa 100644 (file)
@@ -112,6 +112,7 @@ class Bootstrap {
         * Returns the context this bootstrap was started in.
         *
         * @return \TYPO3\CMS\Core\Core\ApplicationContext The context encapsulated in an object
+        * @internal This is not a public API method, do not use in own extensions. Use \TYPO3\CMS\Core\Utility\GeneralUtility::getContext() instead
         */
        public function getContext() {
                return $this->context;
@@ -141,6 +142,7 @@ class Bootstrap {
         */
        public function baseSetup($relativePathPart = '') {
                \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::run($relativePathPart);
+               Utility\GeneralUtility::presetContext($this->context);
                return $this;
        }
 
index 32fb6ea..dbb3537 100644 (file)
@@ -63,6 +63,13 @@ class GeneralUtility {
         */
        static protected $nonSingletonInstances = array();
 
+       /**
+        * The application context
+        *
+        * @var \TYPO3\CMS\Core\Core\ApplicationContext
+        */
+       static protected $context = NULL;
+
        /*************************
         *
         * GET/POST Variables
@@ -4984,5 +4991,32 @@ Connection: close
                }
                echo $obContent;
        }
+
+       /**
+        * Set the ApplicationContext
+        *
+        * This function is used by the Bootstrap to hand over the context. It must not be used anywhere else,
+        * because the context shall never be changed on runtime!
+        *
+        * @param \TYPO3\CMS\Core\Core\ApplicationContext $context
+        * @throws \RuntimeException if context is overriden
+        * @internal This is not a public API method, do not use in own extensions
+        */
+       static public function presetContext(\TYPO3\CMS\Core\Core\ApplicationContext $context) {
+               if (is_null(static::$context)) {
+                       static::$context = $context;
+               } else {
+                       throw new \RuntimeException('Trying to override context which has already been defined!', 1376084316);
+               }
+       }
+
+       /**
+        * Get the ApplicationContext
+        *
+        * @return \TYPO3\CMS\Core\Core\ApplicationContext
+        */
+       static public function getContext() {
+               return static::$context;
+       }
 }
 ?>
\ No newline at end of file