From: Roland Haeder Date: Wed, 18 Mar 2015 00:31:09 +0000 (+0100) Subject: Don't "abuse" setters for actually "unsetting" (closing) instances. Better use X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d7c285e7acc5b68b4fdffe76011d8c4c28aef09d;p=core.git Don't "abuse" setters for actually "unsetting" (closing) instances. Better use explicit "unsetter" methods which seems to be a cleaner way. Signed-off-by: Roland Häder --- diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index cf0e5da7..c5226fed 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -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; } @@ -1322,6 +1322,22 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { return $this->pointerInstance; } + /** + * 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 * diff --git a/inc/classes/main/file_directories/class_BaseAbstractFile.php b/inc/classes/main/file_directories/class_BaseAbstractFile.php index 382ea6fa..3f4869e4 100644 --- a/inc/classes/main/file_directories/class_BaseAbstractFile.php +++ b/inc/classes/main/file_directories/class_BaseAbstractFile.php @@ -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(''); diff --git a/inc/classes/main/file_directories/class_BaseFileIo.php b/inc/classes/main/file_directories/class_BaseFileIo.php index 9b762693..df6d0321 100644 --- a/inc/classes/main/file_directories/class_BaseFileIo.php +++ b/inc/classes/main/file_directories/class_BaseFileIo.php @@ -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); diff --git a/inc/classes/main/file_directories/directory/class_FrameworkDirectoryPointer.php b/inc/classes/main/file_directories/directory/class_FrameworkDirectoryPointer.php index 3d824272..42380fd1 100644 --- a/inc/classes/main/file_directories/directory/class_FrameworkDirectoryPointer.php +++ b/inc/classes/main/file_directories/directory/class_FrameworkDirectoryPointer.php @@ -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; } @@ -220,6 +220,19 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework return $this->iteratorInstance; } + /** + * 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 * diff --git a/inc/classes/main/file_directories/io_stream/class_FileIoStream.php b/inc/classes/main/file_directories/io_stream/class_FileIoStream.php index a7470073..9d1bc1a1 100644 --- a/inc/classes/main/file_directories/io_stream/class_FileIoStream.php +++ b/inc/classes/main/file_directories/io_stream/class_FileIoStream.php @@ -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); diff --git a/inc/classes/main/filter/payment/class_PaymentDiscoveryFilter.php b/inc/classes/main/filter/payment/class_PaymentDiscoveryFilter.php index 2d3fe83b..7a016ece 100644 --- a/inc/classes/main/filter/payment/class_PaymentDiscoveryFilter.php +++ b/inc/classes/main/filter/payment/class_PaymentDiscoveryFilter.php @@ -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); diff --git a/inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php b/inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php index 7b068da7..3d097bdb 100644 --- a/inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php +++ b/inc/classes/main/helper/captcha/web/class_GraphicalCodeCaptcha.php @@ -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(); diff --git a/inc/classes/main/helper/web/forms/class_WebFormHelper.php b/inc/classes/main/helper/web/forms/class_WebFormHelper.php index 2498006a..f7c8fa1d 100644 --- a/inc/classes/main/helper/web/forms/class_WebFormHelper.php +++ b/inc/classes/main/helper/web/forms/class_WebFormHelper.php @@ -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; }