PHP code example of pinepain / simple-config

1. Go to this page and download the library: Download pinepain/simple-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/ */

    

pinepain / simple-config example snippets


    
    
    SimpleConfig\Loaders\FilesLoader;
    use Pinepain\SimpleConfig\Config;

    $loader = new FilesLoader(__DIR__ . '/config');
    $config_items = $loader->load();
    
    $config = new Config($config_items);
    
    $config->has('some.value.you.are.looking.for');
    $config->get('some.value'); // will return all nested values under 'some.value' section
    $config->set('some.value', 'changed'); // change it
    // get item value or use default value if original item missed (and it is as we change it above)
    $config->get('some.value.you.are.looking.for', 'missed'); 

    // app.php
    return [
        'env' => getenv('APP_ENV') ?: 'prod',
    ];
   
    // db.php
    return [
        'connections' => [
            'default' => [
                'driver'   => 'mysql',
                'host'     => getenv('DB_HOST') ?: 'localhost',
                'port'     => 3306,
                'user'     => getenv('DB_USER') ?: 'guest',
                'password' => getenv('DB_PASSWORD') ?: 'secret',
            ]
        ]
    ];


    // app.php
    return [
        'app' => [
            'env' => 'prod',
        ],
        'db' => [
            'connections' => [
                'default' => [
                    'driver'   => 'mysql',
                    'host'     => 'localhost',
                    'port'     => 3306,
                    'user'     => 'guest',
                    'password' => 'secret',
                ]
            ]
        ]
    ];

    // app.php
    return [
        'app' [
            'env' => getenv('APP_ENV') ?: 'prod',
        ],
        'db' =>[
            'connections' => [
                'default' => [
                    'driver'   => 'mysql',
                    'host'     => getenv('DB_HOST') ?: 'localhost',
                    'port'     => 3306,
                    'user'     => getenv('DB_USER') ?: 'guest',
                    'password' => getenv('DB_PASSWORD') ?: 'secret',
                ]
            ]
        ]
    ];

    
    
    namespace Pinepain\SimpleConfig;
    
    interface ConfigInterface
    {
        /**
         * @param array | \ArrayObject $items
         */
        public function __construct($items);

        /**
         * Get all of the configuration items.
         *
         * @return array
         */
        public function all();
    
        /**
         * Determine if the given configuration value exists.
         *
         * @param  string $key
         * @return bool
         */
        public function has($key);
    
        /**
         * Get the specified configuration value.
         *
         * @param  string $key
         * @param  mixed $default
         * @return mixed
         */
        public function get($key, $default = null);
    
        /**
         * Set a given configuration value.
         *
         * @param  string $key
         * @param  mixed $value
         * @return void
         */
        public function set($key, $value = null);
    }

    
    
    namespace Pinepain\SimpleConfig\Loaders;
    
    interface LoaderInterface
    {
        /**
         * Load config items
         *
         * @return array | \ArrayObject
         */
        public function load();
    }

example/config
├── .dotfile.php
├── app.php
├── cache.php
├── db.php
├── mail.php.old
└── somedir/
    └── somefile.php