summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/vendor/mrclay/props-dic/scripts/example-pimple.php
blob: 23bfee59fc9d4c6b7348405af126832367215b17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * Example of Props\Pimple based on official Pimple docs
 */

namespace {
    require __DIR__ . '/../vendor/autoload.php';
}

namespace PropsExample {

    class SessionStorage {
        public function __construct($cookieName) { $this->cookieName = $cookieName; }
    }
    class Session {
        public function __construct($storage) { $this->storage = $storage; }
    }
    class Zend_Mail {
        public function setFrom($from) { $this->from = $from; }
    }

    /**
     * @property-read string     $cookie_name
     * @property-read string     $session_storage_class
     * @property-read Session   $session
     * @property-read \Closure   $random
     * @property-read Zend_Mail $mail
     */
    class MyContainer2 extends \Props\Pimple {
        public function __construct() {
            parent::__construct();

            $this->cookie_name = 'SESSION_ID';

            $this->session_storage_class = 'PropsExample\\SessionStorage';

            $this->session_storage = function (MyContainer2 $c) {
                $class = $c->session_storage_class;
                return new $class($c->cookie_name);
            };

            $this->session = $this->factory(function (MyContainer2 $c) {
                return new Session($c->session_storage);
            });

            $this->random = $this->protect(function () { return rand(); });

            $this->mail = function (MyContainer2 $c) {
                return new Zend_Mail();
            };

            $this->{'mail.default_from'} = 'foo@example.com';

            $this->extend('mail', function($mail, MyContainer2 $c) {
                $mail->setFrom($c->{'mail.default_from'});
                return $mail;
            });
        }
    }

    $c = new MyContainer2;

    $r1 = $c->random;
    $r2 = $c->random;

    echo (int)($r1 === $r2) . "<br>";

    echo $r1() . "<br>";

    echo get_class($c->raw('session')) . '<br>';

    echo var_export($c->session, true) . '<br>';

    echo var_export($c->mail, true) . '<br>';
}