本文整理匯總了PHP中Injector::give方法的典型用法代碼示例。如果您正苦於以下問題:PHP Injector::give方法的具體用法?PHP Injector::give怎麽用?PHP Injector::give使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Injector
的用法示例。
在下文中一共展示了Injector::give方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: process
/**
* Processes a route call, something like `stuff::thing' or just a function name
* or even a closure.
*/
protected function process($proto)
{
/* We're accepting different types of handler declarations. It can be
* anything PHP defines as a 'callable', or in the form class::method. */
$class = '';
$method = '';
$call = $proto['call'];
$matches = $proto['params'];
if (is_string($call) && preg_match('/^.+::.+$/', trim($call))) {
list($class, $method) = explode('::', $call);
} else {
if (is_array($call)) {
$class = $call[0];
$method = $call[1];
} else {
if (is_callable($call)) {
$method = $call;
}
}
}
$response = null;
$this->modules->runMethod('preProcess', array('request' => $this->request, 'proto' => $proto, 'response' => $response));
if (!$class) {
// Just a function call (or a closure?). Less hooks obviously.
// Mounting system stuff into an object and generating the parameters.
$params = array_merge(array((object) array('modules' => $this->modules, 'server' => $this->server, 'request' => $this->request, 'sec' => Injector::give('Security'))), array_slice($matches, 1));
$response = call_user_func_array($method, $params);
} else {
if (class_exists($class)) {
$obj = new $class($this->modules, $this->server, $this->request, new Security());
if (method_exists($obj, 'preRequest')) {
$obj->preRequest();
}
if (method_exists($obj, $method)) {
$response = call_user_func_array(array($obj, $method), array_slice($matches, 1));
if (method_exists($obj, 'postRequest')) {
$response = $obj->postRequest($response);
}
} else {
throw new \BadMethodCallException("Method, {$method}, not supported.");
}
} else {
throw new NoHandlerException("Class, {$class}, not found.");
}
}
// Cleaning up the response...
if (gettype($response) == 'string') {
$response = Injector::give('Response', $response);
} else {
if ($response === null) {
$response = Injector::give('Response');
} else {
if (gettype($response) != 'object' || gettype($response) == 'object' && (get_class($response) != 'atlatl\\Response' && !is_subclass_of($response, 'atlatl\\Response'))) {
throw new IllegalResponseException('Unknown response.');
}
}
}
$this->modules->runMethod('postProcess', array('request' => $this->request, 'proto' => $proto, 'response' => $response));
return $response;
}
示例2: function
<?php
namespace atlatl;
// Core.
Injector::register('Core', function ($prefix = '') {
$server = Injector::give('Server', $_SERVER, $prefix);
$request = Injector::give('Request', $_GET, $_POST);
$mc = Injector::give('ModuleContainer', $server);
return new Core($prefix, $server, $request, $mc);
});
// Server
Injector::register('Server', function (array $data, $prefix = '') {
return new Server($data, $prefix);
});
// Request
Injector::register('Request', function (array $get, array $post, array $session = null, array $cookies = null) {
return new Request($get, $post, $session ?: array(), $cookies ?: array());
});
// ModuleContainer
Injector::register('ModuleContainer', function (Server $server) {
return new ModuleContainer($server);
});
// Response
Injector::register('Response', function ($body = '', $status_code = 200, $content_type = 'text/html; charset=UTF-8') {
return new Response($body, $status_code, $content_type);
});
// Security
Injector::register('Security', function () {
return new Security();
});