first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,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>';
}

View File

@@ -0,0 +1,71 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
class AAA {}
class BBB {}
class CCC {
public function __construct(BBB $bbb) {}
public function setBbb(BBB $bbb) {}
public $aaa;
}
class DDD {}
function get_a_bbb() { return new BBB; }
/**
* @property-read AAA $aaa
* @property-read BBB $bbb1
* @property-read BBB $bbb2
* @property-read BBB $bbb3
* @property-read CCC $ccc
* @property-read DDD $ddd
*
* @method AAA new_aaa()
*/
class MyContainer extends \Props\Container {
public function __construct() {
// store plain old values
$this->ddd = new DDD;
$this->{'bbb.class'} = 'BBB';
// set a factory, which will construct an object on demand
$this->aaa = function () {
return new AAA();
};
// alternative factory syntax, and using a reference to specify the class name
$this->setFactory('bbb1', function (MyContainer $c) {
return new $c->{'bbb.class'};
});
// fetch with a callback
$this->setFactory('bbb2', 'get_a_bbb');
// Closures automatically used as factories
$this->bbb3 = function (MyContainer $c) {
return $c->bbb2;
};
// more advanced factory
$this->ccc = function (MyContainer $c) {
$val = new CCC($c->bbb1);
$val->setBbb($c->bbb2);
$val->aaa = $c->aaa;
return $val;
};
}
}
$c = new MyContainer;
$c->aaa; // factory builds a AAA
$c->aaa; // the same AAA
$c->new_aaa(); // always a freshly-built AAA
$c->bbb1; // factory resolves bar.class, builds a BBB
$c->bbb2; // invoker calls get_a_bbb()
$c->bbb3; // invoker executes anon func, returning the already-cached $c->bbb2 instance
$c->ccc; // factory creates CCC, passing a new BBB object,
// calls setBbb(), passing in $c->bbb2,
// and sets the aaa property to $c->aaa

View File

@@ -0,0 +1,55 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
class Dough {}
class Cheese {}
class Slice {}
class Pizza {
function __construct($style, Cheese $cheese) {}
function setDough(Dough $dough) {}
function getSlice() { return new Slice(); }
}
class CheeseFactory {
static function getCheese() { return new Cheese(); }
}
/**
* @property-read string $style
* @property-read Dough $dough
* @property-read Cheese $cheese
* @property-read Pizza $pizza
* @method Slice new_slice()
*/
class MyDI extends \Props\Container {
public function __construct() {
$this->style = 'deluxe';
$this->dough = function (MyContainer $c) {
return new Dough();
};
$this->setFactory('cheese', 'CheeseFactory::getCheese');
$this->pizza = function (MyContainer $c) {
$pizza = new Pizza($c->style, $c->cheese);
$pizza->setDough($c->dough);
return $pizza;
};
// note 3rd argument $shared is false
$this->slice = function (MyContainer $c) {
return $c->pizza->getSlice();
};
}
}
$c = new MyContainer;
// You can request dependencies in any order. They're resolved as needed.
$slice1 = $c->new_slice(); // This first resolves and caches the cheese, dough, and pizza.
$slice2 = $c->new_slice(); // This just gets a new slice from the existing pizza.
assert($slice1 !== $slice2);
assert($c->pizza === $c->pizza);