Download the PHP package pinepain/simple-config without Composer
On this page you can find all versions of the php package pinepain/simple-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pinepain/simple-config
More information about pinepain/simple-config
Files in pinepain/simple-config
Package simple-config
Short Description Lightweight and flexible library for configuration management
License MIT
Homepage https://github.com/pinepain/php-simple-config
Informations about the package simple-config
WARNING: This project is UNSUPPORTED and ABANDONED
Php Simple Config
About
This is yet another library to deal with Config values. It designed to be obvious, minimal, lightweight, fast and extensible.
This library combines approaches used by well-known and recognizable PHP (and not limited to) frameworks.
Features:
- obvious and simple
- dealing with config best practices
- default values for missed items support
- flexibility (yeah, someone may say that it is so flexible that you have to give it a support)
- minimal and extensible codebase
- designed to have lose coupled code (at least wannabe)
- speed (one of fastest from all what you've ever seen, marketing guys may add "close to bare metal", but that is not true as all we know)
Installation:
composer require pinepain/simple-config 0.*
Configs loading:
As this library tries to keep things simple, Config
class requires only array of config items, so you may have any
configs loading logic you want and your application needs.
Right out of the box there are FilesLoader
configs loade which simply require
's all files in single directory and
store returned items in config array under file's basename (file name without path to it and its extension) key.
Limitation:
- Nested configs loading (that one that reside in nested directories) are not supported.
- Dot-file (that one that starts with dot(
.
) character) and files that have different than.php
extension are ignored. - Files with dots in base name (name without path and extension) are not supported (see note below why).
Assume, we have such directory tree:
then loading configs from config
directory will load only app.php
, cache.php
and db.php
files and will ignore
.dotfile.php
and mail.php.old
files and will skip somedir/
content completely.
Note:
Files with dots in it names (not counting dot between basename and extension) will leads to undiscoverable config
section, e.g. config file db.old.php
will be interpreted as [ 'db.old' => ... ]
entry which can't be accessed via
dot notation while we assume that each dot means one nested level.
While having such config section is ambiguous such naming is not supported by this library.
Example:
Note: While this library is rather set of blocks it was built with mind and hope that you will use some IoC container, but you can do all that manually.
Optimizations and caching
As this library tries to keep things simple, Config
class requires only array of config items, so you may have any
configs loading logic you want and your application needs.
In addition to FilesLoader
loader there are FileCacheLoader
and MergedFilesLoader
loaders which designed to optimize
config loading time by storing all config items in one file to reduce file operations. The main difference between them
is that FileCacheLoader
loader store already calculated config items and MergedFilesLoader
combine config files content
as is into one file. If you are in hurry - see example below to see the difference between them.
Caching:
If you want to cache loaded config use FileCacheLoader
which requires any config loader that implements LoaderInterface
interface and file path to store cached config items to. It has very simple logic: it looks for cached file and if it
exists than retun itrequre
d result, if no cached file found, config items loaded with passed loader and then cached to
file and after that returned to user.
Optimization:
Out of the box we have FilesLoader
loader and FileCacheLoader
caching loader which perfectly works together in most cases.
But if you follow 12-factor app methodology and store config in the environment
and what more, relies on them in your config files, you have to clean cache on every env variable change. In case doing
some dynamic calculus in config files you can't use caching at all.
Here MergedFilesLoader
loader comes. It requires FilesLoader
and file where to store merged config files to.
The loader logic is to merge all config file into one under appropriate section so then only 1 file operation required,
but actual code in configs are still executed on require
.
MergedFilesLoader
is limited to config files parsing and built with hope that it will not be used in situations where
configs used for programming, so it supposed that you have simple config with return
statement that return config items
on require
.
MergedFilesLoader
built using nikic/php-parser.
Example:
Assume we have two file:
and assume we have not env variables set.
Caching wil lead to following content:
Merging will leads to:
If you change environment variables value and will not invalidate cache then you are likely to run into troubles.
With merging you are unlikely to run into such issue. Performance for both FileCacheLoader
and MergedFilesLoader
are
almost the same (run php example/benchmark_loaders.php
to see exact results in your environment).
Performance:
You can run benchmarking in your environment with php example/benchmark_loaders.php
command.
Here are common results on ssd drive:
This bench run at the end of every travis build, so you can check results by seeing builds result.
Where to go from here:
Use provided ConfigInterface
to inject config dependency and use it in your projects. Feels free to report any
issues or suggest new Loaders or some new cool ideas to improve this library.
Config interface:
Configs loader interface:
Notes
Features that are not goal of this library:
- Config items validation logic.
- Dumping config items in fancy way with colors and formatting.
These features are not goals of this library and are subject of application- or framework-specific logic.