public function load($json, \Dice\Dice $dice = null) { if ($dice === null) { $dice = new \Dice\Dice(); } if (trim($json)[0] != '{') { $path = dirname(realpath($json)); $json = str_replace('__DIR__', $path, file_get_contents($json)); } $map = json_decode($json, true); if (!is_array($map)) { throw new \Exception('Could not decode json: ' . json_last_error_msg()); } if (isset($map['rules'])) { foreach ($map['rules'] as $rule) { $name = $rule['name']; unset($rule['name']); $dice->addRule($name, $rule); } } else { foreach ($map as $name => $rule) { $dice->addRule($name, $rule); } } return $dice; }
protected function setUp() { parent::setUp(); $dice = new \Dice\Dice(); $this->dice = $this->getMock('\\Dice\\Dice', array('getRule', 'addRule')); $this->dice->expects($this->any())->method('getRule')->will($this->returnValue($dice->getRule('*'))); $this->xmlLoader = new \Dice\Loader\XML(); }
public function load($json, \Dice\Dice $dice = null) { if ($dice === null) { $dice = new \Dice\Dice(); } $map = json_decode($json, true); if (!is_array($map)) { throw new \Exception('Could not decode json: ' . json_last_error_msg()); } foreach ($map['rules'] as $rule) { $name = $rule['name']; unset($rule['name']); $dice->addRule($name, $rule); } return $dice; }
/** * "Start" the application: * Analyze the URL elements and calls the according controller/method or the fallback */ public function __construct() { // create array with URL parts in $url $this->splitUrl(); //Sistema de Dependencias $dice = new \Dice\Dice(); //Reglas del constructor, directorio de plantillas y singleton $rule = ['constructParams' => ['directory' => APP . 'view'], 'shared' => true]; $dice->addRule('League\\Plates\\Engine', $rule); $rule = ['constructParams' => ['msg' => "ERROR,Ese controlador no existe"]]; $dice->addRule('Error', $rule); // check for controller: no controller given ? then load start-page if (!$this->url_controller) { require APP . 'controller/Home.php'; $page = $dice->create("Home"); $page->index(); } elseif (file_exists(APP . 'controller/' . $this->url_controller . '.php')) { // here we did check for controller: does such a controller exist ? // if so, then load this file and create this controller // example: if controller would be "car", then this line would translate into: $this->car = new car(); require APP . 'controller/' . $this->url_controller . '.php'; $this->url_controller = $dice->create($this->url_controller); // check for method: does such a method exist in the controller ? if (method_exists($this->url_controller, $this->url_action)) { if (!empty($this->url_params)) { // Call the method and pass arguments to it call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params); } else { // If no parameters are given, just call the method without parameters, like $this->home->method(); $this->url_controller->{$this->url_action}(); } } else { if (strlen($this->url_action) == 0) { // no action defined: call the default index() method of a selected controller $this->url_controller->index(); } else { header('HTTP/1.0 404 Not Found'); require APP . 'controller/Error.php'; $cont_error = $dice->create("Error"); $cont_error->index(); } } } else { header('HTTP/1.0 404 Not Found'); require APP . 'controller/Error.php'; $cont_error = $dice->create("Error"); $cont_error->index(); } }
<?php $dice = new \Dice\Dice(); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $dice->create('A'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php require dirname(__FILE__) . '/../../vendor/autoload.php'; // this tells Dice only, that an associated class has to be instantiate using the // following constructor parameter (see addRule at the bottom for more info) $ruleNamedNode = new \Dice\Rule(); $ruleNamedNode->constructParams = array('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'); // this tells Dice, that an associated class has to be instantiate using the // following constructor parameter AND that each occurrences of Node // have to be replaced with an instance of Saft\Rdf\NamedNodeImpl $ruleLiteral = new \Dice\Rule(); $ruleLiteral->constructParams = array('literal value', 'de_DE'); $ruleLiteral->substitutions['Saft\\Rdf\\Node'] = new \Dice\Instance('Saft\\Rdf\\NamedNodeImpl'); // the rules above are worthless alone, but now we connect them to certain classes. // if you create one of these classes using this $dice instance, the rules above will // take effect $dice = new \Dice\Dice(); $dice->addRule('Saft\\Rdf\\NamedNodeImpl', $ruleNamedNode); $dice->addRule('Saft\\Rdf\\LiteralImpl', $ruleLiteral); $literal = $dice->create('Saft\\Rdf\\LiteralImpl'); echo $literal->getValue() . PHP_EOL;
<?php $dice = new \Dice\Dice(); for ($i = 0; $i < $argv[1]; $i++) { $j = $dice->create('J'); } $results = ['time' => 0, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php error_reporting(-1); include_once realpath(dirname(__FILE__)) . '/autoloader.php'; require_once realpath(dirname(__FILE__)) . '/../vendor/Dice/Dice.php'; require_once realpath(dirname(__FILE__)) . '/../config.php'; autoloader::init(); $dice = new \Dice\Dice(); $dice->addRule('\\SlackScore\\Models\\Request', ['shared' => true, 'constructParams' => [$_REQUEST]]); $dice->addRule('\\SlackScore\\Utils\\SlackPost', ['shared' => true, 'constructParams' => [$config['posturl'], $config['username'], $config['channel']]]); $dice->addRule('\\SlackScore\\Repository\\JsonUserRepository', ['shared' => true, 'constructParams' => [$config['users']], 'substitutions' => ['SlackScore\\Storage\\IStorage' => ['instance' => '\\SlackScore\\Storage\\FileSystem']]]); $dice->addRule('\\SlackScore\\Repository\\SlackUserRepository', ['shared' => true, 'constructParams' => [$config['botToken']]]); $dice->addRule('\\SlackScore\\Utils\\Comeback', ['constructParams' => [$config['phrases']], 'substitutions' => ['SlackScore\\Repository\\IUserRepository' => ['instance' => '\\SlackScore\\Repository\\JsonUserRepository']]]); $dice->addRule('\\SlackScore\\Main', ['constructParams' => [$config['token']], 'substitutions' => ['SlackScore\\Repository\\IUserRepository' => ['instance' => '\\SlackScore\\Repository\\JsonUserRepository']]]);
<?php $dice = new \Dice\Dice(); //Trigger all autoloaders $a = $dice->create('J'); unset($a); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $dice->create('J'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php /** * PHP DI example using Dice https://r.je/dice.html */ require 'autoload.php'; require 'Dice.php'; try { $dice = new \Dice\Dice(); $http = $dice->create('\\services\\http', ['api/test.json']); $db = $dice->create('\\services\\db'); $tpl = ['http' => $http->get(), 'db' => $db->connect()]; } catch (Exception $e) { $tpl = ['error' => $e->getMessage()]; } ?> <!DOCTYPE html> <html> <head> <title>PHP Dice DI</title> </head> <body> <h1>PHP Dice DI</h1> <?php if ($tpl['error']) { ?> <h3 class="error"><?php echo $tpl['error']; ?>
<?php /** * This example shows how to force a certain class as substitution of an interface. * The class must implement that interface. */ require dirname(__FILE__) . '/../../vendor/autoload.php'; $rule = new \Dice\Rule(); $rule->substitutions['Saft\\Rdf\\Node'] = new \Dice\Instance('Saft\\Rdf\\NamedNodeImpl'); $dice = new \Dice\Dice(); $dice->addRule('Saft\\Rdf\\NamedNodeImpl', $rule); $namedNode = $dice->create('Saft\\Rdf\\NamedNodeImpl', array('http://uri')); echo $namedNode->getUri() . PHP_EOL;
<?php namespace SaftExample; require "vendor/autoload.php"; use Saft\Store\Store; use Saft\Rdf\NodeFactory; use Saft\Rdf\StatementFactory; use Saft\Sparql\Query\QueryFactory; use Saft\Sparql\Result\ResultFactory; $rule = new \Dice\Rule(); $rule->substitutions['Saft\\Rdf\\NodeFactory'] = new \Dice\Instance('Saft\\Rdf\\NodeFactoryImpl'); $rule->substitutions['Saft\\Rdf\\StatementFactory'] = new \Dice\Instance('Saft\\Rdf\\StatementFactoryImpl'); $rule->substitutions['Saft\\Sparql\\Query\\QueryFactory'] = new \Dice\Instance('Saft\\Sparql\\Query\\QueryFactoryImpl'); $rule->substitutions['Saft\\Store\\Result\\ResultFactory'] = new \Dice\Instance('Saft\\Store\\Result\\ResultFactoryImpl'); $dice = new \Dice\Dice(); $dice->addRule('*', $rule); $params = [['dsn' => 'vos', 'username' => 'dba', 'password' => 'dba']]; $store = $dice->create("Saft\\Addition\\Virtuoso\\Store\\Virtuoso", $params); //$parser = getParser("turtle"); //$statements = $parser->parseStreamToIterator($fileStream, "http://example.org", "turtle"); class ExampleApplication { private $store; private $nf; private $sf; public function __construct(Store $store, NodeFactory $nf, StatementFactory $sf) { $this->store = $store; $this->nf = $nf; $this->sf = $sf;
<?php $dice = new \Dice\Dice(); $dice->addRule('A', ['shared' => true]); //Trigger all autoloaders $b = $dice->create('B'); unset($b); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $b = $dice->create('B'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);