maj+ header
This commit is contained in:
@@ -16,21 +16,18 @@ use Grav\Framework\Cache\Exception\InvalidArgumentException;
|
||||
*/
|
||||
trait CacheTrait
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
/** @var string */
|
||||
private $namespace = '';
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
*/
|
||||
/** @var int|null */
|
||||
private $defaultLifetime = null;
|
||||
|
||||
/**
|
||||
* @var \stdClass
|
||||
*/
|
||||
/** @var \stdClass */
|
||||
private $miss;
|
||||
|
||||
/** @var bool */
|
||||
private $validation = true;
|
||||
|
||||
/**
|
||||
* Always call from constructor.
|
||||
*
|
||||
@@ -45,6 +42,14 @@ trait CacheTrait
|
||||
$this->miss = new \stdClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $validation
|
||||
*/
|
||||
public function setValidation($validation)
|
||||
{
|
||||
$this->validation = (bool) $validation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -307,6 +312,10 @@ trait CacheTrait
|
||||
*/
|
||||
protected function validateKeys($keys)
|
||||
{
|
||||
if (!$this->validation) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$this->validateKey($key);
|
||||
}
|
||||
|
||||
@@ -24,11 +24,6 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface
|
||||
*/
|
||||
public function reverse()
|
||||
{
|
||||
// TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4).
|
||||
if (!method_exists($this, 'createFrom')) {
|
||||
return new static(array_reverse($this->toArray()));
|
||||
}
|
||||
|
||||
return $this->createFrom(array_reverse($this->toArray()));
|
||||
}
|
||||
|
||||
@@ -42,11 +37,6 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface
|
||||
$keys = $this->getKeys();
|
||||
shuffle($keys);
|
||||
|
||||
// TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4).
|
||||
if (!method_exists($this, 'createFrom')) {
|
||||
return new static(array_replace(array_flip($keys), $this->toArray()));
|
||||
}
|
||||
|
||||
return $this->createFrom(array_replace(array_flip($keys), $this->toArray()));
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ class ContentBlock implements ContentBlockInterface
|
||||
protected $tokenTemplate = '@@BLOCK-%s@@';
|
||||
protected $content = '';
|
||||
protected $blocks = [];
|
||||
protected $checksum;
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
@@ -40,6 +41,7 @@ class ContentBlock implements ContentBlockInterface
|
||||
/**
|
||||
* @param array $serialized
|
||||
* @return ContentBlockInterface
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function fromArray(array $serialized)
|
||||
{
|
||||
@@ -48,14 +50,14 @@ class ContentBlock implements ContentBlockInterface
|
||||
$id = isset($serialized['id']) ? $serialized['id'] : null;
|
||||
|
||||
if (!$type || !$id || !is_a($type, 'Grav\Framework\ContentBlock\ContentBlockInterface', true)) {
|
||||
throw new \RuntimeException('Bad data');
|
||||
throw new \InvalidArgumentException('Bad data');
|
||||
}
|
||||
|
||||
/** @var ContentBlockInterface $instance */
|
||||
$instance = new $type($id);
|
||||
$instance->build($serialized);
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException(sprintf('Cannot unserialize Block: %s', $e->getMessage()), $e->getCode(), $e);
|
||||
throw new \InvalidArgumentException(sprintf('Cannot unserialize Block: %s', $e->getMessage()), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
@@ -104,9 +106,13 @@ class ContentBlock implements ContentBlockInterface
|
||||
$array = [
|
||||
'_type' => get_class($this),
|
||||
'_version' => $this->version,
|
||||
'id' => $this->id,
|
||||
'id' => $this->id
|
||||
];
|
||||
|
||||
if ($this->checksum) {
|
||||
$array['checksum'] = $this->checksum;
|
||||
}
|
||||
|
||||
if ($this->content) {
|
||||
$array['content'] = $this->content;
|
||||
}
|
||||
@@ -158,6 +164,7 @@ class ContentBlock implements ContentBlockInterface
|
||||
$this->checkVersion($serialized);
|
||||
|
||||
$this->id = isset($serialized['id']) ? $serialized['id'] : $this->generateId();
|
||||
$this->checksum = isset($serialized['checksum']) ? $serialized['checksum'] : null;
|
||||
|
||||
if (isset($serialized['content'])) {
|
||||
$this->setContent($serialized['content']);
|
||||
@@ -169,6 +176,25 @@ class ContentBlock implements ContentBlockInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $checksum
|
||||
* @return $this
|
||||
*/
|
||||
public function setChecksum($checksum)
|
||||
{
|
||||
$this->checksum = $checksum;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getChecksum()
|
||||
{
|
||||
return $this->checksum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return $this
|
||||
@@ -222,7 +248,7 @@ class ContentBlock implements ContentBlockInterface
|
||||
*/
|
||||
protected function checkVersion(array $serialized)
|
||||
{
|
||||
$version = isset($serialized['_version']) ? (string) $serialized['_version'] : '1';
|
||||
$version = isset($serialized['_version']) ? (int) $serialized['_version'] : 1;
|
||||
if ($version !== $this->version) {
|
||||
throw new \RuntimeException(sprintf('Unsupported version %s', $version));
|
||||
}
|
||||
|
||||
@@ -61,6 +61,17 @@ interface ContentBlockInterface extends \Serializable
|
||||
*/
|
||||
public function build(array $serialized);
|
||||
|
||||
/**
|
||||
* @param string $checksum
|
||||
* @return $this
|
||||
*/
|
||||
public function setChecksum($checksum);
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getChecksum();
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return $this
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace Grav\Framework\ContentBlock;
|
||||
*/
|
||||
class HtmlBlock extends ContentBlock implements HtmlBlockInterface
|
||||
{
|
||||
protected $version = 1;
|
||||
protected $frameworks = [];
|
||||
protected $styles = [];
|
||||
protected $scripts = [];
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
interface FormatterInterface
|
||||
{
|
||||
/**
|
||||
* Get default file extension from current formatter (with dot).
|
||||
*
|
||||
* Default file extension is the first defined extension.
|
||||
*
|
||||
* @return string File extension (can be empty).
|
||||
*/
|
||||
public function getDefaultFileExtension();
|
||||
|
||||
/**
|
||||
* Get file extensions supported by current formatter (with dot).
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSupportedFileExtensions();
|
||||
|
||||
/**
|
||||
* Encode data into a string.
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function encode($data);
|
||||
|
||||
/**
|
||||
* Decode a string into data.
|
||||
*
|
||||
* @param string $data
|
||||
* @return array
|
||||
*/
|
||||
public function decode($data);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
class IniFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* IniFormatter constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.ini'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return $this->getDefaultFileExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFileExtension()
|
||||
{
|
||||
$extensions = $this->getSupportedFileExtensions();
|
||||
|
||||
return (string) reset($extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSupportedFileExtensions()
|
||||
{
|
||||
return (array) $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
$string = '';
|
||||
foreach ($data as $key => $value) {
|
||||
$string .= $key . '="' . preg_replace(
|
||||
['/"/', '/\\\/', "/\t/", "/\n/", "/\r/"],
|
||||
['\"', '\\\\', '\t', '\n', '\r'],
|
||||
$value
|
||||
) . "\"\n";
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
$decoded = @parse_ini_string($data);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException('Decoding INI failed');
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
class JsonFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.json',
|
||||
'encode_options' => 0,
|
||||
'decode_assoc' => true
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return $this->getDefaultFileExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFileExtension()
|
||||
{
|
||||
$extensions = $this->getSupportedFileExtensions();
|
||||
|
||||
return (string) reset($extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSupportedFileExtensions()
|
||||
{
|
||||
return (array) $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
$encoded = @json_encode($data, $this->config['encode_options']);
|
||||
|
||||
if ($encoded === false) {
|
||||
throw new \RuntimeException('Encoding JSON failed');
|
||||
}
|
||||
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
$decoded = @json_decode($data, $this->config['decode_assoc']);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException('Decoding JSON failed');
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
class MarkdownFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
/** @var FormatterInterface */
|
||||
private $headerFormatter;
|
||||
|
||||
public function __construct(array $config = [], FormatterInterface $headerFormatter = null)
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.md',
|
||||
'header' => 'header',
|
||||
'body' => 'markdown',
|
||||
'raw' => 'frontmatter',
|
||||
'yaml' => ['inline' => 20]
|
||||
];
|
||||
|
||||
$this->headerFormatter = $headerFormatter ?: new YamlFormatter($this->config['yaml']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return $this->getDefaultFileExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFileExtension()
|
||||
{
|
||||
$extensions = $this->getSupportedFileExtensions();
|
||||
|
||||
return (string) reset($extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSupportedFileExtensions()
|
||||
{
|
||||
return (array) $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
$headerVar = $this->config['header'];
|
||||
$bodyVar = $this->config['body'];
|
||||
|
||||
$header = isset($data[$headerVar]) ? (array) $data[$headerVar] : [];
|
||||
$body = isset($data[$bodyVar]) ? (string) $data[$bodyVar] : '';
|
||||
|
||||
// Create Markdown file with YAML header.
|
||||
$encoded = '';
|
||||
if ($header) {
|
||||
$encoded = "---\n" . trim($this->headerFormatter->encode($data['header'])) . "\n---\n\n";
|
||||
}
|
||||
$encoded .= $body;
|
||||
|
||||
// Normalize line endings to Unix style.
|
||||
$encoded = preg_replace("/(\r\n|\r)/", "\n", $encoded);
|
||||
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
$headerVar = $this->config['header'];
|
||||
$bodyVar = $this->config['body'];
|
||||
$rawVar = $this->config['raw'];
|
||||
|
||||
$content = [
|
||||
$headerVar => [],
|
||||
$bodyVar => ''
|
||||
];
|
||||
|
||||
$headerRegex = "/^---\n(.+?)\n---\n{0,}(.*)$/uis";
|
||||
|
||||
// Normalize line endings to Unix style.
|
||||
$data = preg_replace("/(\r\n|\r)/", "\n", $data);
|
||||
|
||||
// Parse header.
|
||||
preg_match($headerRegex, ltrim($data), $matches);
|
||||
if(empty($matches)) {
|
||||
$content[$bodyVar] = $data;
|
||||
} else {
|
||||
// Normalize frontmatter.
|
||||
$frontmatter = preg_replace("/\n\t/", "\n ", $matches[1]);
|
||||
if ($rawVar) {
|
||||
$content[$rawVar] = $frontmatter;
|
||||
}
|
||||
$content[$headerVar] = $this->headerFormatter->decode($frontmatter);
|
||||
$content[$bodyVar] = $matches[2];
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
class SerializeFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* IniFormatter constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.ser'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return $this->getDefaultFileExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFileExtension()
|
||||
{
|
||||
$extensions = $this->getSupportedFileExtensions();
|
||||
|
||||
return (string) reset($extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSupportedFileExtensions()
|
||||
{
|
||||
return (array) $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
return serialize($this->preserveLines($data, ["\n", "\r"], ['\\n', '\\r']));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
$decoded = @unserialize($data);
|
||||
|
||||
if ($decoded === false) {
|
||||
throw new \RuntimeException('Decoding serialized data failed');
|
||||
}
|
||||
|
||||
return $this->preserveLines($decoded, ['\\n', '\\r'], ["\n", "\r"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserve new lines, recursive function.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param array $search
|
||||
* @param array $replace
|
||||
* @return mixed
|
||||
*/
|
||||
protected function preserveLines($data, $search, $replace)
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$data = str_replace($search, $replace, $data);
|
||||
} elseif (is_array($data)) {
|
||||
foreach ($data as &$value) {
|
||||
$value = $this->preserveLines($value, $search, $replace);
|
||||
}
|
||||
unset($value);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\File\Formatter
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\File\Formatter;
|
||||
|
||||
use Symfony\Component\Yaml\Exception\DumpException;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml as YamlParser;
|
||||
use RocketTheme\Toolbox\Compat\Yaml\Yaml as FallbackYamlParser;
|
||||
|
||||
class YamlFormatter implements FormatterInterface
|
||||
{
|
||||
/** @var array */
|
||||
private $config;
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config + [
|
||||
'file_extension' => '.yaml',
|
||||
'inline' => 5,
|
||||
'indent' => 2,
|
||||
'native' => true,
|
||||
'compat' => true
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
|
||||
*/
|
||||
public function getFileExtension()
|
||||
{
|
||||
return $this->getDefaultFileExtension();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefaultFileExtension()
|
||||
{
|
||||
$extensions = $this->getSupportedFileExtensions();
|
||||
|
||||
return (string) reset($extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSupportedFileExtensions()
|
||||
{
|
||||
return (array) $this->config['file_extension'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data)
|
||||
{
|
||||
try {
|
||||
return (string) YamlParser::dump(
|
||||
$data,
|
||||
$this->config['inline'],
|
||||
$this->config['indent'],
|
||||
YamlParser::DUMP_EXCEPTION_ON_INVALID_TYPE
|
||||
);
|
||||
} catch (DumpException $e) {
|
||||
throw new \RuntimeException('Encoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data)
|
||||
{
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if ($this->config['native'] && function_exists('yaml_parse')) {
|
||||
// Safely decode YAML.
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
$decoded = @yaml_parse($data);
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
|
||||
if ($decoded !== false) {
|
||||
return (array) $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return (array) YamlParser::parse($data);
|
||||
} catch (ParseException $e) {
|
||||
if ($this->config['compat']) {
|
||||
return (array) FallbackYamlParser::parse($data);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Decoding YAML failed: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,11 +32,6 @@ trait ObjectCollectionTrait
|
||||
$list[$key] = is_object($value) ? clone $value : $value;
|
||||
}
|
||||
|
||||
// TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4).
|
||||
if (!method_exists($this, 'createFrom')) {
|
||||
return new static($list);
|
||||
}
|
||||
|
||||
return $this->createFrom($list);
|
||||
}
|
||||
|
||||
@@ -170,12 +165,7 @@ trait ObjectCollectionTrait
|
||||
{
|
||||
$collections = [];
|
||||
foreach ($this->group($property) as $id => $elements) {
|
||||
// TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4).
|
||||
if (!method_exists($this, 'createFrom')) {
|
||||
$collection = new static($elements);
|
||||
} else {
|
||||
$collection = $this->createFrom($elements);
|
||||
}
|
||||
$collection = $this->createFrom($elements);
|
||||
|
||||
$collections[$id] = $collection;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Grav\Framework\Object\Base;
|
||||
*/
|
||||
trait ObjectTrait
|
||||
{
|
||||
static protected $prefix;
|
||||
/** @var string */
|
||||
static protected $type;
|
||||
|
||||
/**
|
||||
@@ -23,18 +23,28 @@ trait ObjectTrait
|
||||
*/
|
||||
private $_key;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getTypePrefix()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $prefix
|
||||
* @return string
|
||||
*/
|
||||
public function getType($prefix = true)
|
||||
{
|
||||
$type = $prefix ? $this->getTypePrefix() : '';
|
||||
|
||||
if (static::$type) {
|
||||
return ($prefix ? static::$prefix : '') . static::$type;
|
||||
return $type . static::$type;
|
||||
}
|
||||
|
||||
$class = get_class($this);
|
||||
return ($prefix ? static::$prefix : '') . strtolower(substr($class, strrpos($class, '\\') + 1));
|
||||
return $type . strtolower(substr($class, strrpos($class, '\\') + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +118,7 @@ trait ObjectTrait
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize($this->jsonSerialize());
|
||||
return serialize($this->doSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,6 +134,14 @@ trait ObjectTrait
|
||||
$this->doUnserialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function doSerialize()
|
||||
{
|
||||
return $this->jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $serialized
|
||||
*/
|
||||
@@ -159,10 +177,13 @@ trait ObjectTrait
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return $this
|
||||
*/
|
||||
protected function setKey($key)
|
||||
{
|
||||
$this->_key = (string) $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract protected function doHasProperty($property);
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\Object
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Object\Collection;
|
||||
|
||||
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
|
||||
use Doctrine\Common\Collections\Expr\Comparison;
|
||||
|
||||
class ObjectExpressionVisitor extends ClosureExpressionVisitor
|
||||
{
|
||||
/**
|
||||
* Accesses the field of a given object.
|
||||
*
|
||||
* @param object $object
|
||||
* @param string $field
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getObjectFieldValue($object, $field)
|
||||
{
|
||||
$op = $value = null;
|
||||
|
||||
$pos = strpos($field, '(');
|
||||
if (false !== $pos) {
|
||||
list ($op, $field) = explode('(', $field, 2);
|
||||
$field = rtrim($field, ')');
|
||||
}
|
||||
|
||||
if (isset($object[$field])) {
|
||||
$value = $object[$field];
|
||||
} else {
|
||||
$accessors = array('', 'get', 'is');
|
||||
|
||||
foreach ($accessors as $accessor) {
|
||||
$accessor .= $field;
|
||||
|
||||
if (!method_exists($object, $accessor)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $object->{$accessor}();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($op) {
|
||||
$function = 'filter' . ucfirst(strtolower($op));
|
||||
if (method_exists(static::class, $function)) {
|
||||
$value = static::$function($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function filterLower($str)
|
||||
{
|
||||
return mb_strtolower($str);
|
||||
}
|
||||
|
||||
public static function filterUpper($str)
|
||||
{
|
||||
return mb_strtoupper($str);
|
||||
}
|
||||
|
||||
public static function filterLength($str)
|
||||
{
|
||||
return mb_strlen($str);
|
||||
}
|
||||
|
||||
public static function filterLtrim($str)
|
||||
{
|
||||
return ltrim($str);
|
||||
}
|
||||
|
||||
public static function filterRtrim($str)
|
||||
{
|
||||
return rtrim($str);
|
||||
}
|
||||
|
||||
public static function filterTrim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for sorting arrays of objects based on multiple fields + orientations.
|
||||
*
|
||||
* @param string $name
|
||||
* @param int $orientation
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return \Closure
|
||||
*/
|
||||
public static function sortByField($name, $orientation = 1, \Closure $next = null)
|
||||
{
|
||||
if (!$next) {
|
||||
$next = function() {
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
return function ($a, $b) use ($name, $next, $orientation) {
|
||||
$aValue = static::getObjectFieldValue($a, $name);
|
||||
$bValue = static::getObjectFieldValue($b, $name);
|
||||
|
||||
if ($aValue === $bValue) {
|
||||
return $next($a, $b);
|
||||
}
|
||||
|
||||
return (($aValue > $bValue) ? 1 : -1) * $orientation;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function walkComparison(Comparison $comparison)
|
||||
{
|
||||
$field = $comparison->getField();
|
||||
$value = $comparison->getValue()->getValue(); // shortcut for walkValue()
|
||||
|
||||
switch ($comparison->getOperator()) {
|
||||
case Comparison::EQ:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) === $value;
|
||||
};
|
||||
|
||||
case Comparison::NEQ:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) !== $value;
|
||||
};
|
||||
|
||||
case Comparison::LT:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) < $value;
|
||||
};
|
||||
|
||||
case Comparison::LTE:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) <= $value;
|
||||
};
|
||||
|
||||
case Comparison::GT:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) > $value;
|
||||
};
|
||||
|
||||
case Comparison::GTE:
|
||||
return function ($object) use ($field, $value) {
|
||||
return static::getObjectFieldValue($object, $field) >= $value;
|
||||
};
|
||||
|
||||
case Comparison::IN:
|
||||
return function ($object) use ($field, $value) {
|
||||
return \in_array(static::getObjectFieldValue($object, $field), $value, true);
|
||||
};
|
||||
|
||||
case Comparison::NIN:
|
||||
return function ($object) use ($field, $value) {
|
||||
return !\in_array(static::getObjectFieldValue($object, $field), $value, true);
|
||||
};
|
||||
|
||||
case Comparison::CONTAINS:
|
||||
return function ($object) use ($field, $value) {
|
||||
return false !== strpos(static::getObjectFieldValue($object, $field), $value);
|
||||
};
|
||||
|
||||
case Comparison::MEMBER_OF:
|
||||
return function ($object) use ($field, $value) {
|
||||
$fieldValues = static::getObjectFieldValue($object, $field);
|
||||
if (!is_array($fieldValues)) {
|
||||
$fieldValues = iterator_to_array($fieldValues);
|
||||
}
|
||||
return \in_array($value, $fieldValues, true);
|
||||
};
|
||||
|
||||
case Comparison::STARTS_WITH:
|
||||
return function ($object) use ($field, $value) {
|
||||
return 0 === strpos(static::getObjectFieldValue($object, $field), $value);
|
||||
};
|
||||
|
||||
case Comparison::ENDS_WITH:
|
||||
return function ($object) use ($field, $value) {
|
||||
return $value === substr(static::getObjectFieldValue($object, $field), -strlen($value));
|
||||
};
|
||||
|
||||
|
||||
default:
|
||||
throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,14 @@
|
||||
|
||||
namespace Grav\Framework\Object\Interfaces;
|
||||
|
||||
use Doctrine\Common\Collections\Selectable;
|
||||
use Grav\Framework\Collection\CollectionInterface;
|
||||
|
||||
/**
|
||||
* ObjectCollection Interface
|
||||
* @package Grav\Framework\Collection
|
||||
*/
|
||||
interface ObjectCollectionInterface extends CollectionInterface, ObjectInterface
|
||||
interface ObjectCollectionInterface extends CollectionInterface, Selectable, ObjectInterface
|
||||
{
|
||||
/**
|
||||
* Create a copy from this collection by cloning all objects in the collection.
|
||||
|
||||
@@ -8,9 +8,11 @@
|
||||
|
||||
namespace Grav\Framework\Object;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Grav\Framework\Collection\ArrayCollection;
|
||||
use Grav\Framework\Object\Access\NestedPropertyCollectionTrait;
|
||||
use Grav\Framework\Object\Base\ObjectCollectionTrait;
|
||||
use Grav\Framework\Object\Collection\ObjectExpressionVisitor;
|
||||
use Grav\Framework\Object\Interfaces\NestedObjectInterface;
|
||||
use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
|
||||
|
||||
@@ -36,6 +38,39 @@ class ObjectCollection extends ArrayCollection implements ObjectCollectionInterf
|
||||
$this->setKey($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function matching(Criteria $criteria)
|
||||
{
|
||||
$expr = $criteria->getWhereExpression();
|
||||
$filtered = $this->getElements();
|
||||
|
||||
if ($expr) {
|
||||
$visitor = new ObjectExpressionVisitor();
|
||||
$filter = $visitor->dispatch($expr);
|
||||
$filtered = array_filter($filtered, $filter);
|
||||
}
|
||||
|
||||
if ($orderings = $criteria->getOrderings()) {
|
||||
$next = null;
|
||||
foreach (array_reverse($orderings) as $field => $ordering) {
|
||||
$next = ObjectExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
|
||||
}
|
||||
|
||||
uasort($filtered, $next);
|
||||
}
|
||||
|
||||
$offset = $criteria->getFirstResult();
|
||||
$length = $criteria->getMaxResults();
|
||||
|
||||
if ($offset || $length) {
|
||||
$filtered = array_slice($filtered, (int)$offset, $length);
|
||||
}
|
||||
|
||||
return $this->createFrom($filtered);
|
||||
}
|
||||
|
||||
protected function getElements()
|
||||
{
|
||||
return $this->toArray();
|
||||
|
||||
@@ -95,10 +95,10 @@ trait ObjectPropertyTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed $default Default value if property has not been set.
|
||||
* @param bool $doCreate Set true to create variable.
|
||||
* @return mixed Property value.
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed $default Default value if property has not been set.
|
||||
* @param callable|bool $doCreate Set true to create variable.
|
||||
* @return mixed Property value.
|
||||
*/
|
||||
protected function &doGetProperty($property, $default = null, $doCreate = false)
|
||||
{
|
||||
|
||||
@@ -178,7 +178,7 @@ class Route
|
||||
*/
|
||||
public function withGravParam($param, $value)
|
||||
{
|
||||
return $this->withParam('gravParams', $param, $value);
|
||||
return $this->withParam('gravParams', $param, null !== $value ? (string)$value : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,17 +222,16 @@ class Route
|
||||
protected function withParam($type, $param, $value)
|
||||
{
|
||||
$oldValue = isset($this->{$type}[$param]) ? $this->{$type}[$param] : null;
|
||||
$newValue = null !== $value ? (string)$value : null;
|
||||
|
||||
if ($oldValue === $newValue) {
|
||||
if ($oldValue === $value) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$new = clone $this;
|
||||
if ($newValue === null) {
|
||||
if ($value === null) {
|
||||
unset($new->{$type}[$param]);
|
||||
} else {
|
||||
$new->{$type}[$param] = $newValue;
|
||||
$new->{$type}[$param] = $value;
|
||||
}
|
||||
|
||||
return $new;
|
||||
|
||||
@@ -28,6 +28,23 @@ class RouteFactory
|
||||
return new Route($parts);
|
||||
}
|
||||
|
||||
public static function createFromString($path)
|
||||
{
|
||||
$path = ltrim($path, '/');
|
||||
$parts = [
|
||||
'path' => $path,
|
||||
'query' => '',
|
||||
'query_params' => [],
|
||||
'grav' => [
|
||||
'root' => self::$root,
|
||||
'language' => self::$language,
|
||||
'route' => $path,
|
||||
'params' => ''
|
||||
],
|
||||
];
|
||||
return new Route($parts);
|
||||
}
|
||||
|
||||
public static function getRoot()
|
||||
{
|
||||
return self::$root;
|
||||
|
||||
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\Session
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Session;
|
||||
|
||||
/**
|
||||
* Class Session
|
||||
* @package Grav\Framework\Session
|
||||
*/
|
||||
class Session implements SessionInterface
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $started = false;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (null === self::$instance) {
|
||||
throw new \RuntimeException("Session hasn't been initialized.", 500);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
// Session is a singleton.
|
||||
if (\PHP_SAPI === 'cli') {
|
||||
self::$instance = $this;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (null !== self::$instance) {
|
||||
throw new \RuntimeException('Session has already been initialized.', 500);
|
||||
}
|
||||
|
||||
// Destroy any existing sessions started with session.auto_start
|
||||
if ($this->isSessionStarted()) {
|
||||
session_unset();
|
||||
session_destroy();
|
||||
}
|
||||
|
||||
// Set default options.
|
||||
$options += array(
|
||||
'cache_limiter' => 'nocache',
|
||||
'use_trans_sid' => 0,
|
||||
'use_cookies' => 1,
|
||||
'lazy_write' => 1,
|
||||
'use_strict_mode' => 1
|
||||
);
|
||||
|
||||
$this->setOptions($options);
|
||||
|
||||
session_register_shutdown();
|
||||
|
||||
self::$instance = $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return session_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
session_id($id);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return session_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
session_name($name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
if (headers_sent() || \PHP_SESSION_ACTIVE === session_status()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$allowedOptions = [
|
||||
'save_path' => true,
|
||||
'name' => true,
|
||||
'save_handler' => true,
|
||||
'gc_probability' => true,
|
||||
'gc_divisor' => true,
|
||||
'gc_maxlifetime' => true,
|
||||
'serialize_handler' => true,
|
||||
'cookie_lifetime' => true,
|
||||
'cookie_path' => true,
|
||||
'cookie_domain' => true,
|
||||
'cookie_secure' => true,
|
||||
'cookie_httponly' => true,
|
||||
'use_strict_mode' => true,
|
||||
'use_cookies' => true,
|
||||
'use_only_cookies' => true,
|
||||
'referer_check' => true,
|
||||
'cache_limiter' => true,
|
||||
'cache_expire' => true,
|
||||
'use_trans_sid' => true,
|
||||
'trans_sid_tags' => true, // PHP 7.1
|
||||
'trans_sid_hosts' => true, // PHP 7.1
|
||||
'sid_length' => true, // PHP 7.1
|
||||
'sid_bits_per_character' => true, // PHP 7.1
|
||||
'upload_progress.enabled' => true,
|
||||
'upload_progress.cleanup' => true,
|
||||
'upload_progress.prefix' => true,
|
||||
'upload_progress.name' => true,
|
||||
'upload_progress.freq' => true,
|
||||
'upload_progress.min-freq' => true,
|
||||
'lazy_write' => true,
|
||||
'url_rewriter.tags' => true, // Not used in PHP 7.1
|
||||
'hash_function' => true, // Not used in PHP 7.1
|
||||
'hash_bits_per_character' => true, // Not used in PHP 7.1
|
||||
'entropy_file' => true, // Not used in PHP 7.1
|
||||
'entropy_length' => true, // Not used in PHP 7.1
|
||||
];
|
||||
|
||||
foreach ($options as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
// Allow nested options.
|
||||
foreach ($value as $key2 => $value2) {
|
||||
$ckey = "{$key}.{$key2}";
|
||||
if (isset($value2, $allowedOptions[$ckey])) {
|
||||
$this->ini_set("session.{$ckey}", $value2);
|
||||
}
|
||||
}
|
||||
} elseif (isset($value, $allowedOptions[$key])) {
|
||||
$this->ini_set("session.{$key}", $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function start($readonly = false)
|
||||
{
|
||||
// Protection against invalid session cookie names throwing exception: http://php.net/manual/en/function.session-id.php#116836
|
||||
if (isset($_COOKIE[session_name()]) && !preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $_COOKIE[session_name()])) {
|
||||
unset($_COOKIE[session_name()]);
|
||||
}
|
||||
|
||||
$options = $readonly ? ['read_and_close' => '1'] : [];
|
||||
|
||||
$success = @session_start($options);
|
||||
if (!$success) {
|
||||
$last = error_get_last();
|
||||
$error = $last ? $last['message'] : 'Unknown error';
|
||||
throw new \RuntimeException('Failed to start session: ' . $error, 500);
|
||||
}
|
||||
|
||||
$params = session_get_cookie_params();
|
||||
|
||||
setcookie(
|
||||
session_name(),
|
||||
session_id(),
|
||||
time() + $params['lifetime'],
|
||||
$params['path'],
|
||||
$params['domain'],
|
||||
$params['secure'],
|
||||
$params['httponly']
|
||||
);
|
||||
|
||||
$this->started = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function invalidate()
|
||||
{
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(
|
||||
session_name(),
|
||||
'',
|
||||
time() - 42000,
|
||||
$params['path'],
|
||||
$params['domain'],
|
||||
$params['secure'],
|
||||
$params['httponly']
|
||||
);
|
||||
|
||||
session_unset();
|
||||
session_destroy();
|
||||
|
||||
$this->started = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if ($this->started) {
|
||||
session_write_close();
|
||||
}
|
||||
|
||||
$this->started = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
session_unset();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $_SESSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($_SESSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function isStarted()
|
||||
{
|
||||
return $this->started;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$_SESSION[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
unset($_SESSION[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* http://php.net/manual/en/function.session-status.php#113468
|
||||
* Check if session is started nicely.
|
||||
* @return bool
|
||||
*/
|
||||
protected function isSessionStarted()
|
||||
{
|
||||
return \PHP_SAPI !== 'cli' ? \PHP_SESSION_ACTIVE === session_status() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
protected function ini_set($key, $value)
|
||||
{
|
||||
if (!is_string($value)) {
|
||||
if (is_bool($value)) {
|
||||
$value = $value ? '1' : '0';
|
||||
}
|
||||
$value = (string)$value;
|
||||
}
|
||||
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Grav\Framework\Session
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Session;
|
||||
|
||||
/**
|
||||
* Class Session
|
||||
* @package Grav\Framework\Session
|
||||
*/
|
||||
interface SessionInterface extends \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Get current session instance.
|
||||
*
|
||||
* @return Session
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public static function getInstance();
|
||||
|
||||
/**
|
||||
* Get session ID
|
||||
*
|
||||
* @return string|null Session ID
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* Set session ID
|
||||
*
|
||||
* @param string $id Session ID
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id);
|
||||
|
||||
/**
|
||||
* Get session name
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Set session name
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setName($name);
|
||||
|
||||
/**
|
||||
* Sets session.* ini variables.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @see http://php.net/session.configuration
|
||||
*/
|
||||
public function setOptions(array $options);
|
||||
|
||||
/**
|
||||
* Starts the session storage
|
||||
*
|
||||
* @param bool $readonly
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function start($readonly = false);
|
||||
|
||||
/**
|
||||
* Invalidates the current session.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function invalidate();
|
||||
|
||||
/**
|
||||
* Force the session to be saved and closed
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function close();
|
||||
|
||||
/**
|
||||
* Free all session variables.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function clear();
|
||||
|
||||
/**
|
||||
* Returns all session variables.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll();
|
||||
|
||||
/**
|
||||
* Retrieve an external iterator
|
||||
*
|
||||
* @return \ArrayIterator Return an ArrayIterator of $_SESSION
|
||||
*/
|
||||
public function getIterator();
|
||||
|
||||
/**
|
||||
* Checks if the session was started.
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public function isStarted();
|
||||
|
||||
/**
|
||||
* Checks if session variable is defined.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name);
|
||||
|
||||
/**
|
||||
* Returns session variable.
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name);
|
||||
|
||||
/**
|
||||
* Sets session variable.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __set($name, $value);
|
||||
|
||||
/**
|
||||
* Removes session variable.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function __unset($name);
|
||||
}
|
||||
Reference in New Issue
Block a user