1. Go to this page and download the library: Download cocoon-projet/config library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// En environnement production
$config = ConfigFactory::fromDirectory(__DIR__ . '/config');
$dbHost = $config->get('database.mysql.host'); // Valeur de database.production.php ou database.php
use Cocoon\Config\Environment\EnvironmentVariables;
// Charger depuis un fichier .env (indiquer le repertoire)
EnvironmentVariables::load(__DIR__);
// Ou charger manuellement
EnvironmentVariables::set('APP_ENV', 'development');
// ou
env('APP_ENV', 'development');
EnvironmentVariables::set('DB_HOST', 'localhost');
// ou
env('DB_HOST', 'localhost');
use Cocoon\Config\Factory\ConfigFactory;
use Cocoon\Config\Environment\Environment;
// Charger depuis un fichier .env (indiquer le repertoire)
EnvironmentVariables::load(__DIR__);
// Initialiser l'environnement
Environment::init(EnvironmentVariables::get('APP_ENV', 'development'));
// Charger la configuration
$config = ConfigFactory::fromDirectory(__DIR__ . '/config');
// Accéder aux valeurs
$dbHost = $config->get('database.mysql.host');
// Vous pouvez aussi créer une configuration directement à partir d'un tableau
$config = ConfigFactory::fromArray([
'app' => [
'url' => 'http://www.monsite.com',
'debug' => true,
'timezone' => 'Europe/Paris'
],
'database' => [
'host' => 'localhost',
'name' => 'ma_base',
'user' => 'utilisateur'
]
]);
use Cocoon\Config\Cache\ConfigurationCache;
// Vérifier si le cache est valide
if (ConfigurationCache::isFresh($configDir)) {
$config = ConfigurationCache::load();
} else {
$config = ConfigFactory::fromDirectory($configDir);
ConfigurationCache::save($config->all());
}
// Vider le cache si nécessaire
ConfigurationCache::clear();
use Cocoon\Config\Cache\GenericFileCache;
use Cocoon\Config\Factory\ConfigFactory;
use Cocoon\Config\Config;
// Initialiser le cache
$cache = new GenericFileCache(__DIR__ . '/cache');
// Utiliser le cache avec la factory
$config = ConfigFactory::fromDirectory(__DIR__ . '/config', $cache);
// Créer une configuration manuellement
$config = new Config([
'app' => [
'url' => 'http://www.monsite.com',
'debug' => true
]
]);
// Opérations de cache manuelles
$cache->set('ma_cle', $valeur);
$valeur = $cache->get('ma_cle');
$cache->delete('ma_cle');
$cache->clear();
// Récupérer une variable d'environnement
$dbHost = env('DB_HOST', 'localhost');
// Vérifier l'existence d'une variable
if (env('DEBUG', false)) {
// ...
}
// Vérifier l'existence d'une clé
if ($config->has('app.url')) {
$url = $config->get('app.url');
}
// Obtenir une valeur avec une valeur par défaut
$timeout = $config->get('app.timeout', 30);
// Validation des types
if ($config->isString('app.url')) {
// Traitement des chaînes
}
if ($config->isInt('app.port')) {
// Traitement des entiers
}
if ($config->isBool('app.debug')) {
// Traitement des booléens
}
if ($config->isArray('database.mysql')) {
// Traitement des tableaux
}
config/
├── database.php # Configuration par défaut
├── database.production.php # Configuration spécifique à la production
├── database.development.php # Configuration spécifique au développement
└── database.testing.php # Configuration spécifique aux tests
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.