Initial commit: apps.supersam24.ru
This commit is contained in:
commit
72afea602d
2747 files changed
+682150
No files matched your search
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Exception;
|
||||
|
||||
/**
|
||||
* Env ParseException
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class ParseException extends \ErrorException
|
||||
{
|
||||
/**
|
||||
* Constructs a ParseException
|
||||
*
|
||||
* @param string $message The value to parse
|
||||
* @param string $line The line of the value
|
||||
* @param int $line_num The line num of the value
|
||||
*/
|
||||
public function __construct($message, $line = null, $line_num = null)
|
||||
{
|
||||
$message = $this->createMessage($message, $line, $line_num);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a ParseException message
|
||||
*
|
||||
* @param string $message The value to parse
|
||||
* @param string $line The line of the value
|
||||
* @param int $line_num The line num of the value
|
||||
*
|
||||
* @return string The exception message
|
||||
*/
|
||||
private function createMessage($message, $line, $line_num)
|
||||
{
|
||||
if (!is_null($line)) {
|
||||
$message .= sprintf(" near %s", $line);
|
||||
}
|
||||
|
||||
if (!is_null($line_num)) {
|
||||
$message .= sprintf(" at line %d", $line_num);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Helper;
|
||||
|
||||
/**
|
||||
* The class for string helping
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
class StringHelper
|
||||
{
|
||||
/**
|
||||
* The bool variants available in .env
|
||||
*
|
||||
* @var array $bool_variants
|
||||
*/
|
||||
private static $bool_variants = array(
|
||||
'true', 'false', 'yes', 'no'
|
||||
);
|
||||
|
||||
/**
|
||||
* Returns if value is a bool
|
||||
*
|
||||
* @param string $value The value to test
|
||||
*
|
||||
* @return bool Is a value a bool
|
||||
*/
|
||||
public function isBool($value)
|
||||
{
|
||||
return in_array(strtolower($value), self::$bool_variants);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the bool is in a string
|
||||
*
|
||||
* @param string $value The value to test
|
||||
* @param bool $quoted_string Is the context a quoted string
|
||||
* @param int $word_count The amount of words in the sentence
|
||||
*
|
||||
* @return bool Is a value a bool in a string
|
||||
*/
|
||||
public function isBoolInString($value, $quoted_string, $word_count)
|
||||
{
|
||||
return (is_bool($value)) && ($quoted_string || $word_count >= 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if value is null
|
||||
*
|
||||
* @param string $value The value to test
|
||||
*
|
||||
* @return bool Is a value null
|
||||
*/
|
||||
public function isNull($value)
|
||||
{
|
||||
return $value === 'null';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if value is number
|
||||
*
|
||||
* @param string $value The value to test
|
||||
*
|
||||
* @return bool Is a value a number
|
||||
*/
|
||||
public function isNumber($value)
|
||||
{
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if value is a string
|
||||
*
|
||||
* @param string $value The value to test
|
||||
*
|
||||
* @return bool Is a value a string
|
||||
*/
|
||||
public function isString($value)
|
||||
{
|
||||
return $this->startsWith('\'', $value) || $this->startsWith('"', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if variable value is a clone, e.g. BOOL = $(BOOL_1)
|
||||
*
|
||||
* @param string $value The value to test
|
||||
* @param array $matches The matches of the variables
|
||||
* @param bool $quoted_string If the value is in a quoted string
|
||||
*
|
||||
* @return bool Is a value null
|
||||
*/
|
||||
public function isVariableClone($value, $matches, $quoted_string)
|
||||
{
|
||||
return (count($matches[0]) === 1) && $value == $matches[0][0] && !$quoted_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if value starts with a value
|
||||
*
|
||||
* @param string $string The value to search for
|
||||
* @param string $line The line to test
|
||||
*
|
||||
* @return bool Returns if the line starts with value
|
||||
*/
|
||||
public function startsWith($string, $line)
|
||||
{
|
||||
return $string === "" || strrpos($line, $string, -strlen($line)) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if value starts with a number
|
||||
*
|
||||
* @param string $line The line to test
|
||||
*
|
||||
* @return bool Returns if the line starts with a number
|
||||
*/
|
||||
public function startsWithNumber($line)
|
||||
{
|
||||
return is_numeric(substr($line, 0, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips comments from a value
|
||||
*
|
||||
* @param string $value The value to strip comments from
|
||||
*
|
||||
* @return string value
|
||||
*/
|
||||
public function stripComments($value)
|
||||
{
|
||||
$value = explode(" #", $value, 2);
|
||||
return trim($value[0]);
|
||||
}
|
||||
}
|
||||
Vendored
+244
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env;
|
||||
|
||||
use M1\Env\Exception\ParseException;
|
||||
use M1\Env\Helper\StringHelper;
|
||||
use M1\Env\Parser\ValueParser;
|
||||
use M1\Env\Parser\KeyParser;
|
||||
|
||||
/**
|
||||
* The .env parser
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* The Env key parser
|
||||
*
|
||||
* @var \M1\Env\Parser\KeyParser $key_parser
|
||||
*/
|
||||
private $key_parser;
|
||||
|
||||
/**
|
||||
* The line num of the current value
|
||||
*
|
||||
* @var int $line_num
|
||||
*/
|
||||
public $line_num;
|
||||
|
||||
/**
|
||||
* The current parsed values/lines
|
||||
*
|
||||
* @var array $lines
|
||||
*/
|
||||
public $lines = array();
|
||||
|
||||
/**
|
||||
* The String helper class
|
||||
*
|
||||
* @var \M1\Env\Helper\StringHelper $string_helper
|
||||
*/
|
||||
public $string_helper;
|
||||
|
||||
|
||||
/**
|
||||
* The Env value parser
|
||||
*
|
||||
* @var \M1\Env\Parser\ValueParser $value_parser
|
||||
*/
|
||||
public $value_parser;
|
||||
|
||||
/**
|
||||
* The parser constructor
|
||||
*
|
||||
* @param string $content The content to parse
|
||||
* @param array $context Variables context
|
||||
*/
|
||||
public function __construct($content, array $context = array())
|
||||
{
|
||||
$this->key_parser = new KeyParser($this);
|
||||
$this->value_parser = new ValueParser($this, $context);
|
||||
$this->string_helper = new StringHelper();
|
||||
|
||||
$this->doParse($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the .env and returns the contents statically
|
||||
*
|
||||
* @param string $content The content to parse
|
||||
* @param array $context Variables context
|
||||
*
|
||||
* @return array The .env contents
|
||||
*/
|
||||
public static function parse($content, array $context = array())
|
||||
{
|
||||
$parser = new Parser($content, $context);
|
||||
|
||||
return $parser->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the .env, parses it then returns the contents
|
||||
*
|
||||
* @param string $content The content to parse
|
||||
*
|
||||
* @return array The .env contents
|
||||
*/
|
||||
protected function doParse($content)
|
||||
{
|
||||
$raw_lines = array_filter($this->makeLines($content), 'strlen');
|
||||
|
||||
if (empty($raw_lines)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return $this->parseContent($raw_lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the string into an array
|
||||
*
|
||||
* @param string $content The string content to split
|
||||
*
|
||||
* @return array The array of lines to parse
|
||||
*/
|
||||
private function makeLines($content)
|
||||
{
|
||||
return explode("\n", str_replace(array("\r\n", "\n\r", "\r"), "\n", $content));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the .env line by line
|
||||
*
|
||||
* @param array $raw_lines The raw content of the file
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure
|
||||
*
|
||||
* @return array The .env contents
|
||||
*/
|
||||
private function parseContent(array $raw_lines)
|
||||
{
|
||||
$this->lines = array();
|
||||
$this->line_num = 0;
|
||||
|
||||
foreach ($raw_lines as $raw_line) {
|
||||
$this->line_num++;
|
||||
|
||||
if ($this->string_helper->startsWith('#', $raw_line) || !$raw_line) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->parseLine($raw_line);
|
||||
}
|
||||
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a line of the .env
|
||||
*
|
||||
* @param string $raw_line The raw content of the line
|
||||
*
|
||||
* @return array The parsed lines
|
||||
*/
|
||||
private function parseLine($raw_line)
|
||||
{
|
||||
$raw_line = $this->parseExport($raw_line);
|
||||
|
||||
list($key, $value) = $this->parseKeyValue($raw_line);
|
||||
|
||||
$key = $this->key_parser->parse($key);
|
||||
|
||||
if (!is_string($key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->lines[$key] = $this->value_parser->parse($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a export line of the .env
|
||||
*
|
||||
* @param string $raw_line The raw content of the line
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure
|
||||
*
|
||||
* @return string The parsed line
|
||||
*/
|
||||
private function parseExport($raw_line)
|
||||
{
|
||||
$line = trim($raw_line);
|
||||
|
||||
if ($this->string_helper->startsWith("export", $line)) {
|
||||
$export_line = explode("export", $raw_line, 2);
|
||||
|
||||
if (count($export_line) !== 2 || empty($export_line[1])) {
|
||||
throw new ParseException(
|
||||
'You must have a export key = value',
|
||||
$raw_line,
|
||||
$this->line_num
|
||||
);
|
||||
}
|
||||
|
||||
$line = trim($export_line[1]);
|
||||
}
|
||||
|
||||
return $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the key = value items from the line
|
||||
*
|
||||
* @param string $raw_line The raw content of the line
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure
|
||||
*
|
||||
* @return array The parsed lines
|
||||
*/
|
||||
private function parseKeyValue($raw_line)
|
||||
{
|
||||
$key_value = explode("=", $raw_line, 2);
|
||||
|
||||
if (count($key_value) !== 2) {
|
||||
throw new ParseException(
|
||||
'You must have a key = value',
|
||||
$raw_line,
|
||||
$this->line_num
|
||||
);
|
||||
}
|
||||
|
||||
return $key_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the .env
|
||||
*
|
||||
* @return array The .env contents
|
||||
*/
|
||||
public function getContent($keyName = null)
|
||||
{
|
||||
if (!is_null($keyName)) {
|
||||
return (array_key_exists($keyName, $this->lines)) ? $this->lines[$keyName] : null;
|
||||
}
|
||||
return $this->lines;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Parser;
|
||||
|
||||
/**
|
||||
* The abstract parser for Env
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
abstract class AbstractParser
|
||||
{
|
||||
/**
|
||||
* The parent parser
|
||||
*
|
||||
* @var \M1\Env\Parser $parser
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* The abstract parser constructor for Env
|
||||
*
|
||||
* @param \M1\Env\Parser $parser The parent parser
|
||||
*/
|
||||
public function __construct($parser)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Parser;
|
||||
|
||||
use M1\Env\Exception\ParseException;
|
||||
|
||||
/**
|
||||
* The key parser for Env
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
class KeyParser extends AbstractParser
|
||||
{
|
||||
/**
|
||||
* Parses a .env key
|
||||
*
|
||||
* @param string $key The key string
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If key contains a character that isn't alphanumeric or a _
|
||||
*
|
||||
* @return string|false The parsed key, or false if the key is a comment
|
||||
*/
|
||||
public function parse($key)
|
||||
{
|
||||
$key = trim($key);
|
||||
|
||||
if ($this->parser->string_helper->startsWith('#', $key)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ctype_alnum(str_replace('_', '', $key)) || $this->parser->string_helper->startsWithNumber($key)) {
|
||||
throw new ParseException(
|
||||
sprintf('Key can only contain alphanumeric and underscores and can not start with a number: %s', $key),
|
||||
$key,
|
||||
$this->parser->line_num
|
||||
);
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
+264
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Parser;
|
||||
|
||||
use M1\Env\Exception\ParseException;
|
||||
|
||||
/**
|
||||
* The value parser for Env
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
class ValueParser extends AbstractParser
|
||||
{
|
||||
/**
|
||||
* The regex to get the content between double quote (") strings, ignoring escaped quotes.
|
||||
* Unescaped: "(?:[^"\\]*(?:\\.)?)*"
|
||||
*
|
||||
* @var string REGEX_QUOTE_DOUBLE_STRING
|
||||
*/
|
||||
const REGEX_QUOTE_DOUBLE_STRING = '"(?:[^\"\\\\]*(?:\\\\.)?)*\"';
|
||||
|
||||
/**
|
||||
* The regex to get the content between single quote (') strings, ignoring escaped quotes
|
||||
* Unescaped: '(?:[^'\\]*(?:\\.)?)*'
|
||||
*
|
||||
* @var string REGEX_QUOTE_SINGLE_STRING
|
||||
*/
|
||||
const REGEX_QUOTE_SINGLE_STRING = "'(?:[^'\\\\]*(?:\\\\.)?)*'";
|
||||
|
||||
/**
|
||||
* The value types that Env supports
|
||||
*
|
||||
* @var array $value_types
|
||||
*/
|
||||
private static $value_types = array(
|
||||
'string',
|
||||
'bool',
|
||||
'number',
|
||||
'null',
|
||||
);
|
||||
|
||||
/**
|
||||
* The map to convert escaped characters into real characters
|
||||
*
|
||||
* @var array $character_map
|
||||
*/
|
||||
private static $character_map = array(
|
||||
"\\n" => "\n",
|
||||
"\\\"" => "\"",
|
||||
'\\\'' => "'",
|
||||
'\\t' => "\t"
|
||||
);
|
||||
|
||||
/**
|
||||
* The parser for variables
|
||||
*
|
||||
* @var \M1\Env\Parser\VariableParser $variable_parser
|
||||
*/
|
||||
private $variable_parser;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param \M1\Env\Parser $parser The parent parser
|
||||
* @param array $context Variables context
|
||||
*/
|
||||
public function __construct($parser, array $context = array())
|
||||
{
|
||||
parent::__construct($parser);
|
||||
|
||||
$this->variable_parser = new VariableParser($parser, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env value
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return string|null The parsed key, or null if the key is a comment
|
||||
*/
|
||||
public function parse($value)
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if ($this->parser->string_helper->startsWith('#', $value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->parseValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env value
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return string|null The parsed value, or null if the value is null
|
||||
*
|
||||
* @uses parseString
|
||||
* @uses parseNull
|
||||
* @uses parseBool
|
||||
* @uses parseNumber
|
||||
*/
|
||||
private function parseValue($value)
|
||||
{
|
||||
foreach (self::$value_types as $type) {
|
||||
$parsed_value = $value;
|
||||
|
||||
if ($type !== 'string') {
|
||||
$parsed_value = $this->parser->string_helper->stripComments($value);
|
||||
}
|
||||
|
||||
list($is_function, $parse_function) = $this->fetchFunctionNames($type);
|
||||
|
||||
if ($this->parser->string_helper->$is_function($parsed_value)) {
|
||||
return $this->$parse_function($parsed_value);
|
||||
}
|
||||
}
|
||||
|
||||
return (isset($parsed_value)) ? $this->parseUnquotedString($parsed_value) : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the functions for the value type
|
||||
*
|
||||
* @param string $type The value type
|
||||
*
|
||||
* @return string[] The is and parse function names
|
||||
*/
|
||||
private function fetchFunctionNames($type)
|
||||
{
|
||||
$type = ucfirst($type);
|
||||
|
||||
return array(
|
||||
'is'.$type,
|
||||
'parse'.$type
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env string
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return string The parsed string
|
||||
*/
|
||||
private function parseString($value)
|
||||
{
|
||||
$single = false;
|
||||
$regex = self::REGEX_QUOTE_DOUBLE_STRING;
|
||||
$symbol = '"';
|
||||
|
||||
if ($this->parser->string_helper->startsWith('\'', $value)) {
|
||||
$single = true;
|
||||
$regex = self::REGEX_QUOTE_SINGLE_STRING;
|
||||
$symbol = "'";
|
||||
}
|
||||
|
||||
$matches = $this->fetchStringMatches($value, $regex, $symbol);
|
||||
|
||||
$value = trim($matches[0], $symbol);
|
||||
$value = strtr($value, self::$character_map);
|
||||
|
||||
return ($single) ? $value : $this->variable_parser->parse($value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the regex matches in the string
|
||||
*
|
||||
* @param string $regex The regex to use
|
||||
* @param string $value The value to parse
|
||||
* @param string $symbol The symbol we're parsing for
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If the string has a missing end quote
|
||||
*
|
||||
* @return string[] The matches based on the regex and the value
|
||||
*/
|
||||
private function fetchStringMatches($value, $regex, $symbol)
|
||||
{
|
||||
if (!preg_match('/'.$regex.'/', $value, $matches)) {
|
||||
throw new ParseException(
|
||||
sprintf('Missing end %s quote', $symbol),
|
||||
$value,
|
||||
$this->parser->line_num
|
||||
);
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env null value
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return null Null value
|
||||
*/
|
||||
private function parseNull($value)
|
||||
{
|
||||
return (is_null($value) || $value === "null") ? null : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env unquoted string
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return string The parsed string
|
||||
*/
|
||||
private function parseUnquotedString($value)
|
||||
{
|
||||
if ($value === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->variable_parser->parse($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env bool
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return bool The parsed bool
|
||||
*/
|
||||
private function parseBool($value)
|
||||
{
|
||||
$value = strtolower($value);
|
||||
|
||||
return $value === "true" || $value === "yes";
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env number
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return int|float The parsed bool
|
||||
*/
|
||||
private function parseNumber($value)
|
||||
{
|
||||
if (strpos($value, '.') !== false) {
|
||||
return (float) $value;
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
+358
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the m1\env library
|
||||
*
|
||||
* (c) m1 <hello@milescroxford.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @package m1/env
|
||||
* @version 2.0.0
|
||||
* @author Miles Croxford <hello@milescroxford.com>
|
||||
* @copyright Copyright (c) Miles Croxford <hello@milescroxford.com>
|
||||
* @license http://github.com/m1/env/blob/master/LICENSE.md
|
||||
* @link http://github.com/m1/env/blob/master/README.md Documentation
|
||||
*/
|
||||
|
||||
namespace M1\Env\Parser;
|
||||
|
||||
use M1\Env\Exception\ParseException;
|
||||
|
||||
/**
|
||||
* The value parser for Env
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
class VariableParser extends AbstractParser
|
||||
{
|
||||
/**
|
||||
* The regex to get variables '$(VARIABLE)' in .env
|
||||
* Unescaped: ${(.*?)}
|
||||
*
|
||||
* @var string REGEX_ENV_VARIABLE
|
||||
*/
|
||||
const REGEX_ENV_VARIABLE = '\\${(.*?)}';
|
||||
|
||||
/**
|
||||
* The symbol for the assign default value parameter expansion
|
||||
*
|
||||
* @var string SYMBOL_ASSIGN_DEFAULT_VALUE
|
||||
*/
|
||||
const SYMBOL_ASSIGN_DEFAULT_VALUE = '=';
|
||||
|
||||
/**
|
||||
* The symbol for the default value parameter expansion
|
||||
*
|
||||
* @var string SYMBOL_DEFAULT_VALUE
|
||||
*/
|
||||
const SYMBOL_DEFAULT_VALUE = '-';
|
||||
|
||||
/**
|
||||
* Variables context
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param \M1\Env\Parser $parser The parent parser
|
||||
* @param array $context Variables context
|
||||
*/
|
||||
public function __construct($parser, array $context = array())
|
||||
{
|
||||
parent::__construct($parser);
|
||||
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env variable
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
* @param bool $quoted_string Is the value in a quoted string
|
||||
*
|
||||
* @return string The parsed value
|
||||
*/
|
||||
public function parse($value, $quoted_string = false)
|
||||
{
|
||||
$matches = $this->fetchVariableMatches($value);
|
||||
|
||||
if (is_array($matches)) {
|
||||
if ($this->parser->string_helper->isVariableClone($value, $matches, $quoted_string)) {
|
||||
return $this->fetchVariable($value, $matches[1][0], $matches, $quoted_string);
|
||||
}
|
||||
|
||||
$value = $this->doReplacements($value, $matches, $quoted_string);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get variable matches inside a string
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
*
|
||||
* @return array The variable matches
|
||||
*/
|
||||
private function fetchVariableMatches($value)
|
||||
{
|
||||
preg_match_all('/' . self::REGEX_ENV_VARIABLE . '/', $value, $matches);
|
||||
|
||||
if (!is_array($matches) || !isset($matches[0]) || empty($matches[0])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a .env variable
|
||||
*
|
||||
* @param string $value The value to parse
|
||||
* @param string $variable_name The variable name to get
|
||||
* @param array $matches The matches of the variables
|
||||
* @param bool $quoted_string Is the value in a quoted string
|
||||
*
|
||||
* @return string The parsed value
|
||||
* @throws \M1\Env\Exception\ParseException If the variable can not be found
|
||||
*/
|
||||
private function fetchVariable($value, $variable_name, $matches, $quoted_string)
|
||||
{
|
||||
if ($this->hasParameterExpansion($variable_name)) {
|
||||
$replacement = $this->fetchParameterExpansion($variable_name);
|
||||
} elseif ($this->hasVariable($variable_name)) {
|
||||
$replacement = $this->getVariable($variable_name);
|
||||
} else {
|
||||
throw new ParseException(
|
||||
sprintf('Variable has not been defined: %s', $variable_name),
|
||||
$value,
|
||||
$this->parser->line_num
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->parser->string_helper->isBoolInString($replacement, $quoted_string, count($matches[0]))) {
|
||||
$replacement = ($replacement) ? 'true' : 'false';
|
||||
}
|
||||
|
||||
return $replacement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the variable has a parameter expansion
|
||||
*
|
||||
* @param string $variable The variable to check
|
||||
*
|
||||
* @return bool Does the variable have a parameter expansion
|
||||
*/
|
||||
private function hasParameterExpansion($variable)
|
||||
{
|
||||
if ((strpos($variable, self::SYMBOL_DEFAULT_VALUE) !== false) ||
|
||||
(strpos($variable, self::SYMBOL_ASSIGN_DEFAULT_VALUE) !== false)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches and sets the parameter expansion
|
||||
*
|
||||
* @param string $variable_name The parameter expansion inside this to fetch
|
||||
*
|
||||
* @return string The parsed value
|
||||
*/
|
||||
private function fetchParameterExpansion($variable_name)
|
||||
{
|
||||
$parameter_type = $this->fetchParameterExpansionType($variable_name);
|
||||
|
||||
list($parameter_symbol, $empty_flag) = $this->fetchParameterExpansionSymbol($variable_name, $parameter_type);
|
||||
list($variable, $default) = $this->splitVariableDefault($variable_name, $parameter_symbol);
|
||||
|
||||
$value = $this->getVariable($variable);
|
||||
|
||||
return $this->parseVariableParameter(
|
||||
$variable,
|
||||
$default,
|
||||
$this->hasVariable($variable),
|
||||
$empty_flag && empty($value),
|
||||
$parameter_type
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the parameter expansion type
|
||||
*
|
||||
* @param string $variable_name The parameter expansion type to get from this
|
||||
*
|
||||
* @return string The parameter expansion type
|
||||
*/
|
||||
private function fetchParameterExpansionType($variable_name)
|
||||
{
|
||||
if (strpos($variable_name, self::SYMBOL_ASSIGN_DEFAULT_VALUE) !== false) {
|
||||
return 'assign_default_value';
|
||||
}
|
||||
|
||||
return 'default_value'; // self::DEFAULT_VALUE_SYMBOL
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the parameter type symbol
|
||||
*
|
||||
* @param string $variable_name The variable
|
||||
* @param string $type The type of parameter expansion
|
||||
*
|
||||
* @return array The symbol and if there is a empty check
|
||||
*/
|
||||
private function fetchParameterExpansionSymbol($variable_name, $type)
|
||||
{
|
||||
$class = new \ReflectionClass($this);
|
||||
$symbol = $class->getConstant('SYMBOL_'.strtoupper($type));
|
||||
$pos = strpos($variable_name, $symbol);
|
||||
|
||||
$check_empty = substr($variable_name, ($pos - 1), 1) === ":";
|
||||
|
||||
if ($check_empty) {
|
||||
$symbol = sprintf(":%s", $symbol);
|
||||
}
|
||||
|
||||
return array($symbol, $check_empty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the parameter expansion into variable and default
|
||||
*
|
||||
* @param string $variable_name The variable name to split
|
||||
* @param string $parameter_symbol The parameter expansion symbol
|
||||
*
|
||||
* @throws \M1\Env\Exception\ParseException If the parameter expansion if not valid syntax
|
||||
*
|
||||
* @return array The split variable and default value
|
||||
*/
|
||||
private function splitVariableDefault($variable_name, $parameter_symbol)
|
||||
{
|
||||
$variable_default = explode($parameter_symbol, $variable_name, 2);
|
||||
|
||||
if (count($variable_default) !== 2 || empty($variable_default[1])) {
|
||||
throw new ParseException(
|
||||
'You must have valid parameter expansion syntax, eg. ${parameter:=word}',
|
||||
$variable_name,
|
||||
$this->parser->line_num
|
||||
);
|
||||
}
|
||||
|
||||
return array(trim($variable_default[0]), trim($variable_default[1]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses and sets the variable and default if needed
|
||||
*
|
||||
* @param string $variable The variable to parse
|
||||
* @param string $default The default value
|
||||
* @param bool $exists Does the variable exist
|
||||
* @param bool $empty Is there the variable empty if exists and the empty flag is set
|
||||
* @param string $type The type of parameter expansion
|
||||
*
|
||||
* @return string The parsed value
|
||||
*/
|
||||
private function parseVariableParameter($variable, $default, $exists, $empty, $type)
|
||||
{
|
||||
if ($exists && !$empty) {
|
||||
return $this->getVariable($variable);
|
||||
}
|
||||
|
||||
return $this->assignVariableParameterDefault($variable, $default, $empty, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and sets the variable parameter to default
|
||||
*
|
||||
* @param string $variable The variable to parse
|
||||
* @param string $default The default value
|
||||
* @param bool $empty Is there the variable empty if exists and the empty flag is set
|
||||
* @param string $type The type of parameter expansion
|
||||
*
|
||||
* @return string The parsed default value
|
||||
*/
|
||||
private function assignVariableParameterDefault($variable, $default, $empty, $type)
|
||||
{
|
||||
$default = $this->parser->value_parser->parse($default);
|
||||
|
||||
if ($type === "assign_default_value" && $empty) {
|
||||
$this->parser->lines[$variable] = $default;
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a variable exists
|
||||
*
|
||||
* @param string $variable The variable name to get
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function hasVariable($variable)
|
||||
{
|
||||
if (array_key_exists($variable, $this->parser->lines)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (array_key_exists($variable, $this->context)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get variable value
|
||||
*
|
||||
* @param string $variable
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getVariable($variable)
|
||||
{
|
||||
if (array_key_exists($variable, $this->parser->lines)) {
|
||||
return $this->parser->lines[$variable];
|
||||
}
|
||||
|
||||
if (array_key_exists($variable, $this->context)) {
|
||||
return $this->context[$variable];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the variable replacements
|
||||
*
|
||||
* @param string $value The value to throw an error with if doesn't exist
|
||||
* @param array $matches The matches of the variables
|
||||
* @param bool $quoted_string Is the value in a quoted string
|
||||
*
|
||||
* @return string The parsed value
|
||||
*/
|
||||
public function doReplacements($value, $matches, $quoted_string)
|
||||
{
|
||||
$replacements = array();
|
||||
|
||||
for ($i = 0; $i <= (count($matches[0]) - 1); $i++) {
|
||||
$replacement = $this->fetchVariable($value, $matches[1][$i], $matches, $quoted_string);
|
||||
$replacements[$matches[0][$i]] = $replacement;
|
||||
}
|
||||
|
||||
if (!empty($replacements)) {
|
||||
$value = strtr($value, $replacements);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user