Compare commits
11 Commits
ac7d6d9379
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce4cfcdb7a | ||
|
|
b6f048d81c | ||
|
|
7e79eca195 | ||
|
|
67d6be02c5 | ||
|
|
a9e45feb7f | ||
|
|
858a85954d | ||
|
|
aeec3e36d1 | ||
|
|
232fa35e6d | ||
|
|
85ecae5365 | ||
|
|
3702d526bd | ||
|
|
2664f6261c |
6
.env.example
Normal file
6
.env.example
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# php-bsrutils environment variables
|
||||||
|
# This library only needs logging configuration if you override the defaults.
|
||||||
|
# Most applications define their own logging config, making this optional.
|
||||||
|
|
||||||
|
# Optional: Override default log file location
|
||||||
|
# LOG_FILE=/var/log/app/log.txt
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
vendor
|
vendor/
|
||||||
*.swp
|
*.swp
|
||||||
output
|
output
|
||||||
composer.lock
|
composer.lock
|
||||||
|
.env
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "Guillermo Pages",
|
"name" : "Guillermo Pages",
|
||||||
"email" : "g@lespagesweb.ch"
|
"email" : "g@meow.ch"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@@ -18,6 +18,6 @@
|
|||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"pds/skeleton": "^1.0",
|
"pds/skeleton": "^1.0",
|
||||||
"phpunit/phpunit": "^6"
|
"phpunit/phpunit": "^8.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1019
composer.lock
generated
1019
composer.lock
generated
File diff suppressed because it is too large
Load Diff
15
config/configuration.local.php
Normal file
15
config/configuration.local.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
use Bsr\Utils\Logger\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default configuration for php-bsrutils library.
|
||||||
|
* Contains only logger defaults needed for the Logger class.
|
||||||
|
* Application-specific config should be in the consuming application's config files.
|
||||||
|
*/
|
||||||
|
return array(
|
||||||
|
'log' => array(
|
||||||
|
'file' => realpath(dirname(__FILE__) . '/..') . '/log/log.txt',
|
||||||
|
'format' => '%ip% - [%date%] - %status% %error% - %time% - %func%',
|
||||||
|
'verbosity' => Logger::NORMAL,
|
||||||
|
),
|
||||||
|
);
|
||||||
13
config/configuration.logger.normal.php
Normal file
13
config/configuration.logger.normal.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
use Bsr\Utils\Logger\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger configuration - normal verbosity
|
||||||
|
*/
|
||||||
|
return array(
|
||||||
|
'log' => array(
|
||||||
|
'file' => realpath(dirname(__FILE__) . '/..') . '/log/log.txt',
|
||||||
|
'format' => '%ip% - [%date%] - %status% %error% - %time% - %func%',
|
||||||
|
'verbosity' => Logger::NORMAL,
|
||||||
|
),
|
||||||
|
);
|
||||||
13
config/configuration.logger.quiet.php
Normal file
13
config/configuration.logger.quiet.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
use Bsr\Utils\Logger\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger configuration - quiet verbosity
|
||||||
|
*/
|
||||||
|
return array(
|
||||||
|
'log' => array(
|
||||||
|
'file' => realpath(dirname(__FILE__) . '/..') . '/log/log.txt',
|
||||||
|
'format' => '%ip% - [%date%] - %status% %error% - %time% - %func%',
|
||||||
|
'verbosity' => Logger::QUIET,
|
||||||
|
),
|
||||||
|
);
|
||||||
13
config/configuration.logger.verbose.php
Normal file
13
config/configuration.logger.verbose.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
use Bsr\Utils\Logger\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger configuration - verbose verbosity
|
||||||
|
*/
|
||||||
|
return array(
|
||||||
|
'log' => array(
|
||||||
|
'file' => realpath(dirname(__FILE__) . '/..') . '/log/log.txt',
|
||||||
|
'format' => '%ip% - [%date%] - %status% %error% - %time% - %func%',
|
||||||
|
'verbosity' => Logger::VERBOSE,
|
||||||
|
),
|
||||||
|
);
|
||||||
@@ -75,6 +75,8 @@ class Configuration {
|
|||||||
if (null === self::$customConfigFilePath) {
|
if (null === self::$customConfigFilePath) {
|
||||||
$envSpecificConfig = 'local';
|
$envSpecificConfig = 'local';
|
||||||
if ($env = getenv('APPLICATION_ENV')) {
|
if ($env = getenv('APPLICATION_ENV')) {
|
||||||
|
} else if (isset($_ENV['APPLICATION_ENV'])) {
|
||||||
|
$env = $_ENV['APPLICATION_ENV'];
|
||||||
} else if (isset($_SERVER['APPLICATION_ENV'])) {
|
} else if (isset($_SERVER['APPLICATION_ENV'])) {
|
||||||
$env = $_SERVER['APPLICATION_ENV'];
|
$env = $_SERVER['APPLICATION_ENV'];
|
||||||
} else if (WhichEnv::has('APPLICATION_ENV')) {
|
} else if (WhichEnv::has('APPLICATION_ENV')) {
|
||||||
@@ -155,7 +157,7 @@ class Configuration {
|
|||||||
{
|
{
|
||||||
foreach ($keys as $k) {
|
foreach ($keys as $k) {
|
||||||
if (!is_array($data)) {
|
if (!is_array($data)) {
|
||||||
throw new \Exception("Try to access non-array as array, key '$key''");
|
throw new \Exception("Try to access non-array as array, key '$k''");
|
||||||
}
|
}
|
||||||
if (!isset($data[$k])) {
|
if (!isset($data[$k])) {
|
||||||
return $default;
|
return $default;
|
||||||
|
|||||||
39
src/Utils/DotEnv.php
Normal file
39
src/Utils/DotEnv.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
namespace Bsr\Utils;
|
||||||
|
|
||||||
|
class DotEnv {
|
||||||
|
|
||||||
|
public static function loadFromFile($filepath) {
|
||||||
|
if (!file_exists($filepath)) {
|
||||||
|
throw new \Exception("Cannot find file: $filepath");
|
||||||
|
}
|
||||||
|
$contents = file_get_contents($filepath);
|
||||||
|
$lines = explode(PHP_EOL, $contents);
|
||||||
|
$envvars = array_reduce($lines, function ($vars, $line) {
|
||||||
|
$parts = explode('=', $line);
|
||||||
|
$partsCount = count($parts);
|
||||||
|
if ($partsCount === 1 && strlen($parts[0]) <= 0) {
|
||||||
|
return $vars;
|
||||||
|
}
|
||||||
|
[$varname, $value] = $partsCount === 1
|
||||||
|
? [$parts[0], '']
|
||||||
|
: ($partsCount > 2
|
||||||
|
? [$parts[0], implode('=', array_slice($parts, 1))]
|
||||||
|
: $parts
|
||||||
|
);
|
||||||
|
$vars[$varname] = $value;
|
||||||
|
return $vars;
|
||||||
|
}, []);
|
||||||
|
return $envvars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function MUTATE_SERVER_addEnvVars(array $variables) {
|
||||||
|
$_SERVER = array_merge($_SERVER, $variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function load(string $rootDir) {
|
||||||
|
$envvars = self::loadFromFile($rootDir . DIRECTORY_SEPARATOR . '.env');
|
||||||
|
self::MUTATE_SERVER_addEnvVars($envvars);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
4
src/Utils/FileSystem/Exception/BadEnvException.php
Normal file
4
src/Utils/FileSystem/Exception/BadEnvException.php
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
namespace Bsr\Utils\FileSystem\Exception;
|
||||||
|
|
||||||
|
class BadEnvException extends \Exception {}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Bsr\Utils\FileSystem;
|
namespace Bsr\Utils\FileSystem;
|
||||||
|
|
||||||
|
use Bsr\Utils\FileSystem\Exception\BadEnvException;
|
||||||
|
|
||||||
class FileSystem
|
class FileSystem
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -21,10 +23,11 @@ class FileSystem
|
|||||||
$buildPath = '';
|
$buildPath = '';
|
||||||
for ($i=0; $i<$partsCount; $i++) {
|
for ($i=0; $i<$partsCount; $i++) {
|
||||||
$part = $parts[$i];
|
$part = $parts[$i];
|
||||||
|
if ($part === '') continue;
|
||||||
if (!file_exists($buildPath . "/$part")) {
|
if (!file_exists($buildPath . "/$part")) {
|
||||||
if (!is_writable($buildPath)) {
|
if (!is_writable($buildPath)) {
|
||||||
if ($throw) {
|
if ($throw) {
|
||||||
throw new Exception("Attempting to create : $path, but $buildPath is not writeable.");
|
throw new BadEnvException("Attempting to create : $path, but $buildPath is not writeable.");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,11 @@ class Logger {
|
|||||||
$ip = '(n-a)';
|
$ip = '(n-a)';
|
||||||
|
|
||||||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||||
$ip = array_shift(
|
$arr = array_map(
|
||||||
array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))
|
'trim',
|
||||||
|
explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])
|
||||||
);
|
);
|
||||||
|
$ip = array_shift($arr);
|
||||||
} else if (isset($_SERVER['REMOTE_ADDR'])) {
|
} else if (isset($_SERVER['REMOTE_ADDR'])) {
|
||||||
$ip = $_SERVER['REMOTE_ADDR'];
|
$ip = $_SERVER['REMOTE_ADDR'];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ namespace Bsr\Utils\WhichEnv;
|
|||||||
* based on a cutom rule
|
* based on a cutom rule
|
||||||
*/
|
*/
|
||||||
class WhichEnv {
|
class WhichEnv {
|
||||||
private $rule;
|
|
||||||
|
static private $rule;
|
||||||
|
|
||||||
static public function registerRule(WhichEnvRuleInterface $rule)
|
static public function registerRule(WhichEnvRuleInterface $rule)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user