当前位置: 首页>>代码示例>>PHP>>正文


PHP ServiceLocator::set方法代码示例

本文整理汇总了PHP中ServiceLocator::set方法的典型用法代码示例。如果您正苦于以下问题:PHP ServiceLocator::set方法的具体用法?PHP ServiceLocator::set怎么用?PHP ServiceLocator::set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceLocator的用法示例。


在下文中一共展示了ServiceLocator::set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: run

 /**
  * @return mixed|null
  * @throws \RuntimeException
  * @throws \Exception
  */
 public function run()
 {
     $this->initialize();
     // default error handling
     if (!isset($this->callbacks['Application.Error'])) {
         $this->addFeature(new Feature\BasicErrorHandler());
     }
     /** @var $router Router */
     $router = $this->serviceLocator->get('Router');
     $this->trigger('Application.PreRoute');
     try {
         $routeMatch = $router->route();
         $this->serviceLocator->set('RouteMatch', $routeMatch, true);
     } catch (\Exception $e) {
         return $this->trigger('Application.Error', array('type' => self::ERROR_EXCEPTION, 'exception' => $e));
     }
     $this->trigger('Application.PostRoute');
     if ($routeMatch == null) {
         /** @var $router Router */
         $router = $this->serviceLocator->get('Router');
         $routeMatch = $router->getLastRouteMatch();
         if (!$routeMatch) {
             return $this->trigger('Application.Error', array('type' => self::ERROR_UNROUTABLE));
         }
     } elseif (!$routeMatch instanceof Router\RouteMatch) {
         throw new \InvalidArgumentException('Provided RouteMatch must be of type P\\Router\\RouteMatch');
     }
     $route = $routeMatch->getRoute();
     if (!$route instanceof Router\RouteInterface) {
         throw new \RuntimeException('Matched route must implement MiniP\\ApplicationRoute\\ApplicationRouteInterface');
     }
     $this->trigger('Application.PreDispatch');
     try {
         $routeSource = $router->getSource();
         $dispatchParams = $routeMatch->getParameters();
         if ($routeSource instanceof Router\HttpSource) {
             $dispatchParams['HttpUri'] = $routeSource['uri'];
             $dispatchParams['HttpMethod'] = $routeSource['method'];
         }
         $this->applicationState->pushScope('Application.Dispatch', $dispatchParams);
         try {
             $callable = get_callable($route->getDispatchable(), $this->applicationState);
         } catch (\Exception $e) {
             $this->applicationState->popScope();
             return $this->trigger('Application.Error', array('type' => self::ERROR_UNDISPATCHABLE));
         }
         $result = invoke($callable, $this->applicationState, null, false);
         $this->applicationState->setResult($result);
         $this->applicationState->popScope();
     } catch (\Exception $e) {
         $this->applicationState->popScope();
         return $this->trigger('Application.Error', array('type' => self::ERROR_EXCEPTION, 'exception' => $e));
     }
     $this->trigger('Application.PostDispatch');
 }
开发者ID:pframework,项目名称:p,代码行数:60,代码来源:Application.php

示例2: set

ini_set('display_errors', '1');
echo '<p style="background-color:#ccc; font-family:arial;">Service Locator</p>';
//~ require 03-dependeny-injection.php
//~ class Pizza
//~ {
//~ public function __construct ( CrustInterface $crust )
//~ {
//~ $this-> crust = $crust;
//~ }
//~ public function addTopping ( ToppingInterface $topping )
//~ {
//~ $this-> toppings [] = $topping;
//~ }
//~ }
class ServiceLocator
{
    protected $services = array();
    public function set($id, $className)
    {
        $this->services[$id] = new $className();
    }
    public function get($id)
    {
        return $this->services[$id];
    }
}
$s1 = new ServiceLocator();
$s1->set('wheat-crust', 'CrustWheatFlour');
$s1->set('salami', 'ToppingSalami');
$s1->set('cheese', 'ToppingCheese');
//~ $pizzaSalami new Pizza ($s1->get('wheat-crust'));
开发者ID:jawadmjn,项目名称:php-learn,代码行数:31,代码来源:04-service-locator.php

示例3: set

    }
    public function set($name, $val)
    {
        $this->dependencies[$name] = $val;
    }
}
class Router
{
    protected $request;
    protected $response;
    public function __construct(ServiceLocator $sl, $path)
    {
        $this->request = $sl->get('request');
        $this->response = $sl->get('response');
        $this->path = $path;
        // &hellip;
    }
}
// Create SL
$sl = new ServiceLocator();
// Tell DiC how to create dependencies
$sl->set('request', function () {
    return new Request();
});
$sl->set('response', function () {
    return new Response();
});
// Create a router, injecting the dependencies
$router = new Router($sl, '/hello');
echo '<pre>';
var_dump($router);
开发者ID:robinstaes,项目名称:ws2-sws-course-materials,代码行数:31,代码来源:di_04.php


注:本文中的ServiceLocator::set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。