Initial commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
|
||||
|
||||
/*
|
||||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.md file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
|
||||
|
||||
/**
|
||||
* Base class for "Render Once"-style ViewHelpers: session, cookie,
|
||||
* request, template variable set, ViewHelper variable set etc.
|
||||
*
|
||||
* @author Claus Due <claus@namelesscoder.net>
|
||||
* @package Vhs
|
||||
* @subpackage ViewHelpers\Once
|
||||
*/
|
||||
abstract class AbstractOnceViewHelper extends AbstractConditionViewHelper {
|
||||
|
||||
/**
|
||||
* Standard storage - static variable meaning uniqueness of $identifier
|
||||
* across each Request, i.e. unique to each individual plugin/content.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $identifiers = array();
|
||||
|
||||
/**
|
||||
* Initialize arguments
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initializeArguments() {
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('identifier', 'string', 'Identity of this condition - if used in other places, the condition applies to the same identity in the storage (i.e. cookie name or session key)');
|
||||
$this->registerArgument('lockToDomain', 'boolean', 'If TRUE, locks this condition to a specific domain, i.e. the storage of $identity is associated with a domain. If same identity is also used without domain lock, it matches any domain locked condition', FALSE, FALSE);
|
||||
$this->registerArgument('ttl', 'integer', 'Time-to-live for skip registration, number of seconds. After this expires the registration is unset', FALSE, 86400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard render method. Implementers should override
|
||||
* the assertShouldSkip() method and/or the getIdentifier()
|
||||
* and storeIdentifier() methods as applies to each
|
||||
* implementers method of storing identifiers.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render() {
|
||||
$this->removeIfExpired();
|
||||
$evaluation = $this->assertShouldSkip();
|
||||
if (FALSE === $evaluation) {
|
||||
$content = $this->renderThenChild();
|
||||
} else {
|
||||
$content = $this->renderElseChild();
|
||||
}
|
||||
$this->storeIdentifier();
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getIdentifier() {
|
||||
if (TRUE === isset($this->arguments['identifier'])) {
|
||||
return $this->arguments['identifier'];
|
||||
}
|
||||
return get_class($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @retrun void
|
||||
*/
|
||||
protected function storeIdentifier() {
|
||||
$identifier = $this->getIdentifier();
|
||||
if (FALSE === isset(self::$identifiers[$identifier])) {
|
||||
self::$identifiers[$identifier] = time();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function removeIfExpired() {
|
||||
$identifier = $this->getIdentifier();
|
||||
if (TRUE === isset(self::$identifiers[$identifier]) && self::$identifiers[$identifier] <= time() - $this->arguments['ttl']) {
|
||||
unset(self::$identifiers[$identifier]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
protected function assertShouldSkip() {
|
||||
$identifier = $this->getIdentifier();
|
||||
return (TRUE === isset(self::$identifiers[$identifier]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Override: forcibly disables page caching - a TRUE condition
|
||||
* in this ViewHelper means page content would be depending on
|
||||
* the current visitor's session/cookie/auth etc.
|
||||
*
|
||||
* Returns value of "then" attribute.
|
||||
* If then attribute is not set, iterates through child nodes and renders ThenViewHelper.
|
||||
* If then attribute is not set and no ThenViewHelper and no ElseViewHelper is found, all child nodes are rendered
|
||||
*
|
||||
* @return string rendered ThenViewHelper or contents of <f:if> if no ThenViewHelper was found
|
||||
* @api
|
||||
*/
|
||||
protected function renderThenChild() {
|
||||
$GLOBALS['TSFE']->no_cache = 1;
|
||||
return parent::renderThenChild();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
|
||||
|
||||
/*
|
||||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.md file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Once: Cookie
|
||||
*
|
||||
* Displays nested content or "then" child once, then sets a
|
||||
* cookie with $ttl, optionally locked to domain name, which
|
||||
* makes the condition return FALSE as long as the cookie exists.
|
||||
*
|
||||
* "Once"-style ViewHelpers are purposed to only display their
|
||||
* nested content once per XYZ, where the XYZ depends on the
|
||||
* specific type of ViewHelper (session, cookie etc).
|
||||
*
|
||||
* In addition the ViewHelper is a ConditionViewHelper, which
|
||||
* means you can utilize the f:then and f:else child nodes as
|
||||
* well as the "then" and "else" arguments.
|
||||
*
|
||||
* @author Claus Due <claus@namelesscoder.net>
|
||||
* @package Vhs
|
||||
* @subpackage ViewHelpers\Once
|
||||
*/
|
||||
class CookieViewHelper extends AbstractOnceViewHelper {
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function storeIdentifier() {
|
||||
$identifier = $this->getIdentifier();
|
||||
$domain = TRUE === isset($this->arguments['lockToDomain']) && TRUE === $this->arguments['lockToDomain'] ? $_SERVER['HTTP_HOST'] : NULL;
|
||||
setcookie($identifier, '1', time() + $this->arguments['ttl'], NULL, $domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
protected function assertShouldSkip() {
|
||||
$identifier = $this->getIdentifier();
|
||||
return (TRUE === isset($_COOKIE[$identifier]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function removeIfExpired() {
|
||||
$identifier = $this->getIdentifier();
|
||||
$existsInCookie = (boolean) (TRUE === isset($_COOKIE[$identifier]));
|
||||
if (TRUE === $existsInCookie) {
|
||||
$this->removeCookie();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function removeCookie() {
|
||||
$identifier = $this->getIdentifier();
|
||||
unset($_SESSION[$identifier], $_COOKIE[$identifier]);
|
||||
setcookie($identifier, NULL, time() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
|
||||
|
||||
/*
|
||||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.md file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Once: Instance
|
||||
*
|
||||
* Displays nested content or "then" child once per instance
|
||||
* of the content element or plugin being rendered, as identified
|
||||
* by the contentObject UID (or globally if no contentObject
|
||||
* is associated).
|
||||
*
|
||||
* "Once"-style ViewHelpers are purposed to only display their
|
||||
* nested content once per XYZ, where the XYZ depends on the
|
||||
* specific type of ViewHelper (session, cookie etc).
|
||||
*
|
||||
* In addition the ViewHelper is a ConditionViewHelper, which
|
||||
* means you can utilize the f:then and f:else child nodes as
|
||||
* well as the "then" and "else" arguments.
|
||||
*
|
||||
* @author Claus Due <claus@namelesscoder.net>
|
||||
* @package Vhs
|
||||
* @subpackage ViewHelpers\Once
|
||||
*/
|
||||
class InstanceViewHelper extends AbstractOnceViewHelper {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getIdentifier() {
|
||||
if (TRUE === isset($this->arguments['identifier']) && NULL !== $this->arguments['identifier']) {
|
||||
return $this->arguments['identifier'];
|
||||
}
|
||||
$request = $this->controllerContext->getRequest();
|
||||
$identifier = implode('_', array(
|
||||
$request->getControllerActionName(),
|
||||
$request->getControllerName(),
|
||||
$request->getPluginName(),
|
||||
$request->getControllerExtensionName()
|
||||
));
|
||||
return $identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function storeIdentifier() {
|
||||
$index = get_class($this);
|
||||
$identifier = $this->getIdentifier();
|
||||
if (FALSE === is_array($GLOBALS[$index])) {
|
||||
$GLOBALS[$index] = array();
|
||||
}
|
||||
$GLOBALS[$index][$identifier] = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
protected function assertShouldSkip() {
|
||||
$index = get_class($this);
|
||||
$identifier = $this->getIdentifier();
|
||||
return isset($GLOBALS[$index][$identifier]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
|
||||
|
||||
/*
|
||||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.md file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Once: Session
|
||||
*
|
||||
* Displays nested content or "then" child once per session.
|
||||
*
|
||||
* "Once"-style ViewHelpers are purposed to only display their
|
||||
* nested content once per XYZ, where the XYZ depends on the
|
||||
* specific type of ViewHelper (session, cookie etc).
|
||||
*
|
||||
* In addition the ViewHelper is a ConditionViewHelper, which
|
||||
* means you can utilize the f:then and f:else child nodes as
|
||||
* well as the "then" and "else" arguments.
|
||||
*
|
||||
* @author Claus Due <claus@namelesscoder.net>
|
||||
* @package Vhs
|
||||
* @subpackage ViewHelpers\Once
|
||||
*/
|
||||
class SessionViewHelper extends AbstractOnceViewHelper {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function render() {
|
||||
if ('' === session_id()) {
|
||||
session_start();
|
||||
}
|
||||
return parent::render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function storeIdentifier() {
|
||||
$identifier = $this->getIdentifier();
|
||||
$index = get_class($this);
|
||||
if (FALSE === is_array($_SESSION[$index])) {
|
||||
$_SESSION[$index] = array();
|
||||
}
|
||||
$_SESSION[$index][$identifier] = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
protected function assertShouldSkip() {
|
||||
$identifier = $this->getIdentifier();
|
||||
$index = get_class($this);
|
||||
return (boolean) (TRUE === isset($_SESSION[$index][$identifier]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function removeIfExpired() {
|
||||
$identifier = $this->getIdentifier();
|
||||
$index = get_class($this);
|
||||
$existsInSession = (boolean) (TRUE === isset($_SESSION[$index]) && TRUE === isset($_SESSION[$index][$identifier]));
|
||||
if (TRUE === $existsInSession && time() - $this->arguments['ttl'] >= $_SESSION[$index][$identifier]) {
|
||||
unset($_SESSION[$index][$identifier]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace FluidTYPO3\Vhs\ViewHelpers\Once;
|
||||
|
||||
/*
|
||||
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.md file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Once: Standard
|
||||
*
|
||||
* Displays nested content or "then" child once per rendering
|
||||
* stack - i.e. per Layout, or Template if no Layout is used.
|
||||
*
|
||||
* "Once"-style ViewHelpers are purposed to only display their
|
||||
* nested content once per XYZ, where the XYZ depends on the
|
||||
* specific type of ViewHelper (session, cookie etc).
|
||||
*
|
||||
* In addition the ViewHelper is a ConditionViewHelper, which
|
||||
* means you can utilize the f:then and f:else child nodes as
|
||||
* well as the "then" and "else" arguments.
|
||||
*
|
||||
* @author Claus Due <claus@namelesscoder.net>
|
||||
* @package Vhs
|
||||
* @subpackage ViewHelpers\Once
|
||||
*/
|
||||
class StandardViewHelper extends AbstractOnceViewHelper {
|
||||
}
|
||||
Reference in New Issue
Block a user