/** * Magic Method for passing methods to a class registered in the IoC container. * This provides a convenient method of accessing functions on classes that * could not otherwise be accessed staticly. * * Facades allow Laravel to still have a high level of dependency injection * and testability while still accomodating the common desire to conveniently * use classes via static methods. */ public static function __callStatic($method, $parameters) { $class = IoC::resolve(static::$resolve); $count = count($parameters); if ($count > 5) { return call_user_func_array(array($class, $method), $parameters); } elseif ($count == 0) { return $class->{$method}(); } elseif ($count == 1) { return $class->{$method}($parameters[0]); } elseif ($count == 2) { return $class->{$method}($parameters[0], $parameters[1]); } elseif ($count == 3) { return $class->{$method}($parameters[0], $parameters[1], $parameters[2]); } elseif ($count == 4) { return $class->{$method}($parameters[0], $parameters[1], $parameters[2], $parameters[3]); } elseif ($count == 5) { return $class->{$method}($parameters[0], $parameters[1], $parameters[2], $parameters[3], $parameters[4]); } }
/** * Generate the session table on the database. * * @param array $arguments * @return void */ public function table($arguments = array()) { $migrator = IoC::resolve('task: migrate'); $key = IoC::resolve('task: key'); // Since sessions can't work without an application key, we will go // ahead and set the key if one has not already been set for the // application so the developer doesn't need to set it. $key->generate(); // To create the session table, we will actually create a database // migration and then run it. This allows the application to stay // portable through the framework's migrations system. $migration = $migrator->make(array('create_session_table')); $stub = path('sys') . 'cli/tasks/session/migration' . EXT; File::put($migration, File::get($stub)); // By default no session driver is set within the configuration. // Since the developer is requesting that the session table be // created on the database, we'll set it. $this->driver('database'); echo PHP_EOL; $migrator->run(); }
/** * Dynamically resolve items from the application IoC container. * * <code> * // Retrieve an object registered in the container * $mailer = $this->mailer; * * // Equivalent call using the IoC container instance * $mailer = IoC::resolve('mailer'); * </code> */ public function __get($key) { if (IoC::registered($key)) { return IoC::resolve($key); } }
/** * Resolve an instance of the given task name. * * <code> * // Resolve an instance of a task * $task = Command::resolve('application', 'migrate'); * * // Resolve an instance of a task wtihin a bundle * $task = Command::resolve('bundle', 'foo'); * </code> * * @param string $bundle * @param string $task * @return object */ public static function resolve($bundle, $task) { $identifier = Bundle::identifier($bundle, $task); // First we'll check to see if the task has been registered in the // application IoC container. This allows all dependencies to be // injected into tasks for more testability. if (IoC::registered("task: {$identifier}")) { return IoC::resolve("task: {$identifier}"); } // If the task file exists, we'll format the bundle and task name // into a task class name and resolve an instance of the so that // the requested method may be executed. if (file_exists($path = Bundle::path($bundle) . 'tasks/' . $task . EXT)) { require $path; $task = static::format($bundle, $task); return new $task(); } }
/** * Install a bundle using a provider. * * @param string $bundle * @param string $path * @return void */ protected function download($bundle, $path) { $provider = "bundle.provider: {$bundle['provider']}"; IoC::resolve($provider)->install($bundle, $path); }
/** * Dynamically resolve items from the application IoC container. * * <code> * // Retrieve an object registered in the container as "mailer" * $mailer = $this->mailer; * * // Equivalent call using the IoC container instance * $mailer = IoC::resolve('mailer'); * </code> */ public function __get($key) { if (IoC::registered($key)) { return IoC::resolve($key); } throw new \OutOfBoundsException("Attempting to access undefined property [{$key}] on controller."); }
/** * Create a new HelpSpot Migration instance. * * @return void */ public function __construct() { Bundle::start('doctrine'); $this->em = IoC::resolve('doctrine::manager'); $this->sm = $this->em->getConnection()->getSchemaManager(); }
/** * Test that when we resolve TestClassTwoForIoC with a parameter * that it actually uses that instead of a blank class TestClassOneForIoC */ public function testClassTwoResolvesClassOneWithArgument() { $class_one = IoC::resolve('TestClassOneForIoC'); $class_one->test_variable = 42; $class_two = IoC::resolve('TestClassTwoForIoC', array($class_one)); $this->assertEquals(42, $class_two->class_one->test_variable); }
*/ if (!IoC::registered('task: migrate')) { IoC::register('task: migrate', function () { $database = new Tasks\Migrate\Database(); $resolver = new Tasks\Migrate\Resolver($database); return new Tasks\Migrate\Migrator($resolver, $database); }); } /** * The bundle task is responsible for the installation of bundles * and their dependencies. It utilizes the bundles API to get the * meta-data for the available bundles. */ if (!IoC::registered('task: bundle')) { IoC::register('task: bundle', function () { $repository = IoC::resolve('bundle.repository'); return new Tasks\Bundle\Bundler($repository); }); } /** * The key task is responsible for generating a secure, random * key for use by the application when encrypting strings or * setting the hash values on cookie signatures. */ if (!IoC::registered('task: key')) { IoC::singleton('task: key', function () { return new Tasks\Key(); }); } /** * The session task is responsible for performing tasks related
if (IoC::registered('doctrine::cache.provider')) { $cache = IoC::resolve('doctrine::cache.provider'); } else { $cache = new Doctrine\Common\Cache\ArrayCache(); } /** * Register the cache provider with the Doctrine configuration. */ $config = new Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); /** * Resolve and register the meta-data driver. */ if (IoC::registered('doctrine::metadata.driver')) { $driverImpl = IoC::resolve('doctrine::metadata.driver', array($config)); } else { $driverImpl = $config->newDefaultAnnotationDriver(Config::get('doctrine::config.models')); } $config->setMetadataDriverImpl($driverImpl); /** * Register the proxy configuration with Doctrine. */ $config->setProxyDir(Config::get('doctrine::config.proxy.directory')); $config->setProxyNamespace(Config::get('doctrine::config.proxy.namespace')); $config->setAutoGenerateProxyClasses(Config::get('doctrine::config.proxy.auto_generate')); /** * Register an EntityManager in the IoC container as an instance. */ $em = EntityManager::create(array('pdo' => Laravel\Database::connection()->pdo), $config); IoC::instance('doctrine::manager', $em);
/** * Send a new message using a view. * * @param string|array $view * @param array $data * @param Closure|string $callback * @return void */ public static function send($view, array $data, $callback) { //$mailer = new static; $mailer = IoC::resolve('mailer'); return $mailer->sendMessage($view, $data, $callback); }
$mailer = new Mailer($swiftMailer); $from = Config::get('mail.from'); // If a "from" address is set, we will set it on the mailer so that all mail // messages sent by the applications will utilize the same "from" address // on each one, which makes the developer's life a lot more convenient. if (is_array($from) and isset($from['address'])) { $mailer->alwaysFrom($from['address'], $from['name']); } // toggle email debugging mode $pretend = Config::get('mail.pretend', false); $mailer->pretend($pretend); return $mailer; }); // Register a swift mailer in the IoC container IoC::register('swift.mailer', function () { $transport = IoC::resolve('swift.mailer.transport'); return Swift_Mailer::newInstance($transport); }); // Register a transporter in the IoC container IoC::register('swift.mailer.transport', function () { extract(Config::get('mail')); // $host = Config::get('mail.host'); // $port = Config::get('mail.port'); // $encryption = Config::get('mail.encryption'); // $username = Config::get('mail.username'); // $password = Config::get('mail.password'); // The Swift SMTP transport instance will allow us to use any SMTP backend // for delivering mail such as Sendgrid, Amazon SMS, or a custom server // a developer has available. We will just pass this configured host. $transport = Swift_SmtpTransport::newInstance($host, $port); if (isset($encryption)) {