本文整理汇总了PHP中Slim\Environment::getInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP Environment::getInstance方法的具体用法?PHP Environment::getInstance怎么用?PHP Environment::getInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Environment
的用法示例。
在下文中一共展示了Environment::getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
private function request($method, $path, $data = array(), $optionalHeaders = array())
{
// Capture STDOUT
ob_start();
$options = array('REQUEST_METHOD' => strtoupper($method), 'PATH_INFO' => $path, 'SERVER_NAME' => 'local.dev');
if ($method === 'get') {
$options['QUERY_STRING'] = http_build_query($data);
} elseif (is_array($data)) {
$options['slim.input'] = http_build_query($data);
} else {
$options['slim.input'] = $data;
}
// Prepare a mock environment
Slim\Environment::mock(array_merge($options, $optionalHeaders));
$env = Slim\Environment::getInstance();
$this->app->router = new NoCacheRouter($this->app->router);
$this->app->request = new Slim\Http\Request($env);
// Custom headers
$this->app->request->headers = new Slim\Http\Headers($env);
$this->app->response = new Slim\Http\Response();
// Establish some useful references to the slim app properties
$this->request = $this->app->request();
$this->response = $this->app->response();
// Execute our app
$this->app->run();
// Return the application output. Also available in `response->body()`
return ob_get_clean();
}
示例2: setUrl
protected function setUrl($path, $params = '', $config = array())
{
Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => $path, 'QUERY_STRING' => $params, 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim'));
$this->env = Environment::getInstance();
$this->req = new Request($this->env);
$this->res = new Response();
$this->app = new Slim(array_merge(array('controller.class_prefix' => '\\SlimController\\Tests\\Fixtures\\Controller', 'controller.class_suffix' => 'Controller', 'controller.method_suffix' => 'Action', 'controller.template_suffix' => 'php', 'templates.path' => __DIR__ . '/Fixtures/templates'), $config));
}
示例3: testBestFormatWithPriorities
public function testBestFormatWithPriorities()
{
\Slim\Environment::mock(array('ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'negotiation.priorities' => array('xml')));
$s = new \Slim\Slim();
$s->add(new ContentNegotiation());
$s->run();
$env = \Slim\Environment::getInstance();
$bestFormat = $env['request.best_format'];
$this->assertNotNull($bestFormat);
$this->assertEquals('xml', $bestFormat);
}
示例4: call
public function call()
{
$basePath = $this->app->request->getScriptName();
$virtualPath = $this->app->request->getPathInfo();
$pathChunk = explode("/", $virtualPath);
if (count($pathChunk) > 1 && in_array($pathChunk[1], $this->availableLangs)) {
$this->app->locale->set($pathChunk[1]);
$pathChunk = array_slice($pathChunk, 2);
}
$_SERVER['REQUEST_URI'] = $basePath . "/" . implode("/", $pathChunk);
$newEnv = \Slim\Environment::getInstance(true);
$newRequest = new \Slim\Http\Request($newEnv);
$this->app->container->request = $newRequest;
$this->next->call();
}
示例5: render
/**
* Our (marginally) smarter render function. The layout can be changed by
* setting a "_layout" data key to the new layout file, or boolean false to
* disable layout.
*
* @param string $template template name
* @return string rendered template
*/
public function render($template)
{
$env = \Slim\Environment::getInstance();
$this->setData('_base', $env['SCRIPT_NAME']);
$data = $this->getData();
if (isset($data['_layout'])) {
$layout = $data['_layout'];
} else {
$layout = $this->layout;
}
$content = parent::render($template);
if ($layout !== false) {
$this->setData($data);
$this->setData('content', $content);
$content = parent::render($layout);
}
return $content;
}
示例6: custom
/**
* performs a custom request
*
* @param string $method the request type (POST, DELETE, PUT, GET, ...)
* @param string $target the taget URL
* @param string $header an array with header informations
* @param string $content the request content/body
*
* @return an array with the request result (status, header, content)
* - ['headers'] = an array of header informations e.g. ['headers']['Content-Type']
* - ['content'] = the response content
* - ['status'] = the status code e.g. 200,201,404,409,...
*/
public static function custom($method, $target, $header, $content, $authbool = true, $sessiondelete = false)
{
$begin = microtime(true);
$done = false;
if (!CConfig::$onload && strpos($target, 'http://localhost/') === 0 && file_exists(dirname(__FILE__) . '/request_cconfig.json')) {
if (self::$components === null) {
self::$components = CConfig::loadStaticConfig('', '', dirname(__FILE__), 'request_cconfig.json');
}
$coms = self::$components->getLinks();
if ($coms != null) {
if (!is_array($coms)) {
$coms = array($coms);
}
$e = strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/'));
$f = substr(str_replace("\\", "/", dirname(__FILE__)), $e);
$g = substr(str_replace("\\", "/", $_SERVER['SCRIPT_FILENAME']), $e);
$a = 0;
for (; $a < strlen($g) && $a < strlen($f) && $f[$a] == $g[$a]; $a++) {
}
$h = substr(str_replace("\\", "/", $_SERVER['PHP_SELF']), 0, $a - 1);
foreach ($coms as $com) {
if ($com->getPrefix() === null || $com->getLocalPath() == null || $com->getClassFile() == null || $com->getClassName() == null) {
Logger::Log('nodata: ' . $method . ' ' . $target, LogLevel::DEBUG, false, dirname(__FILE__) . '/../calls.log');
continue;
}
$url = 'http://localhost' . $h . '/' . $com->getLocalPath();
if (strpos($target, $url . '/') === 0) {
$result = array();
$tar = dirname(__FILE__) . '/../' . $com->getLocalPath() . '/' . $com->getClassFile();
$tar = str_replace("\\", "/", $tar);
if (!file_exists($tar)) {
continue;
}
$add = substr($target, strlen($url));
$sid = CacheManager::getNextSid();
CacheManager::getTree($sid, $target, $method);
$cachedData = CacheManager::getCachedDataByURL($sid, $target, $method);
if ($cachedData !== null) {
$result['content'] = $cachedData->content;
$result['status'] = $cachedData->status;
///Logger::Log('out>> '.$method.' '.$target, LogLevel::DEBUG, false, dirname(__FILE__) . '/../calls.log');
CacheManager::cacheData($sid, $com->getTargetName(), $target, $result['content'], $result['status'], $method);
} else {
$args = array('REQUEST_METHOD' => $method, 'PATH_INFO' => $add, 'slim.input' => $content);
if (isset($_SERVER['HTTP_SESSION'])) {
$args['HTTP_SESSION'] = $_SERVER['HTTP_SESSION'];
}
if (isset($_SERVER['HTTP_USER'])) {
$args['HTTP_USER'] = $_SERVER['HTTP_USER'];
}
if ($authbool) {
if (isset($_SESSION['UID'])) {
$args['HTTP_USER'] = $_SESSION['UID'];
$_SERVER['HTTP_USER'] = $_SESSION['UID'];
}
if (isset($_SESSION['SESSION'])) {
$args['HTTP_SESSION'] = $_SESSION['SESSION'];
$_SERVER['HTTP_SESSION'] = $_SESSION['SESSION'];
}
if ($sessiondelete) {
if (isset($_SERVER['REQUEST_TIME'])) {
$args['HTTP_DATE'] = $_SERVER['REQUEST_TIME'];
$_SERVER['HTTP_DATE'] = $_SERVER['REQUEST_TIME'];
}
} else {
if (isset($_SESSION['LASTACTIVE'])) {
$args['HTTP_DATE'] = $_SESSION['LASTACTIVE'];
$_SERVER['HTTP_DATE'] = $_SESSION['LASTACTIVE'];
}
}
}
if (isset($_SERVER['HTTP_DATE'])) {
$args['HTTP_DATE'] = $_SERVER['HTTP_DATE'];
}
$oldArgs = array('REQUEST_METHOD' => \Slim\Environment::getInstance()->offsetGet('REQUEST_METHOD'), 'PATH_INFO' => \Slim\Environment::getInstance()->offsetGet('PATH_INFO'), 'slim.input' => \Slim\Environment::getInstance()->offsetGet('slim.input'), 'HTTP_DATE' => \Slim\Environment::getInstance()->offsetGet('HTTP_DATE'), 'HTTP_USER' => \Slim\Environment::getInstance()->offsetGet('HTTP_USER'), 'HTTP_SESSION' => \Slim\Environment::getInstance()->offsetGet('HTTP_SESSION'), 'REQUEST_TIME' => \Slim\Environment::getInstance()->offsetGet('REQUEST_TIME'));
$oldRequestURI = $_SERVER['REQUEST_URI'];
$oldScriptName = $_SERVER['SCRIPT_NAME'];
///$oldRedirectURL = $_SERVER['REDIRECT_URL'];
///echo "old: ".$_SERVER['REQUEST_URI']."\n";
$_SERVER['REQUEST_URI'] = substr($target, strlen('http://localhost/') - 1);
//$tar.$add;
///$_SERVER['REDIRECT_URL']= substr($target,strlen('http://localhost/')-1);
///echo "mein: ".substr($target,strlen('http://localhost/')-1)."\n";
$_SERVER['SCRIPT_NAME'] = $h . '/' . $com->getLocalPath() . '/' . $com->getClassFile();
//$tar;
$_SERVER['QUERY_STRING'] = '';
$_SERVER['REQUEST_METHOD'] = $method;
//.........这里部分代码省略.........
示例7: __construct
public function __construct()
{
$this->environment = \Slim\Environment::getInstance();
}
示例8: __construct
public function __construct()
{
parent::__construct(\Slim\Environment::getInstance());
}
示例9: parseQueryString
protected function parseQueryString()
{
// get the query strong from the environment
$queryString = Environment::getInstance()->offsetGet('QUERY_STRING');
// get the parser
$parser = $this->getParser();
try {
// stash the parseResult;
$this->parseResult = $parser->parse($queryString);
} catch (Parser\ParserException $e) {
$this->badRequest('Parse error: ' . $e->getMessage());
}
}
示例10: validate
public function validate()
{
$environment = \Slim\Environment::getInstance();
return !in_array($environment["REQUEST_METHOD"], $this->options["passthru"]);
}
示例11: count
*/
$page_cnt = count($posts) / $count_per_page;
$remain = count($posts) % $count_per_page;
if ($remain > 0) {
$page_cnt++;
}
for ($i = 1; $i <= $page_cnt; $i++) {
if (!file_exists($out_path . '/pages/' . $i)) {
mkdir($out_path . '/pages/' . $i, 0777, true);
if ($debug) {
echo 'created directory ' . $out_path . '/pages/' . $i . "\n";
}
}
$req_uri = '/pages/' . $i . '/index.html';
$_SERVER['REQUEST_URI'] = $script_name . $req_uri;
$app->environment = \Slim\Environment::getInstance(true);
ob_start();
require $pub_path . '/index.php';
$contents = ob_get_contents();
ob_end_clean();
file_put_contents($out_path . $req_uri, $contents);
if ($debug) {
echo 'wrote ' . $out_path . $req_uri . "\n";
}
}
/**
* copy php file
*/
$files = glob($pub_path . '/*');
foreach ($files as $file) {
if (!is_file($file)) {
示例12: __construct
public function __construct()
{
$env = \Slim\Environment::getInstance();
$this->request = new \Slim\Http\Request($env);
$this->routes = array();
}
示例13: testErrorResource
/**
* Test valid resource handle to php://stdErr
*/
public function testErrorResource()
{
$env = \Slim\Environment::getInstance(true);
$this->assertTrue(is_resource($env['slim.errors']));
}
示例14: app
/**
* @return webApplicationBase
*
* @throws webApplicationException
*/
protected function app()
{
/** @var Slim $slimOriginal */
static $app, $router;
if ($app === null) {
$app = $this->getBaseApplication();
//MPCMF_DEBUG && self::log()->addDebug('Before bindings call...');
$app->beforeBindings();
$app->processBindings();
$env = Environment::getInstance();
$env['slim.input'] = http_build_query($_POST);
$router = clone $app->slim()->router();
} else {
/** @var mpcmfWeb $app */
//MPCMF_DEBUG && self::log()->addDebug('Before bindings call...');
$app->beforeBindings();
$env = Environment::getInstance(true);
$env['slim.input'] = http_build_query($_POST);
$slim = $app->slim();
unset($slim->request, $slim->response, $slim->router, $slim->environment);
$slim->request = new Request($env);
$slim->response = new Response();
$slim->router = clone $router;
$slim->environment = $env;
}
return $app;
}
示例15: run
/**
* Führt das Modul entsprechend der Commands.json und Component.json Definitionen aus
*/
public function run()
{
// runs the CConfig
$com = new CConfig($this->_prefix, $this->_path, $this->_noInfo, $this->_noHelp);
// lädt die Konfiguration des Moduls
if ($com->used()) {
return;
}
$conf = $com->loadConfig();
$this->_conf = $conf;
$this->_com = $com;
$commands = $com->commands(array(), true, true);
// multi Requests werden noch nicht unterstützt, das Model soll automatisch die Möglichkeit bieten,
// mehrere Anfragen mit einmal zu beantworten
////$commands[] = array('name' => 'postMultiGetRequest','method' => 'POST', 'path' => '/multiGetRequest', 'inputType' => 'Link', 'outputType' => '');
// Erstellt für jeden angebotenen Befehl einen Router
// Ein Router stellt einen erlaubten Aufruf dar (mit Methode und URI), sodass geprüft werden kann,
// welcher für die Beantwortung zuständig ist
$router = new \Slim\Router();
foreach ($commands as &$command) {
if (!isset($command['name'])) {
continue;
}
if (!isset($command['method'])) {
$command['method'] = 'GET';
}
if (!isset($command['callback'])) {
$command['callback'] = $command['name'];
}
if (!isset($command['seqInput'])) {
$command['seqInput'] = 'TRUE';
}
if (!isset($command['singleOutput'])) {
$command['singleOutput'] = 'FALSE';
}
if (!isset($command['placeholder'])) {
$command['placeholder'] = array();
}
// Methoden können durch Komma getrennt aufgelistet sein
$methods = explode(',', $command['method']);
foreach ($methods as $method) {
$route = new \Slim\Route($command['path'], array($this->_class, $command['callback']), false);
$route->via(strtoupper($method));
$route->setName($command['name']);
$router->map($route);
// wenn es ein GET Befehl ist, wird automatisch HEAD unterstützt
if (strtoupper($method) == 'GET') {
// erzeugt einen HEAD Router
$route = new \Slim\Route($command['path'], array($this->_class, $command['callback']), false);
$route->via('HEAD');
$route->setName($command['name']);
$router->map($route);
}
}
}
// hier wird die eingehende URI erzeugt
// Bsp.: /user/1
$scriptName = $_SERVER['SCRIPT_NAME'];
$requestUri = $_SERVER['REQUEST_URI'];
$path = str_replace('?' . (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''), '', substr_replace($requestUri, '', 0, strlen(strpos($requestUri, $scriptName) !== false ? $scriptName : str_replace('\\', '', dirname($scriptName)))));
// ermittelt den zuständigen Befehl
$matches = $router->getMatchedRoutes(strtoupper($_SERVER['REQUEST_METHOD']), $path);
if (count($matches) > 0) {
// mindestens ein zutreffender Befehl wurde gefunden (nimm den Ersten)
$matches = $matches[0];
// suche den zugehörigen $commands Eintrag
$selectedCommand = null;
foreach ($commands as $command) {
if ($command['name'] === $matches->getName()) {
$selectedCommand = $command;
break;
}
}
if ($selectedCommand == null) {
http_response_code(500);
return;
}
// lies die eingehenden PHP Daten
$rawInput = \Slim\Environment::getInstance()->offsetGet('slim.input');
if (!$rawInput) {
$rawInput = @file_get_contents('php://input');
}
// wir wollen wissen, ob die Eingabedaten als Liste rein kommen
$arr = true;
// wenn zu diesem Befehl ein inputType angegeben wurde, wird eine Type::decodeType() aufgerufen
if (isset($selectedCommand['inputType']) && trim($selectedCommand['inputType']) != '') {
$inputType = $selectedCommand['inputType'];
$rawInput = call_user_func_array('\\' . $inputType . '::decode' . $inputType, array($rawInput));
if (!is_array($rawInput)) {
// es ist keine Liste, also mach eine daraus (damit man sie einheitlich behandeln kann)
$rawInput = array($rawInput);
$arr = false;
}
}
if (strtoupper($selectedCommand['seqInput']) != 'TRUE') {
$arr = false;
}
//.........这里部分代码省略.........