-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
938 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Ivory/HttpAdapter/Event/Cookie/AbstractPersistentCookieJar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\Cookie; | ||
|
||
/** | ||
* Abstract persistent cookie jar. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
abstract class AbstractPersistentCookieJar extends CookieJar implements PersistentCookieJarInterface | ||
{ | ||
/** | ||
* Creates a persistent cookie jar. | ||
* | ||
* @param \Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface|null $cookieFactory The cookie factory. | ||
* @param boolean $load TRUE if it should load the cookies else FALSE. | ||
*/ | ||
public function __construct(CookieFactoryInterface $cookieFactory = null, $load = true) | ||
{ | ||
parent::__construct($cookieFactory); | ||
|
||
if ($load) { | ||
$this->load(); | ||
} | ||
} | ||
|
||
/** | ||
* Destructs the persistent cookie jar. | ||
*/ | ||
public function __destruct() | ||
{ | ||
$this->save(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function serialize() | ||
{ | ||
return json_encode(array_map(function (CookieInterface $cookie) { | ||
return $cookie->toArray(); | ||
}, $this->getCookies())); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unserialize($serialized) | ||
{ | ||
$data = json_decode($serialized, true); | ||
|
||
if (empty($data)) { | ||
$this->cookies = array(); | ||
} else { | ||
$cookieFactory = $this->cookieFactory; | ||
|
||
$this->cookies = array_map(function (array $cookie) use ($cookieFactory) { | ||
return $cookieFactory->create( | ||
$cookie['name'], | ||
$cookie['value'], | ||
$cookie['attributes'], | ||
$cookie['created_at'] | ||
); | ||
}, $data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\Cookie; | ||
|
||
use Ivory\HttpAdapter\HttpAdapterException; | ||
|
||
/** | ||
* File cookie jar. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class FileCookieJar extends AbstractPersistentCookieJar | ||
{ | ||
/** @var string */ | ||
protected $file; | ||
|
||
/** | ||
* Creates a file cookie jar. | ||
* | ||
* @param string $file The file. | ||
* @param \Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface|null $cookieFactory The cookie factory. | ||
*/ | ||
public function __construct($file, CookieFactoryInterface $cookieFactory = null) | ||
{ | ||
$this->setFile($file); | ||
|
||
parent::__construct($cookieFactory, file_exists($file)); | ||
} | ||
|
||
/** | ||
* Gets the file. | ||
* | ||
* @return string The file. | ||
*/ | ||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
/** | ||
* Sets the file. | ||
* | ||
* @param string $file The file. | ||
*/ | ||
public function setFile($file) | ||
{ | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load() | ||
{ | ||
if (($data = @file_get_contents($this->file)) === false) { | ||
$error = error_get_last(); | ||
throw HttpAdapterException::cannotLoadCookieJar($error['message']); | ||
} | ||
|
||
$this->unserialize($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save() | ||
{ | ||
if (@file_put_contents($this->file, $this->serialize()) === false) { | ||
$error = error_get_last(); | ||
throw HttpAdapterException::cannotSaveCookieJar($error['message']); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Ivory/HttpAdapter/Event/Cookie/PersistentCookieJarInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\Cookie; | ||
|
||
/** | ||
* Persistent cookie jar. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
interface PersistentCookieJarInterface extends CookieJarInterface, \Serializable | ||
{ | ||
/** | ||
* Loads the cookie jar. | ||
* | ||
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occured. | ||
* | ||
* @return void No return value. | ||
*/ | ||
public function load(); | ||
|
||
/** | ||
* Saves the cookie jar. | ||
* | ||
* @throws \Ivory\HttpAdapter\HttpAdapterException If an error occured. | ||
* | ||
* @return void No return value. | ||
*/ | ||
public function save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\Cookie; | ||
|
||
/** | ||
* Session cookie jar. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class SessionCookieJar extends AbstractPersistentCookieJar | ||
{ | ||
/** @var string */ | ||
protected $key; | ||
|
||
/** | ||
* Creates a session cookie jar. | ||
* | ||
* @param string $key The key. | ||
* @param \Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface|null $cookieFactory The cookie factory. | ||
*/ | ||
public function __construct($key, CookieFactoryInterface $cookieFactory = null) | ||
{ | ||
$this->setKey($key); | ||
|
||
parent::__construct($cookieFactory); | ||
} | ||
|
||
/** | ||
* Gets the key. | ||
* | ||
* @return string The key. | ||
*/ | ||
public function getKey() | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* Sets the key. | ||
* | ||
* @param string $key The key. | ||
*/ | ||
public function setKey($key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load() | ||
{ | ||
$this->unserialize(isset($_SESSION[$this->key]) ? $_SESSION[$this->key] : null); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save() | ||
{ | ||
$_SESSION[$this->key] = $this->serialize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.