Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
Speeding things up
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusan Malusev committed Jul 12, 2019
1 parent 51a9337 commit b0dd6a6
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 154 deletions.
7 changes: 7 additions & 0 deletions .idea/dictionaries/dusan.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 93 additions & 94 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":616:{a:2:{s:7:"defects";a:4:{s:49:"Dusan\DotEnv\Tests\EnvParserTest::test_env_parser";i:3;s:53:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_error";i:4;s:76:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_space_error_in_variable_name";i:4;s:52:"Dusan\DotEnv\Tests\EnvParserTest::test_interpolation";i:3;}s:5:"times";a:4:{s:49:"Dusan\DotEnv\Tests\EnvParserTest::test_env_parser";d:0.015;s:53:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_error";d:0.005;s:76:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_space_error_in_variable_name";d:0.002;s:52:"Dusan\DotEnv\Tests\EnvParserTest::test_interpolation";d:0.022;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":616:{a:2:{s:7:"defects";a:4:{s:49:"Dusan\DotEnv\Tests\EnvParserTest::test_env_parser";i:3;s:53:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_error";i:4;s:76:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_space_error_in_variable_name";i:4;s:52:"Dusan\DotEnv\Tests\EnvParserTest::test_interpolation";i:3;}s:5:"times";a:4:{s:49:"Dusan\DotEnv\Tests\EnvParserTest::test_env_parser";d:0.026;s:53:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_error";d:0.003;s:76:"Dusan\DotEnv\Tests\EnvParserTest::test_env_with_space_error_in_variable_name";d:0.002;s:52:"Dusan\DotEnv\Tests\EnvParserTest::test_interpolation";d:0.003;}}}
83 changes: 42 additions & 41 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
{
"name": "dusan/dotenv",
"description": "Env Parser for PhpMvc Framework",
"authors": [
{
"name": "Dusan Malusev",
"email": "[email protected]"
}
"name": "dusan/dotenv",
"description": "Env Parser for PhpMvc Framework",
"authors": [
{
"name": "Dusan Malusev",
"email": "[email protected]"
}
],
"autoload": {
"files": [
"src/functions.php"
],
"autoload": {
"files": [
"src/functions.php"
],
"psr-4": {
"Dusan\\DotEnv\\": "src/"
}
},
"autoload-dev": {
"files": [
"src/functions.php"
],
"psr-4": {
"Dusan\\DotEnv\\Tests\\": "tests/"
}
},
"type": "library",
"minimum-stability": "stable",
"require": {
"dusan/php-mvc-file": "1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.1",
"codedungeon/phpunit-result-printer": "^0.26.2"
},
"license": "GPL-2.0",
"repositories": [
{
"type": "git",
"url": "https://github.com/malusev998/PhpMvc-File.git",
"branch": "master"
}
]
}
"psr-4": {
"Dusan\\DotEnv\\": "src/"
}
},
"autoload-dev": {
"files": [
"src/functions.php"
],
"psr-4": {
"Dusan\\DotEnv\\Tests\\": "tests/"
}
},
"type": "library",
"minimum-stability": "stable",
"require": {
"dusan/php-mvc-file": "1.0",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.1",
"codedungeon/phpunit-result-printer": "^0.26.2"
},
"license": "GPL-2.0",
"repositories": [
{
"type": "git",
"url": "https://github.com/malusev998/PhpMvc-File.git",
"branch": "master"
}
]
}
56 changes: 39 additions & 17 deletions src/EnvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(string $file)


/**
* @inheritDoc
* {@inheritDoc}
*
* @param bool $raw
*
Expand All @@ -72,11 +72,13 @@ public function parse(bool $raw = false): void
$this->handler->sharedLock();

$envs = [];

while (($c = $this->handler->fgetc()) !== false) {
$column = 0;
// Handling Comments, Empty lines and leading spaces
if ($c === self::COMMENT) while (($c = $this->handler->fgetc()) !== self::NEW_LINE) continue;
if ($c === self::NEW_LINE || $c === self::CARRIAGE_RETURN || $c === self::SPACE) continue;
$envs[$this->extractName($c)] = $this->extractValue($envs, $raw);
$envs[$this->extractName($c, $column)] = $this->extractValue($envs, $raw, $column);
}

$this->envs = $envs;
Expand All @@ -86,11 +88,12 @@ public function parse(bool $raw = false): void

/**
* @param string $startingChar
* @param int $column
*
* @return string
* @throws \Dusan\DotEnv\Exceptions\DotEnvSyntaxError
*/
private function extractName(string $startingChar): string
private function extractName(string $startingChar, int & $column): string
{
$key = $startingChar;
while (($c = $this->handler->fgetc()) !== self::EQUALS) {
Expand All @@ -101,28 +104,42 @@ private function extractName(string $startingChar): string
}
if ($c === self::EQUALS) break;
else {
throw new DotEnvSyntaxError('Spaces are now allowed in env variable name, LINE = ' . $this->handler->key());
$error = new DotEnvSyntaxError('Spaces are now allowed in env variable name');
$error->setEnvLine($this->handler->key());
$error->setColumn($column);
throw $error;
}
};
if ($c === self::CARRIAGE_RETURN || $c === self::NEW_LINE || $c === self::COMMENT)
throw new DotEnvSyntaxError('Error on line ' . $this->handler->key());
}
if ($c === self::CARRIAGE_RETURN || $c === self::NEW_LINE || $c === self::COMMENT) {
$error = new DotEnvSyntaxError('Unexpected end of line');
$error->setEnvLine($this->handler->key());
$error->setColumn($column);
throw $error;
}
$key .= $c;
$column++;
}
return $key;
}

/**
* Parses the individual value from the .env file
*
* @param array $envs
* @param bool $raw
* @param int $column
*
* @return string
* @throws \Dusan\DotEnv\Exceptions\EnvVariableNotFound
*/
private function extractValue(array $envs, bool $raw): string
private function extractValue(array $envs, bool $raw, int & $column): string
{
$value = '';
// Trimming the leading spaces of the value
while (($c = $this->handler->fgetc()) === self::SPACE) continue;
while (($c = $this->handler->fgetc()) === self::SPACE) {
$column++;
continue;
};
$this->handler->fseek($this->handler->ftell() - 1);

// Handling Multiline values
Expand All @@ -135,21 +152,26 @@ private function extractValue(array $envs, bool $raw): string
} else {
$value .= $c;
}
$column++;
}
return $value;
}
// Single line
// Handling Single line values
while (($c = $this->handler->fgetc()) !== false) {
if($c === self::CARRIAGE_RETURN) break;
if($c === self::NEW_LINE) break;
if ($c === self::CARRIAGE_RETURN) break;
if ($c === self::NEW_LINE) break;
// Every space character will be ignored
if ($c === self::SPACE) continue;
if ($c === self::SPACE) break;
// If comment is found at the end of value it will be ignored
if ($c === self::COMMENT)
// Just moving the file pointer to the \r or \n
while (($c = $this->handler->fgetc()) !== false && $c !== self::NEW_LINE) continue;
// Appending the read value to the temporary handler
else $value .= $c;
// Just moving the file pointer to the or \n
while (($c = $this->handler->fgetc()) !== false && $c !== self::NEW_LINE) {
$column++;
continue;
}
else
$value .= $c;
$column++;
}
return $value;
}
Expand Down
47 changes: 47 additions & 0 deletions src/Exceptions/DotEnvSyntaxError.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,55 @@

class DotEnvSyntaxError extends Exception
{
/** @var integer */
private $envLine;

/** @var integer */
private $column;

public function __construct($message = "Check your syntax in .env file", $code = 0, Throwable $previous = NULL)
{
parent::__construct($message, $code, $previous);
}

/**
* @return int
*/
public function getEnvLine(): int
{
return $this->envLine;
}

/**
* @param int $envLine
*
* @return DotEnvSyntaxError
*/
public function setEnvLine(int $envLine): DotEnvSyntaxError
{
$this->envLine = $envLine;
return $this;
}


/**
* @return int
*/
public function getColumn(): int
{
return $this->column;
}

/**
* @param int $column
*
* @return DotEnvSyntaxError
*/
public function setColumn(int $column): DotEnvSyntaxError
{
$this->column = $column;
return $this;
}


}
2 changes: 2 additions & 0 deletions tests/EnvTest/.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ MULTI_LINE = " this
"

COMMENTS_AT_END_OF_VALUE = Name #here is some comment

NEW_VALUE = test
2 changes: 1 addition & 1 deletion tests/EnvTest/EnvParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_env_parser()
is multi line
', $array['MULTI_LINE']);
} catch (DotEnvSyntaxError $e) {
$this->fail($e->getMessage());
$this->fail(sprintf("%s %d %d", $e->getMessage(), $e->getEnvLine(), $e->getColumn()));
} catch (Exception $e) {
$this->fail($e->getMessage());
}
Expand Down

0 comments on commit b0dd6a6

Please sign in to comment.