コード例 #1
0
<?php

// bootstrap
include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php');
// must register autoloader
spl_autoload_register(function ($class) {
    if (strpos($class, 'Foo\\Bar') !== 0) {
        return;
    }
    include_once __DIR__ . '/11/' . str_replace('\\', '/', substr($class, 7)) . '.php';
});
// compile phase
$compiler = new Zend\Di\Definition\CompilerDefinition();
$compiler->addDirectory(__DIR__ . '/11/');
$compiler->compile();
$arrayDef = $compiler->toArrayDefinition();
$definitions = new Zend\Di\DefinitionList($arrayDef);
$di = new Zend\Di\Di($definitions);
$baz = $di->get('Foo\\Bar\\Baz');
// expression to test
$works = $baz->bam instanceof Foo\Bar\Bam;
// display result
echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL;
コード例 #2
0
<?php

require_once 'library/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));
$loader->registerNamespace('SON', 'library/SON');
$loader->register();
$components = array('SON');
foreach ($components as $component) {
    $diCompiler = new Zend\Di\Definition\CompilerDefinition();
    $diCompiler->addDirectory('library/' . $component);
    $diCompiler->compile();
    file_put_contents('data/di/' . $component . '-definition.php', '<?php return ' . var_export($diCompiler->toArrayDefinition()->toArray(), true) . ';');
}