Esempio n. 1
0
 public function testRegisterComponentUsingClosure()
 {
     $config = ['key' => 'value'];
     $this->app->register('sc', function () use($config) {
         $component = new SomeComponent('');
         $component->config($config);
         return $component;
     });
     $this->assertSame($config, $this->app->sc->getConfig());
 }
Esempio n. 2
0
class Registry
{
    /**
     * Returns the connection
     */
    public static function getConnection()
    {
        return new Connection(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo"));
    }
}
class SomeComponent
{
    protected $_connection;
    /**
     * Sets the connection externally
     */
    public function setConnection($connection)
    {
        $this->_connection = $connection;
    }
    public function someDbTask()
    {
        $connection = $this->_connection;
        // ...
    }
}
$some = new SomeComponent();
//Pass the connection defined in the registry
$some->setConnection(Registry::getConnection());
$some->someDbTask();
Esempio n. 3
0
<?php

//Create the dependencies or retrieve them from the registry
$connection = new Connection();
$session = new Session();
$fileSystem = new FileSystem();
$filter = new Filter();
$selector = new Selector();
//Pass them as constructor parameters
$some = new SomeComponent($connection, $session, $fileSystem, $filter, $selector);
// ... or using setters
$some->setConnection($connection);
$some->setSession($session);
$some->setFileSystem($fileSystem);
$some->setFilter($filter);
$some->setSelector($selector);
Esempio n. 4
0
class SomeComponent
{
    protected $_connection;
    /**
     * Sets the connection externally
     */
    public function setConnection($connection)
    {
        $this->_connection = $connection;
    }
    /**
     * This method always needs the shared connection
     */
    public function someDbTask()
    {
        $connection = $this->_connection;
        // ...
    }
    /**
     * This method always needs a new connection
     */
    public function someOtherDbTask($connection)
    {
    }
}
$some = new SomeComponent();
//This injects the shared connection
$some->setConnection(Registry::getSharedConnection());
$some->someDbTask();
//Here, we always pass a new connection as parameter
$some->someOtherDbTask(Registry::getConnection());
Esempio n. 5
0
<?php

class SomeComponent
{
    protected $_connection;
    /**
     * Sets the connection externally
     */
    public function setConnection($connection)
    {
        $this->_connection = $connection;
    }
    public function someDbTask()
    {
        $connection = $this->_connection;
        // ...
    }
}
$some = new SomeComponent();
//Create the connection
$connection = new Connection(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo"));
//Inject the connection in the component
$some->setConnection($connection);
$some->someDbTask();
Esempio n. 6
0
    public function someDbTask()
    {
        // Get the connection service
        // Always returns a new connection
        $connection = $this->_di->get('db');
    }
    public function someOtherDbTask()
    {
        // Get a shared connection service,
        // this will return the same connection everytime
        $connection = $this->_di->getShared('db');
        //This method also requires an input filtering service
        $filter = $this->_db->get('filter');
    }
}
$di = new Phalcon\DI();
//Register a "db" service in the container
$di->set('db', function () {
    return new Connection(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo"));
});
//Register a "filter" service in the container
$di->set('filter', function () {
    return new Filter();
});
//Register a "session" service in the container
$di->set('session', function () {
    return new Session();
});
//Pass the service container as unique parameter
$some = new SomeComponent($di);
$some->someTask();
Esempio n. 7
0
<?php

class SomeComponent
{
    /**
     * The instantiation of the connection is hardcoded inside
     * the component so is difficult replacing it externally
     * or change its behavior
     */
    public function someDbTask()
    {
        $connection = new Connection(array("host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo"));
        // ...
    }
}
$some = new SomeComponent();
$some->someDbTask();