| 1 |
<?php |
| 2 |
/** |
| 3 |
* Example of Props\Pimple based on official Pimple docs |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace { |
| 7 |
require __DIR__ . '/../vendor/autoload.php'; |
| 8 |
} |
| 9 |
|
| 10 |
namespace PropsExample { |
| 11 |
|
| 12 |
class SessionStorage { |
| 13 |
public function __construct($cookieName) { $this->cookieName = $cookieName; } |
| 14 |
} |
| 15 |
class Session { |
| 16 |
public function __construct($storage) { $this->storage = $storage; } |
| 17 |
} |
| 18 |
class Zend_Mail { |
| 19 |
public function setFrom($from) { $this->from = $from; } |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @property-read string $cookie_name |
| 24 |
* @property-read string $session_storage_class |
| 25 |
* @property-read Session $session |
| 26 |
* @property-read \Closure $random |
| 27 |
* @property-read Zend_Mail $mail |
| 28 |
*/ |
| 29 |
class MyContainer2 extends \Props\Pimple { |
| 30 |
public function __construct() { |
| 31 |
parent::__construct(); |
| 32 |
|
| 33 |
$this->cookie_name = 'SESSION_ID'; |
| 34 |
|
| 35 |
$this->session_storage_class = 'PropsExample\\SessionStorage'; |
| 36 |
|
| 37 |
$this->session_storage = function (MyContainer2 $c) { |
| 38 |
$class = $c->session_storage_class; |
| 39 |
return new $class($c->cookie_name); |
| 40 |
}; |
| 41 |
|
| 42 |
$this->session = $this->factory(function (MyContainer2 $c) { |
| 43 |
return new Session($c->session_storage); |
| 44 |
}); |
| 45 |
|
| 46 |
$this->random = $this->protect(function () { return rand(); }); |
| 47 |
|
| 48 |
$this->mail = function (MyContainer2 $c) { |
| 49 |
return new Zend_Mail(); |
| 50 |
}; |
| 51 |
|
| 52 |
$this->{'mail.default_from'} = '[email protected]'; |
| 53 |
|
| 54 |
$this->extend('mail', function($mail, MyContainer2 $c) { |
| 55 |
$mail->setFrom($c->{'mail.default_from'}); |
| 56 |
return $mail; |
| 57 |
}); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$c = new MyContainer2; |
| 62 |
|
| 63 |
$r1 = $c->random; |
| 64 |
$r2 = $c->random; |
| 65 |
|
| 66 |
echo (int)($r1 === $r2) . "<br>"; |
| 67 |
|
| 68 |
echo $r1() . "<br>"; |
| 69 |
|
| 70 |
echo get_class($c->raw('session')) . '<br>'; |
| 71 |
|
| 72 |
echo var_export($c->session, true) . '<br>'; |
| 73 |
|
| 74 |
echo var_export($c->mail, true) . '<br>'; |
| 75 |
} |
| 76 |
|