Example #1
0
 /**
  * @return Session
  */
 private static function session() : Session
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new Session();
     return DI::set(__METHOD__, null, $item);
 }
Example #2
0
 /**
  * @return Dispatcher
  */
 private static function dispatcher() : Dispatcher
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new Dispatcher();
     return DI::set(__METHOD__, null, $item);
 }
Example #3
0
 /**
  * @return Logger
  */
 private static function logger() : Logger
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new Logger();
     return DI::set(__METHOD__, null, $item);
 }
Example #4
0
 /**
  * @return Dispatcher
  */
 public function instanceDispatcher() : Dispatcher
 {
     if ($return = DI::get(get_class($this), spl_object_hash($this))) {
         return $return;
     }
     $item = new Dispatcher();
     return DI::set(get_class($this), spl_object_hash($this), $item);
 }
Example #5
0
 /**
  * @return ServerResponse
  */
 protected static function response() : ServerResponse
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new ServerResponse();
     return DI::set(__METHOD__, null, $item);
 }
Example #6
0
 /**
  * @return Translator
  */
 protected static function translator() : Translator
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new Translator();
     return DI::set(__METHOD__, null, $item);
 }
Example #7
0
 /**
  * @return Router
  */
 protected static function router() : Router
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $item = new Router();
     return DI::set(__METHOD__, null, $item);
 }
Example #8
0
 /**
  * @param string $name
  *
  * @return Cache
  */
 private static function cache(string $name = null) : Cache
 {
     if ($return = DI::get(__METHOD__, $name)) {
         return $return;
     }
     $config = DI::config()->get('cache/' . ($name ?: 'default'));
     $item = new Cache($config);
     return DI::set(__METHOD__, $name, $item);
 }
Example #9
0
 /**
  * @param string $name
  *
  * @return Cache
  */
 private static function httpClient(string $name = null) : Cache
 {
     if ($return = DI::get(__METHOD__, $name)) {
         return $return;
     }
     $config = DI::config()->get('httpclient/' . $name);
     if (is_callable($config)) {
         $item = $config();
     } else {
         $item = new HttpClient();
         $item->setBaseUri($config);
     }
     return DI::set(__METHOD__, $name, $item);
 }
Example #10
0
 /**
  * @param string $name
  *
  * @return \Swift_Mailer
  */
 private static function mailer(string $name = null) : \Swift_Mailer
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $config = DI::config()->getIfExists('email/' . ($name ?: 'default'));
     if (!$config) {
         $transport = \Swift_MailTransport::newInstance();
         $return = \Swift_Mailer::newInstance($transport);
     } elseif (is_callable($config)) {
         $return = $config();
     } else {
         $uri = new Uri($config);
         switch ($uri->getScheme()) {
             case 'smtp':
                 $transport = \Swift_SmtpTransport::newInstance($uri->getHost(), $uri->getPort());
                 if ($uri->getUser()) {
                     $transport->setUsername($uri->getUser());
                 }
                 if ($uri->getPassword()) {
                     $transport->setPassword($uri->getPassword());
                 }
                 if ($uri->getQuery('auth')) {
                     $transport->setAuthMode($uri->getQuery('auth'));
                 }
                 if ($uri->getQuery('encryption')) {
                     $transport->setEncryption($uri->getQuery('encryption'));
                 }
                 break;
             case 'echo':
                 $transport = EchoTransport::newInstance();
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf("Undefined email mailer type '%s'", $uri->getScheme()));
                 break;
         }
         $return = \Swift_Mailer::newInstance($transport);
         if ($uri->getQuery('plugins')) {
             foreach ($uri->getQuery('plugins') as $plugin) {
                 $return->registerPlugin(new $plugin());
             }
         }
     }
     return DI::set(__METHOD__, $name, $return);
 }