Compare commits

...

10 Commits

Author SHA1 Message Date
Guillermo Pages
b6f048d81c refactor: remove hardcoded credentials from config
- Strip configuration.local.php to logger-only defaults
- Remove db, solr, session, checkfile_url, worker IDs (unused legacy)
- Clean credential comments from logger config files
- Add .env.example template
- Add .env to .gitignore
2026-01-14 14:15:02 +01:00
Guillermo Pages
7e79eca195 build: change package name to meow/php-bsrutils 2023-08-23 10:21:39 +02:00
Guillermo Pages
67d6be02c5 fix: only variables should be passed by reference 2022-10-09 20:01:55 +02:00
Guillermo Pages
a9e45feb7f feat: support variables 2022-09-11 16:17:00 +02:00
Guillermo Pages
858a85954d feat: add better exception message 2022-07-01 11:20:12 +02:00
Guillermo Pages
aeec3e36d1 fix: too many root slashes 2021-09-13 16:34:00 +02:00
Guillermo Pages
232fa35e6d fix: File Exception.php not found 2021-09-13 16:20:58 +02:00
Guillermo Pages
85ecae5365 fix: typo 2021-08-30 17:53:18 +02:00
Guillermo Pages
3702d526bd feat: add DotEnv class 2021-08-30 17:52:45 +02:00
Guillermo Dev
2664f6261c forgot static 2018-12-04 14:05:27 +01:00
15 changed files with 625 additions and 556 deletions

6
.env.example Normal file
View 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
View File

@@ -1,4 +1,5 @@
vendor
vendor/
*.swp
output
composer.lock
.env

View File

@@ -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"
}
}

1019
composer.lock generated

File diff suppressed because it is too large Load Diff

View 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,
),
);

View 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,
),
);

View 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,
),
);

View 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,
),
);

View File

@@ -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')) {
@@ -155,7 +157,7 @@ class Configuration {
{
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
View 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);
}
}

View File

@@ -0,0 +1,4 @@
<?php
namespace Bsr\Utils\FileSystem\Exception;
class BadEnvException extends \Exception {}

View File

@@ -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;
}

View File

@@ -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'];
}

View File

@@ -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)
{