[TASK] Don't use destructor as shutdown function 77/24277/4
authorThorsten Kahler <thorsten.kahler@typo3.org>
Wed, 2 Oct 2013 14:48:29 +0000 (16:48 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Sun, 10 Nov 2013 11:40:10 +0000 (12:40 +0100)
GeneralUtility::makeInstanceService() registers service classes'
destructors as shutdown functions. This has two drawbacks:
* destructors are called twice usually
* service classes need to have destructors

The shutdown function for temp file cleanup in AbstractService is
registered on demand instead.

Change-Id: I0c41d821dc2279eeb126badf306f73d3b53b3010
Resolves: #52463
Releases: 6.2
Reviewed-on: https://review.typo3.org/24277
Reviewed-by: Sascha Egerer
Tested-by: Sascha Egerer
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/core/Classes/Service/AbstractService.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php

index 33d209e..30e579b 100644 (file)
@@ -91,6 +91,11 @@ abstract class AbstractService {
        public $tempFiles = array();
 
        /**
+        * @var array list of registered shutdown functions; should be used to prevent registering the same function multiple times
+        */
+       protected $shutdownRegistry = array();
+
+       /**
         * @var string Prefix for temporary files
         */
        protected $prefixId = '';
@@ -396,6 +401,10 @@ abstract class AbstractService {
         * @todo Define visibility
         */
        public function registerTempFile($absFile) {
+               if (!isset($this->shutdownRegistry[__METHOD__])) {
+                       register_shutdown_function(array($this, 'unlinkTempFiles'));
+                       $this->shutdownRegistry[__METHOD__] = TRUE;
+               }
                $this->tempFiles[] = $absFile;
        }
 
@@ -534,9 +543,6 @@ abstract class AbstractService {
         * @todo Define visibility
         */
        public function init() {
-               // Does not work :-(  but will not hurt
-               // use it as inspiration for a service based on this class
-               register_shutdown_function(array(&$this, '__destruct'));
                // look in makeInstanceService()
                $this->reset();
                // Check for external programs which are defined by $info['exec']
index 24c0025..206bf1e 100644 (file)
@@ -4381,8 +4381,6 @@ Connection: close
                                        if ($obj->init()) {
                                                // create persistent object
                                                $GLOBALS['T3_VAR']['makeInstanceService'][$info['className']] = $obj;
-                                               // needed to delete temp files
-                                               register_shutdown_function(array(&$obj, '__destruct'));
                                                return $obj;
                                        }
                                        $error = $obj->getLastErrorArray();