Skip to content

Commit

Permalink
Added workaround for phalcon/cphalcon#14346
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Sep 2, 2019
1 parent cc40e41 commit fbdb344
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
namespace Vokuro;

use Phalcon\Di;
use Phalcon\Di\DiInterface;

/**
* Call Dependency Injection container
*
* @return mixed
* @return mixed|null|DiInterface
*/
function container()
{
Expand Down
44 changes: 44 additions & 0 deletions app/Phalcon/Beta2FixSecurity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Phalcon;

use function Vokuro\container;

/**
* Extended class for fixing phalcon/cphalcon#14346 issue
*
* @see https://github.com/phalcon/cphalcon/pull/14347
*/
class Beta2FixSecurity extends Security
{
/**
* @inheritDoc
*/
public function getRequestToken(): string
{
if (empty($this->requestToken)) {
return $this->getSessionToken();
}

return (string) $this->requestToken;
}

/**
* @inheritDoc
*
* @return string
* @throws Exception
*/
public function getSessionToken(): string
{
if (!container()->has('session')) {
throw new Exception(
Exception::containerServiceNotFound("the 'session' service")
);
}

$session = container()->getShared('session');
return (string) $session->get($this->tokenValueSessionId);
}
}
31 changes: 24 additions & 7 deletions app/Providers/SecurityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

namespace Vokuro\Providers;

use Phalcon\Beta2FixSecurity;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Security;
use Phalcon\Version;

class SecurityProvider implements ServiceProviderInterface
{
Expand All @@ -29,15 +31,30 @@ class SecurityProvider implements ServiceProviderInterface
*/
public function register(DiInterface $di): void
{
$di->set($this->providerName, function () use ($di) {
$that = $this;
$di->set($this->providerName, function () use ($di, $that) {
return $that->getSecurity($di);
});
}

/**
* Remove current method after after next release of Phalcon 4
*
* @see https://github.com/phalcon/cphalcon/issues/14346
*
* @param DiInterface $di
* @return Security
*/
protected function getSecurity(DiInterface $di): Security
{
if (Version::get() !== '4.0.0-beta.2') {
$security = new Security();
$security->setDI($di);
} else {
$security = new Beta2FixSecurity();
}

// Initialize session.
$security->getTokenKey();
$security->getToken();
$security->setDI($di);

return $security;
});
return $security;
}
}

0 comments on commit fbdb344

Please sign in to comment.