blob: 36410fc25c3084aa1ac05b931def706b1785946d (
plain) (
tree)
|
|
<?php
declare(strict_types=1);
/**
* @author: Alexander Kiryukhin <alexander@kiryukhin.su>
* @license: MIT
*/
namespace NeonXP\Dotenv\Parser;
/**
* Class Parser
* @package NeonXP\Dotenv\Parser
*/
class Parser implements ParserInterface
{
const REGEX_EXPORT_PREFIX = '/^\s*export\s/i';
// Quotes and comments
const SINGLE_QUOTED_LINE_WITH_COMMENT = '/^\'(.*?)\'\s+#.*?$/i';
const DOUBLE_QUOTED_LINE_WITH_COMMENT = '/^\"(.+?)\"\s+#.*?$/i';
const SINGLE_QUOTED_LINE = '/^\'(.+?)\'$/i';
const DOUBLE_QUOTED_LINE = '/^\"(.*?)\"$/i';
// Types
const BOOLEAN = '/^(true|false)$/i';
const NUMBER = '/^(\d+)$/';
public function parseLine(string $line): array
{
$line = preg_replace(self::REGEX_EXPORT_PREFIX, '', $line);
list($key, $value) = explode('=', $line, 2) + ['', ''];
$key = trim($key);
$value = trim($value);
$matches = [];
if (
preg_match(self::SINGLE_QUOTED_LINE_WITH_COMMENT, $value, $matches) ||
preg_match(self::DOUBLE_QUOTED_LINE_WITH_COMMENT, $value, $matches) ||
preg_match(self::SINGLE_QUOTED_LINE, $value, $matches) ||
preg_match(self::DOUBLE_QUOTED_LINE, $value, $matches)
) {
$value = $matches[1];
}
if (preg_match(self::BOOLEAN, $value)) {
$value = (strtolower($value) === 'true');
} elseif (preg_match(self::NUMBER, $value)) {
$value = intval($value);
}
return ['key' => $key, 'value' => $value];
}
}
|