PHP code example of axy / env

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

    

axy / env example snippets


use axy\env\Factory;

class Service
{
    public function __construct($env = null)
    {
        $this->env = Factory::create($env);
    }

    public function action()
    {
        $timeOfAction = $this->env->getCurrentTime();
        // ...
    }

    private $env;
}

// ...

$service = new Service();
$service->action();

$service = new Service(['time' => '2014-11-04 10:11:12']);
$service->action(); // the service will receive the specified time

use axy\env\Env;

$env = new Env();

$config = [
    'time' => '2015-11-04 11:11:11',
    'get' => [
        'id' => 5,
    ],
];

$env = new Env($config);

$config = new Config();
$config->time = '2015-11-04 11:11:11';
$config->get = ['id' => 5];

$env = new Env($config);

$config = new Config();
$config->time = '2015-11-04 11:11:11';
$env = new Env($config);

$config->time = '2011-02-03 10:10:10';
echo date('j.m.Y', $env->getCurrentTime()); // 4.11.2015, config is cloned

$env->getCurrentTime(void): int


$config->time = '2015-11-04 00:01:02';
$env = new Env($config);

echo date('j.m.Y', $env->getCurrentTime()); // 4.11.2015

/**
 * Cron daemon
 * Once an hour to kill, not to eat a lot of memory
 */

$startTime = time();

while (time() - $startTime() < 3500) {
    step();
    sleep(5);
}


$env = new Env([
    'time' => '1980-01-02 11:20:30',
    'timeChanging' => true,
]);

echo date('H:i:s', $env->getCurrentTime()).PHP_EOL; // 11:20:30
sleep(7);
echo date('H:i:s', $env->getCurrentTime()).PHP_EOL; // 11:20:37

$config = [
    'functions' => [
        'time' => 'myOwnTimeImplementation',
    ],
];

$config = [
    'get' => ['x' => 1],
];
$env = new Env($config);

$env->get['x']; // 1
$env->post['x']; // $_POST['x']

$env->strlen('string'); // 6
$env->header('Content-Type: text/plain'); // Send header

$config = [
    'functions' => [
        'header' => function ($header) {
            // save header to a local storage
        },
    ],
];

$env = new Env();

$env->header('Content-Type: text/plain'); // The header will not be sent

if ($env->isFunctionExists('getallheaders')) {
    return $env->getallheaders();
}

$config = [
    'functions' => [
        'getallheaders' => function () {
            if (function_exists('getallheaders')) {
                return getallheaders();
            } else {
                return parseServerVarsForHeaders();
            }
        },
    ],
];

// now $env->getallheaders() always available

$config = [
    'functions' => [
        'getallheaders' => null, // never
    ],
];

$env->streams->stdout->write('Output');

$config = [
    'streams' => [
        'stdin' => new MyStream(),
    ],
];
$env = new Env($config);

$config = [
    'streams' => [
        'stdin' => new \axy\env\Stream(fopen('/my/file.txt')),
    ],
];
$env = new Env($config);

$mock = new \axy\env\StreamMock('content');
$mock->read(3); // "con"
$mock->read(); // "tent"
$mock->write('!');
$mock->setPosition(0);
$mock->read(); // "content!"

use axy\env\Factory;

$env = Factory::getStandard();

$config = [
    'time' => 123456,
];

$env = Factory::create($config);

$config = new Config();
$config->time = 123456,

$env = Factory::create($config);

$env1 = new Env([
    'time' => 123456,
]);

$env2 = Factory::create($env1); // $env1 === $env2

use axy\env\Factory;

class Service
{
    public function __construct($env = null)
    {
        $this->env = Factory::create($env);
    }

    // ...
}

$service = new Service(['time' => '2011-01-01']);