/** * Setup the IoC container instance. * * @param \Illuminate\Container\Container|null $container * @return void */ protected function setupContainer($container) { $this->container = $container ?: new Container(); if (!$this->container->bound('config')) { $this->container->instance('config', new Fluent()); } }
/** * @test */ public function it_should_not_affect_parent_container() { $this->parent->instance('a', 1); $this->local->instance('a', 2); $this->local->instance('b', 3); $this->assertTrue($this->parent->bound('a')); $this->assertEquals(1, $this->parent->make('a')); $this->assertFalse($this->parent->bound('b')); $this->assertEquals(2, $this->local->make('a')); $this->assertEquals(3, $this->local->make('b')); }
/** * Set up the tests */ public function setUp() { date_default_timezone_set('Europe/London'); // Create container $this->app = new Container(); $provider = new EventServiceProvider($this->app); $provider->register(); // Bind mocked instances into it $this->app['config'] = $this->mockConfig(); $this->app->instance('request', $this->mockRequest()); $this->app['translation.loader'] = Mockery::mock('Illuminate\\Translation\\FileLoader'); $this->app->instance('Illuminate\\Container\\Container', $this->app); }
/** * Set up the tests * * @return void */ public function setUp() { $this->app = new Container(); // Laravel classes --------------------------------------------- / $this->app->instance('path.base', '/src'); $this->app->instance('path', '/src/app'); $this->app->instance('path.public', '/src/public'); $this->app->instance('path.storage', '/src/app/storage'); $this->app['files'] = new Filesystem(); $this->app['config'] = $this->getConfig(); // Trucker classes ------------------------------------------- / $serviceProvider = new TruckerServiceProvider($this->app); $this->app = $serviceProvider->bindClasses($this->app); }
/** * Load the external config files specified by the command line option. */ protected function loadExternalConfig() { $config = new Repository(static::getDefaultConfig()); $filesystem = new Filesystem(); foreach ($this->getConfigFiles($filesystem) as $filename) { $this->output->writeln("<info>Reading config from <path>{$filename}</path></info>"); if ($filesystem->extension($filename) == 'php') { $configValues = $filesystem->getRequire($filename); } else { $configValues = Yaml::parse($filesystem->get($filename)); } $config->set(array_dot($configValues)); } $this->container->instance('config', $config); }
/** * Set the route collection instance. * * @param \Illuminate\Routing\RouteCollection $routes * @return void */ public function setRoutes(RouteCollection $routes) { foreach ($routes as $route) { $route->setRouter($this)->setContainer($this->container); } $this->routes = $routes; $this->container->instance('routes', $this->routes); }
/** * Dispatch a call to a registered macro * * @param string $method The macro's name * @param array $parameters The macro's arguments * * @return mixed */ public function toMacros($method, $parameters) { if (!$this->app['former']->hasMacro($method)) { return false; } // Get and format macro $callback = $this->app['former']->getMacro($method); if ($callback instanceof Closure) { return call_user_func_array($callback, $parameters); } elseif (!is_string($callback)) { return false; } // Get class and method list($class, $method) = explode('@', $callback); $this->app->instance('Illuminate\\Container\\Container', $this->app); return call_user_func_array(array($this->app->make($class), $method), $parameters); }
/** * Resolve a controller from the container. * * @param string $class * * @return \Illuminate\Routing\Controller */ protected function resolveController($class) { $controller = $this->container->make($class); if (!$this->container->bound($class)) { $this->container->instance($class, $controller); } return $controller; }
protected function createContainer() { $container = new Container(); $container->instance('array_iterator', new \ArrayIterator(range(1, 5))); $container->bind('error', function () { throw new \RuntimeException(); }); return new LaravelContainerAdapter($container); }
/** * Set up the tests * * @return void */ public function setUp() { $this->app = new Container(); // Laravel classes --------------------------------------------- / $this->app->instance('path.base', '/src'); $this->app->instance('path', '/src/app'); $this->app->instance('path.public', '/src/public'); $this->app->instance('path.storage', '/src/app/storage'); $this->app['files'] = new Filesystem(); $this->app['config'] = $this->getConfig(); $this->app['remote'] = $this->getRemote(); $this->app['artisan'] = $this->getArtisan(); $this->app['rocketeer.command'] = $this->getCommand(); // Rocketeer classes ------------------------------------------- / $serviceProvider = new RocketeerServiceProvider($this->app); $this->app = $serviceProvider->bindPaths($this->app); $this->app = $serviceProvider->bindCoreClasses($this->app); $this->app = $serviceProvider->bindClasses($this->app); $this->app = $serviceProvider->bindScm($this->app); }
protected function createApplicationContainer() { $this->app = new \Illuminate\Container\Container(); $this->app->singleton('config', function () { return new \Illuminate\Config\Repository(); }); $this->app->instance('log', $log = new \Illuminate\Log\Writer(new \Monolog\Logger('testing'))); $this->app->instance('Psr\\Log\\LoggerInterface', $log = new \Illuminate\Log\Writer(new \Monolog\Logger('testing'))); $this->registerConfigure(); $this->registerDatabase(); $this->registerCache(); $annotationConfiguration = new \Ytake\LaravelAspect\AnnotationConfiguration($this->app['config']->get('ytake-laravel-aop.annotation')); $annotationConfiguration->ignoredAnnotations(); $this->app->singleton('aspect.manager', function ($app) { return new \Ytake\LaravelAspect\AspectManager($app); }); $this->app->bind(\Illuminate\Container\Container::class, function () { return $this->app; }); \Illuminate\Container\Container::setInstance($this->app); }
/** * Get the routes controller instance. * * @return mixed */ public function getController() { if (!isset($this->action['uses']) || !is_string($this->action['uses'])) { return; } elseif (isset($this->controller)) { return $this->controller; } if (Str::contains($this->action['uses'], '@')) { list($controller, $this->method) = explode('@', $this->action['uses']); $this->container->instance($controller, $this->controller = $this->container->make($controller)); return $this->controller; } }
public function testDispatchShouldCallAfterResolvingIfCommandNotQueued() { $container = new Container(); $handler = m::mock('StdClass')->shouldIgnoreMissing(); $handler->shouldReceive('after')->once(); $container->instance('Handler', $handler); $dispatcher = new Dispatcher($container); $dispatcher->mapUsing(function () { return 'Handler@handle'; }); $dispatcher->dispatch(new BusDispatcherTestBasicCommand(), function ($handler) { $handler->after(); }); }
/** * {@inheritdoc} */ protected function setUp() { $app = new Container(); $app->instance('config', new Repository()); (new EventServiceProvider($app))->register(); (new AmiServiceProvider($app))->register(); $this->loop = $app[LoopInterface::class]; $this->loop->nextTick(function () { if (!$this->running) { $this->loop->stop(); } }); $this->stream = $app[Stream::class]; $this->events = $app['events']; $this->app = $app; }
/** * Bind paths to the configuration files * * @return void */ protected function bindConfiguration() { $path = $this->getBasePath(); $logs = $this->getStoragePath(); // Prepare the paths to bind $paths = array('config' => '.rocketeer', 'events' => '.rocketeer/events', 'tasks' => '.rocketeer/tasks', 'logs' => $logs . '/logs'); foreach ($paths as $key => $file) { $filename = $path . $file; // Check whether we provided a file or folder if (!is_dir($filename) and file_exists($filename . '.php')) { $filename .= '.php'; } // Use configuration in current folder if none found $realpath = realpath('.') . '/' . $file; if (!file_exists($filename) and file_exists($realpath)) { $filename = $realpath; } $this->app->instance('path.rocketeer.' . $key, $filename); } }
/** * Acts as a router that redirects methods to all of Form classes * * @param string $method The method called * @param array $parameters An array of parameters * * @return mixed */ public function __call($method, $parameters) { // Dispatch to Form\Elements // Explicitly check false since closeGroup() may return an empty string if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { return $element; } // Dispatch to Form\Form if ($form = $this->dispatch->toForm($method, $parameters)) { $this->app->instance('form.form', $form); return $this->app['form.form']; } // Dispatch to Form\Group if ($group = $this->dispatch->toGroup($method, $parameters)) { return $group; } // Dispatch to Form\Actions if ($actions = $this->dispatch->toActions($method, $parameters)) { return $actions; } // Dispatch to macros if ($macro = $this->dispatch->toMacros($method, $parameters)) { return $macro; } // Checking for any supplementary classes $classes = explode('_', $method); $method = array_pop($classes); // Dispatch to the different Form\Fields $framework = isset($this->app['form.form.framework']) ? $this->app['form.form.framework'] : $this->app['form.framework']; $field = $this->dispatch->toFields($method, $parameters); if ($field instanceof Field) { $field = $framework->getFieldClasses($field, $classes); } // Else bind field $this->app->instance('form.field', $field); return $this->app['form.field']; }
/** * Make a new controller instance through the container. * * @return \Illuminate\Routing\Controller|\Laravel\Lumen\Routing\Controller */ protected function makeControllerInstance() { list($this->controllerClass, $this->controllerMethod) = explode('@', $this->action['uses']); $this->container->instance($this->controllerClass, $this->controller = $this->container->make($this->controllerClass)); return $this->controller; }
/** * Set the raw adapter routes. * * @param array $routes * * @return void */ public function setAdapterRoutes(array $routes) { $this->adapter->setRoutes($routes); $this->container->instance('api.routes', $this->getRoutes()); }
/** * Replace the request instance with the previous request instance. * * @return void */ protected function replaceRequestInstance() { array_pop($this->requestStack); $this->container->instance('request', end($this->requestStack)); }
/** * Set the cache manager to be used by connections. * * @param \Illuminate\Cache\CacheManager $cache * @return void */ public function setCacheManager(CacheManager $cache) { $this->container->instance('cache', $cache); }
<?php /** * * Author: snake * Date: 15-7-22 * Time: 上午11:14 */ require __DIR__ . '/../vendor/autoload.php'; use Illuminate\Container\Container; // 用法 $container = new Container(); $container->instance('app', $container); // 构造函数自动注入 /** * @var Desktop $d */ //$d = $container->make('Desktop'); //$d->description(); // 绑定 // 把一个类起一个重命名 //$container->bind('SnakeBox',function(){ // return new Box(); //}); // ///** // * @var Box $b // */ //$b = $container->make('SnakeBox'); //$b->description(); // 别名注入
/** * Associate an abstract with a concrete object. * * @param $abstract * @param $instance */ public function instance($abstract, $instance) { static::$container->instance($abstract, $instance); }
/** * Replace the request instance with the previous request instance. * * @return void */ protected function replaceRequestInstance() { array_pop($this->requestStack); $this->container->instance('request', end($this->requestStack)); $this->router->setCurrentRequest($this->container['request']); }
/** * Register an existing instance as shared in the container. * * @param string $abstract * @param mixed $instance */ public function instance($abstract, $instance) { $this->delegate->instance($abstract, $instance); }