Don't "abuse" setters for actually "unsetting" (closing) instances. Better use
authorRoland Haeder <roland@mxchange.org>
Wed, 18 Mar 2015 00:31:09 +0000 (01:31 +0100)
committerRoland Haeder <roland@mxchange.org>
Wed, 18 Mar 2015 00:31:09 +0000 (01:31 +0100)
explicit "unsetter" methods which seems to be a cleaner way.

Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/file_directories/class_BaseAbstractFile.php
inc/classes/main/file_directories/class_BaseFileIo.php
inc/classes/main/file_directories/directory/class_FrameworkDirectoryPointer.php
inc/classes/main/file_directories/io_stream/class_FileIoStream.php
inc/classes/main/filter/payment/class_PaymentDiscoveryFilter.php
inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php
inc/classes/main/helper/web/forms/class_WebFormHelper.php

index cf0e5da..c5226fe 100644 (file)
@@ -1038,7 +1038,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
         * @param       $protocolInstance       An instance of an HandleableProtocol
         * @return      void
         */
-       public final function setProtocolInstance (HandleableProtocol $protocolInstance = NULL) {
+       public final function setProtocolInstance (HandleableProtocol $protocolInstance) {
                $this->protocolInstance = $protocolInstance;
        }
 
@@ -1323,6 +1323,22 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        }
 
        /**
+        * Unsets pointer instance which triggers a call of __destruct() if the
+        * instance is still there. This is surely not fatal on already "closed"
+        * file pointer instances.
+        *
+        * I don't want to mess around with above setter by giving it a default
+        * value NULL as setter should always explicitly only set (existing) object
+        * instances and NULL is NULL.
+        *
+        * @return      void
+        */
+       protected final function unsetPointerInstance () {
+               // Simply it to NULL
+               $this->pointerInstance = NULL;
+       }
+
+       /**
         * Setter for Indexable instance
         *
         * @param       $indexInstance  An instance of an Indexable class
index 382ea6f..3f4869e 100644 (file)
@@ -133,10 +133,13 @@ class BaseAbstractFile extends BaseFrameworkSystem {
         *
         * @return      void
         */
-       public function closeFile () {
+       private function closeFile () {
                // Debug message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
 
+               // Close down pointer instance as well by unsetting it
+               $this->unsetPointerInstance();
+
                // Remove file name
                $this->setFileName('');
 
index 9b76269..df6d032 100644 (file)
@@ -67,7 +67,7 @@ class BaseFileIo extends BaseFrameworkSystem {
         * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
         * @throws      InvalidResourceException        If there is being set
         */
-       public function closeFile () {
+       private function closeFile () {
                // Debug message
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
 
@@ -79,6 +79,9 @@ class BaseFileIo extends BaseFrameworkSystem {
                        throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
                }
 
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
+
                // Close the file pointer and reset the instance variable
                @fclose($this->getPointer());
                $this->setPointer(NULL);
index 3d82427..42380fd 100644 (file)
@@ -196,18 +196,18 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         */
        public function closeDirectory () {
                // Close the directory by unsetting it
-               $this->setDirectoryIteratorInstance(NULL);
+               $this->unsetDirectoryIteratorInstance();
                $this->setPathName('');
        }
 
        /**
         * Setter for the directory pointer
         *
-        * @param       $iteratorInstance       An instanceof a DirectoryIterator class or NULL to unset ("close") it.
+        * @param       $iteratorInstance       An instanceof a DirectoryIterator class
         * @return      void
         */
-       protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance = NULL) {
-               // Set instance (or NULL)
+       protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
+               // Set instance
                $this->iteratorInstance = $iteratorInstance;
        }
 
@@ -221,6 +221,19 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
        }
 
        /**
+        * Remove directory iterator instance (effectively closing it) by setting
+        * it to NULL. This will trigger a call on the destructor which will then
+        * "close" the iterator.
+        *
+        * @param       $iteratorInstance       An instanceof a DirectoryIterator class
+        * @return      void
+        */
+       protected final function unsetDirectoryIteratorInstance (e) {
+               // "Unset" the instance
+               $this->iteratorInstance = NULL;
+       }
+
+       /**
         * Setter for path name
         *
         * @param       $pathName       The new path name
index a747007..9d1bc1a 100644 (file)
@@ -130,7 +130,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
                } // END - while
 
                // Close the file
-               $fileInstance->closeFile();
+               unset($fileInstance);
        }
 
        /**
@@ -166,7 +166,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil
                } // END - while
 
                // Close directory handle
-               $fileInstance->closeFile();
+               unset($fileInstance);
 
                // Convert it into an array
                $inputBuffer = explode(chr(10), $inputBuffer);
index 2d3fe83..7a016ec 100644 (file)
@@ -101,7 +101,7 @@ class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
                // Try to get real discovery class
                try {
                        // Get an instance from the object factory
-                       $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName().'_payment_discovery', array($this));
+                       $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', array($this));
 
                        // Call the discovery method
                        $discoveryInstance->discover($requestInstance);
index 7b068da..3d097bd 100644 (file)
@@ -137,13 +137,13 @@ class GraphicalCodeCaptcha extends BaseCaptcha implements SolveableCaptcha {
                $templateInstance->loadCodeTemplate('captch_graphic_code');
 
                // Rename variable
-               $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName().'_captcha');
-               $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName().'_hash');
-               $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName().'_encrypt');
+               $templateInstance->renameVariable('captcha_code', $helperInstance->getFormName() . '_captcha');
+               $templateInstance->renameVariable('captcha_hash', $helperInstance->getFormName() . '_hash');
+               $templateInstance->renameVariable('encrypted_code', $helperInstance->getFormName() . '_encrypt');
 
                // Assign variables
-               $templateInstance->assignVariable($helperInstance->getFormName().'_encrypt', urlencode(base64_encode($this->encryptedString)));
-               $templateInstance->assignVariable($helperInstance->getFormName().'_hash', $this->hashedString);
+               $templateInstance->assignVariable($helperInstance->getFormName() . '_encrypt', urlencode(base64_encode($this->encryptedString)));
+               $templateInstance->assignVariable($helperInstance->getFormName() . '_hash', $this->hashedString);
 
                // Compile the template
                $templateInstance->compileTemplate();
index 2498006..f7c8fa1 100644 (file)
@@ -684,7 +684,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                }
 
                // Get a configured instance
-               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName().'_captcha', array($this, $extraInstance));
+               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName() . '_captcha', array($this, $extraInstance));
 
                // Initiate the CAPTCHA
                $captchaInstance->initiateCaptcha();
@@ -751,7 +751,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         * @return      $isSecured      Whether this form is secured by a CAPTCHA
         */
        public function ifFormSecuredWithCaptcha () {
-               $isSecured = ($this->getConfigInstance()->getConfigEntry($this->getFormName().'_captcha_secured') == 'Y');
+               $isSecured = ($this->getConfigInstance()->getConfigEntry($this->getFormName() . '_captcha_secured') == 'Y');
                return $isSecured;
        }