public function setUp() { IoC::register('Logger', function () { return $this->getMock('stdClass', ['log']); }); IoC::register('Product', function ($name, $price) { return new Product($name, $price); }); $this->product = new Product('Water', 5.99); }
class Baz { protected $argument; public function __construct($argument) { $this->argument = $argument; } public function working() { return $this->argument->say() . 'Yep!'; } } /** * Define dependencies */ $dic = new IoC(); $dic->register('greeter', function () { return new FooBar(); }); $dic->register('status', function () use($dic) { return new Baz($dic->get('greeter')); }); /** * Real Usage */ $greeter = $dic->get('greeter'); print $greeter->say() . ' ' . $greeter->hello() . ' ' . $greeter->world() . PHP_EOL . '<br />'; // I say: Hello , World! $status = $dic->get('status'); // I say: Yep! print $status->working();
<?php if (!File::exists(path('base') . 'vendor/autoload.php')) { throw new Exception('You need to run composer update to complete installation of this project.'); } require path('base') . 'vendor/autoload.php'; Autoloader::namespaces(array('Mail' => Bundle::path('mail'))); IoC::register('Mail', function () { return new Mail\Libraries\Mail(); });
/** * Test the Controller::resolve method. * * @group laravel */ public function testResolveMethodChecksTheIoCContainer() { IoC::register('controller: home', function () { require_once path('app') . 'controllers/home.php'; $controller = new Home_Controller(); $controller->foo = 'bar'; return $controller; }); $controller = Controller::resolve(DEFAULT_BUNDLE, 'home'); $this->assertEquals('bar', $controller->foo); }
/** * Test the IoC::controller method. * * @group laravel */ public function testControllerMethodRegistersAController() { IoC::register('controller: ioc.test', function () { }); $this->assertTrue(IoC::registered('controller: ioc.test')); }
}); Event::listen('laravel.auth: logout', function () { Session::forget('oneauth'); }); /* |-------------------------------------------------------------------------- | OneAuth IoC |-------------------------------------------------------------------------- | | Register Auth adapter as IoC, allow it to be replaced by any Authentication | bundle that doesn't use Laravel\Auth\Drivers. | */ if (!IoC::registered('oneauth.driver: auth.check')) { // Check whether current user is logged-in to the system or a guest IoC::register('oneauth.driver: auth.check', function () { return Auth::check(); }); } if (!IoC::registered('oneauth.driver: auth.user')) { // Get logged in user, if the user doesn't logged in yet, return null IoC::register('oneauth.driver: auth.user', function () { return Auth::user(); }); } if (!IoC::registered('oneauth.driver: auth.login')) { // Login the user by users.id IoC::register('oneauth.driver: auth.login', function ($user_id) { return Auth::login($user_id); }); }
<?php require '../TablePrinter.php'; require 'Loggr.php'; require 'Loggr/EchoOut.php'; require 'Product.php'; require 'PurchaseManager.php'; require 'IoC.php'; IoC::register('Logger', function () { return new Loggr(new Loggr\EchoOut()); }); IoC::register('Product', function ($name, $price) { return new Product($name, $price); }); $pm = new PurchaseManager(); $store = [new Product('Juice', '1.99'), new Product('Milk', '3.99'), new Product('Water', '0.99')]; $pm->purchase($store[0]); $pm->purchase($store[1]); $pm->purchaseDiscountedProduct($store[2], 50); $tp = new TablePrinter(['Product', 'Price']); echo "You've purchased:\n"; foreach ($pm->purchaseHistory() as $purchase) { $tp->addRow($purchase->getName(), '$' . $purchase->getPrice()); } $tp->output();
<?php Autoloader::namespaces(array('Bouncer' => Bundle::path('bouncer') . 'src')); Autoloader::alias('Bouncer\\Bouncer', 'Bouncer'); IoC::register('bouncer: roles_extractor', function () { return function ($user) { return array_map(function ($r) { return $r->name; }, $user->roles); }; });
return new Ibundle_Base_Task($dependency, 'install'); }); // Activate IoC::register('task: ibundle::activate', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'activate'); }); // Deactivate IoC::register('task: ibundle::deactivate', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'deactivate'); }); // Track IoC::register('task: ibundle::track', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'track'); }); // Untrack IoC::register('task: ibundle::untrack', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'untrack'); }); // Available IoC::register('task: ibundle::available', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'available'); }); // Activated IoC::register('task: ibundle::activated', function () use($dependency) { return new Ibundle_Base_Task($dependency, 'activated'); }); // Initialize: Depreciated in 1.1.0! IoC::register('task: ibundle::initialize', function () { echo 'NOTE: This method is depreciated, use the "ibundle::track" command instead.' . PHP_EOL . PHP_EOL; return IoC::resolve('task: ibundle::track'); });
| and state of your application. Here we'll just fire up the session | if a session driver has been configured. | */ if (!Request::cli() and Config::get('session.driver') !== '') { Session::load(); } Autoloader::map(array('simple_html_dom' => path('app') . 'libraries/simple_html_dom.php')); /* IoC registration */ IoC::register('tester', function () { return new Tester(); }); IoC::register('requests', function () { return new ClementiaRequest(); }); IoC::register('htmlparser', function () { return new HtmlParser(); }); /* utility functions */ function array_map_deep($callback, array $array) { $new = array(); foreach ($array as $key => $val) { if (is_array($val)) { $new[$key] = array_map_deep($callback, $val); } else { $new[$key] = call_user_func($callback, $val); } } return $new; }
<?php use Litmus\Handler\LitmusHandler; IoC::register('controller: litmus::image', function () { return new Litmus_Image_Controller(new LitmusHandler()); });
<?php // Configure the PSR-0 compatible autoloader Autoloader::namespaces(array('Stampie' => Bundle::path('stampie') . 'stampie' . DS . 'lib' . DS . 'Stampie')); // Register a postmark mailer in the IoC container IoC::register('postmark', function () { // Load the default settings, if they exist. $token = Config::get('stampie::postmark', array()); // Return the instance for later usage $adapter = new Stampie\Adapter\Buzz(new Buzz\Browser()); return new Stampie\Mailer\Postmark($adapter, $token['apiKey']); }); // Register more mailer here // More infomation about how to use Stampie can be found at https://github.com/henrikbjorn/Stampie
Orchestra\Extension\Config::map('oneauth', array('basecamp_id' => 'oneauth::api.providers.basecamp.id', 'basecamp_secret' => 'oneauth::api.providers.basecamp.secret', 'dropbox_key' => 'oneauth::api.providers.dropbox.key', 'dropbox_secret' => 'oneauth::api.providers.dropbox.secret', 'facebook_id' => 'oneauth::api.providers.facebook.id', 'facebook_secret' => 'oneauth::api.providers.facebook.secret', 'facebook_scope' => 'oneauth::api.providers.facebook.scope', 'flickr_key' => 'oneauth::api.providers.flickr.key', 'flickr_secret' => 'oneauth::api.providers.flickr.secret', 'foursquare_id' => 'oneauth::api.providers.foursquare.id', 'foursquare_secret' => 'oneauth::api.providers.foursquare.secret', 'github_id' => 'oneauth::api.providers.github.id', 'github_secret' => 'oneauth::api.providers.github.secret', 'google_id' => 'oneauth::api.providers.google.id', 'google_secret' => 'oneauth::api.providers.google.secret', 'instagram_id' => 'oneauth::api.providers.instagram.id', 'instagram_secret' => 'oneauth::api.providers.instagram.secret', 'linkedin_key' => 'oneauth::api.providers.linkedin.key', 'linkedin_secret' => 'oneauth::api.providers.linkedin.secret', 'paypal_id' => 'oneauth::api.providers.paypal.id', 'paypal_secret' => 'oneauth::api.providers.paypal.secret', 'soundcloud_id' => 'oneauth::api.providers.soundcloud.id', 'soundcloud_secret' => 'oneauth::api.providers.soundcloud.secret', 'tumblr_key' => 'oneauth::api.providers.tumblr.key', 'tumblr_secret' => 'oneauth::api.providers.tumblr.secret', 'twitter_key' => 'oneauth::api.providers.twitter.key', 'twitter_secret' => 'oneauth::api.providers.twitter.secret', 'vimeo_key' => 'oneauth::api.providers.vimeo.key', 'vimeo_secret' => 'oneauth::api.providers.vimeo.secret', 'windowslive_id' => 'oneauth::api.providers.windowslive.id', 'windowslive_secret' => 'oneauth::api.providers.windowslive.secret')); /* |-------------------------------------------------------------------------- | Integration with Orchestra |-------------------------------------------------------------------------- | | Map controller routing for OneAuth. | */ Route::controller(array('oneauth::connect')); /* |-------------------------------------------------------------------------- | Integration with Orchestra |-------------------------------------------------------------------------- | | Add on logged-in integration between OneAuth and Orchestra. | */ Event::listen('orchestra.auth: login', function () { $user = IoC::resolve('oneauth.driver: auth.user'); Event::fire('oneauth.sync', array($user->id)); }); IoC::register('orchestra.user: register', function () { $user = new Orchestra\Model\User(); if (!is_null($session = OneAuth\Auth\Core::session())) { $user->fullname = $session['info']['name'] ?: ''; $user->email = $session['info']['email'] ?: ''; } return $user; }); include_once Bundle::path('oneauth') . 'orchestra' . DS . 'configure' . EXT;
<?php // Configure the autoloader Autoloader::map(array('PHPMailer' => Bundle::path('phpmailer') . 'lib' . DS . 'class.phpmailer.php')); // Register a mailer in the IoC container IoC::register('phpmailer', function () { // Instantiate a new PHPMailer. Passing 'true' to the constructor // means that exceptions are thrown on errors, which you can // catch in your application. $mailer = new PHPMailer(true); // set the default plugin dir for the instance $mailer->PluginDir = Bundle::path('phpmailer') . 'lib' . DS; // Load the default settings, if they exist. $config = Config::get('phpmailer::phpmailer', array()); foreach ($config as $key => $value) { if (is_null($value)) { $mailer->{$key}(); } else { if (is_array($value)) { call_user_func_array(array($mailer, $key), $value); } else { $mailer->{$key} = $value; } } } // Return the instance. return $mailer; });
<?php // Set hashids configuration $config = array('salt' => 'KareLGrouP159753', 'length' => 6); // Autoload libraries folder Autoloader::directories(array(Bundle::path('hashids') . 'libraries')); Autoloader::namespaces(array('Hashids\\Libraries' => Bundle::path('hashids') . 'libraries')); // Register Hashids in the IoC container for DRY code // and Dependency Injection IoC::register('hashids', function () use($config) { return new Hashids\Libraries\hashids($config['salt'], $config['length']); });
<?php Autoloader::namespaces(array('Navigation\\Model' => Bundle::path('navigation') . 'models' . DS, 'Navigation' => Bundle::path('navigation') . 'libraries' . DS)); IoC::register('Menu', function () { return $menu = new Navigation\Menu(); }); /* |-------------------------------------------------------------------------- | Load Module Helpers file |-------------------------------------------------------------------------- | | Load all module magic methods and make them | available globally in the application | */ include __DIR__ . DS . 'helpers.php';