Compare commits
10 Commits
ac7d6d9379
...
b6f048d81c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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
|
||||
output
|
||||
composer.lock
|
||||
.env
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name" : "bsr/utils",
|
||||
"name" : "meow/php-bsrutils",
|
||||
"description" : "Bsr configuration module",
|
||||
"authors" : [
|
||||
{
|
||||
@@ -7,7 +7,7 @@
|
||||
},
|
||||
{
|
||||
"name" : "Guillermo Pages",
|
||||
"email" : "g@lespagesweb.ch"
|
||||
"email" : "g@meow.ch"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
@@ -18,6 +18,6 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"pds/skeleton": "^1.0",
|
||||
"phpunit/phpunit": "^6"
|
||||
"phpunit/phpunit": "^8.2"
|
||||
}
|
||||
}
|
||||
|
||||
1023
composer.lock
generated
1023
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,
|
||||
),
|
||||
);
|
||||
@@ -31,9 +31,9 @@ class Configuration {
|
||||
private $values = array();
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private function __construct()
|
||||
private function __construct()
|
||||
{
|
||||
if (!isset($this->values['session'])) {
|
||||
$this->values['session'] = array();
|
||||
@@ -47,7 +47,7 @@ class Configuration {
|
||||
$this->loadConfigFromFile();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The path to the file including file name
|
||||
* @param string $filePath
|
||||
@@ -75,6 +75,8 @@ class Configuration {
|
||||
if (null === self::$customConfigFilePath) {
|
||||
$envSpecificConfig = 'local';
|
||||
if ($env = getenv('APPLICATION_ENV')) {
|
||||
} else if (isset($_ENV['APPLICATION_ENV'])) {
|
||||
$env = $_ENV['APPLICATION_ENV'];
|
||||
} else if (isset($_SERVER['APPLICATION_ENV'])) {
|
||||
$env = $_SERVER['APPLICATION_ENV'];
|
||||
} else if (WhichEnv::has('APPLICATION_ENV')) {
|
||||
@@ -131,8 +133,8 @@ class Configuration {
|
||||
* Checks whether the nested key exists
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $keys
|
||||
* @return boolean
|
||||
* @param array $keys
|
||||
* @return boolean
|
||||
*/
|
||||
public static function existsKeys(array $data, array $keys)
|
||||
{
|
||||
@@ -147,15 +149,15 @@ class Configuration {
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $keys
|
||||
* @param array $keys
|
||||
* @param any $default
|
||||
* @return boolean
|
||||
* @return boolean
|
||||
*/
|
||||
public static function fetchRecursive(array $data, array $keys, $default = null)
|
||||
{
|
||||
foreach ($keys as $k) {
|
||||
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])) {
|
||||
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
|
||||
namespace Bsr\Utils\FileSystem;
|
||||
|
||||
use Bsr\Utils\FileSystem\Exception\BadEnvException;
|
||||
|
||||
class FileSystem
|
||||
{
|
||||
/**
|
||||
@@ -21,10 +23,11 @@ class FileSystem
|
||||
$buildPath = '';
|
||||
for ($i=0; $i<$partsCount; $i++) {
|
||||
$part = $parts[$i];
|
||||
if ($part === '') continue;
|
||||
if (!file_exists($buildPath . "/$part")) {
|
||||
if (!is_writable($buildPath)) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -25,9 +25,11 @@ class Logger {
|
||||
$ip = '(n-a)';
|
||||
|
||||
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||
$ip = array_shift(
|
||||
array_map('trim', explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))
|
||||
$arr = array_map(
|
||||
'trim',
|
||||
explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])
|
||||
);
|
||||
$ip = array_shift($arr);
|
||||
} else if (isset($_SERVER['REMOTE_ADDR'])) {
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
}
|
||||
@@ -37,12 +39,12 @@ class Logger {
|
||||
|
||||
/**
|
||||
* Initialize the start property with microtime in float format
|
||||
* Initialize self::$data array with a union from argument $data
|
||||
* Initialize self::$data array with a union from argument $data
|
||||
* and some initial values array : ip, date and some default (none)
|
||||
*
|
||||
*
|
||||
* If $data param contains some of the keys in the right hand array:
|
||||
* 'ip', 'date', ..., 'error', then the $data will prevail
|
||||
*
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
@@ -80,9 +82,9 @@ class Logger {
|
||||
|
||||
/**
|
||||
* If $key is passed, creates/overwrites existing self::$data[$key] with $info
|
||||
* Otherwise, uses array_merge:
|
||||
* Otherwise, uses array_merge:
|
||||
* if $info has same string keys as self::$data, $info overwrites
|
||||
* if $info has same integer keys as self::$data, $info keys are renumbered
|
||||
* if $info has same integer keys as self::$data, $info keys are renumbered
|
||||
* and appended to self::$data
|
||||
*
|
||||
* @param any $info
|
||||
@@ -117,7 +119,7 @@ class Logger {
|
||||
|
||||
/**
|
||||
* Allow stopping by overriding some self::$data values with $data param
|
||||
* - store the time lapse between start() and stop() calls in self::$data
|
||||
* - store the time lapse between start() and stop() calls in self::$data
|
||||
*
|
||||
* @param $data allow storing some info on stop
|
||||
* @return string saved log message filepath
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace Bsr\Utils\WhichEnv;
|
||||
* based on a cutom rule
|
||||
*/
|
||||
class WhichEnv {
|
||||
private $rule;
|
||||
|
||||
static private $rule;
|
||||
|
||||
static public function registerRule(WhichEnvRuleInterface $rule)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
namespace Bsr\Utils\WhichEnv;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
interface WhichEnvRuleInterface {
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user