This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Home
gimpe edited this page Jun 19, 2011
·
5 revisions
Kohana 3.1 module to do Dependency Injection .
Configuration file format:
<?php
return array(
// id used to retrive the object
'id' => array(
// object to return for the id
'class' => 'NameSpace\Class',
// attributes to set via constructor (in order)
'constructor_attributes' => array(
0 => 'value1',
1 => 'value2',
),
// attributes to set via public attributes or setter method
'setter_attributes' => array(
'attribute1' => 'value1',
'attribute2' => 'value2',
),
// instanciate only on first use (default: TRUE) or instanciate during module init (FALSE)
'use_lazyload' => TRUE,
// use a single instance (default: TRUE) or create new instances each time (FALSE)
'use_singleton' => TRUE,
),
);
Configuration exemple:
<?php
return array(
'doctrine' => array(
'class' => 'Doctrine_ORM',
'use_lazyload' => TRUE,
'use_singleton' => FALSE,
),
);
Usage:
<?php
abstract class Controller_Database extends Controller
{
private $orm;
public function before()
{
parent::before();
$this->orm = Registry::instance()->get('doctrine');
}
}
Usage for an existing instance or a singleton:
config/registry.php
<?php
return array(
'logger' => array(
'use_lazyload' => TRUE, // deffer object creation to allow an explicit set
),
);
bootstrap.php
// ...
Registry::$instance->set('logger', Kohana::$log);
classes/controller/welcome.php
<?php
class Controller_Welcome extends Controller
{
public function action_index()
{
$logger = Registry::instance()->get('logger');
$logger->add(7, 'Hello log!');
}
}