本文整理汇总了PHP中Symfony\Component\HttpFoundation\Request::setFactory方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::setFactory方法的具体用法?PHP Request::setFactory怎么用?PHP Request::setFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpFoundation\Request
的用法示例。
在下文中一共展示了Request::setFactory方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: configureFactory
/**
* @param string $type
*/
private static function configureFactory($type)
{
if (version_compare(Kernel::VERSION, '2.5', '<')) {
// nothing to configure as Request::setFactory require SF > 2.5
return;
}
if (!in_array($type, array('host_with_path', 'host_with_path_by_locale'))) {
return;
}
Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
return new SiteRequest($query, $request, $attributes, $cookies, $files, $server, $content);
});
}
示例2: testTrustedHosts
/**
* Tests hostname validation with settings.
*
* @covers ::setupTrustedHosts
* @dataProvider providerTestTrustedHosts
*/
public function testTrustedHosts($host, $server_name, $message, $expected = FALSE)
{
$request = new Request();
$trusted_host_patterns = ['^example\\.com$', '^.+\\.example\\.com$', '^example\\.org', '^.+\\.example\\.org'];
if (!empty($host)) {
$request->headers->set('HOST', $host);
}
$request->server->set('SERVER_NAME', $server_name);
$method = new \ReflectionMethod('Drupal\\Core\\DrupalKernel', 'setupTrustedHosts');
$method->setAccessible(TRUE);
$valid_host = $method->invoke(null, $request, $trusted_host_patterns);
$this->assertSame($expected, $valid_host, $message);
// Reset the trusted hosts because it is statically stored on the request.
$method->invoke(null, $request, []);
// Reset the request factory because it is statically stored on the request.
Request::setFactory(NULL);
}
示例3: testFactory
public function testFactory()
{
Request::setFactory(function (array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
return new NewRequest();
});
$this->assertEquals('foo', Request::create('/')->getFoo());
Request::setFactory(null);
}
示例4: setupTrustedHosts
/**
* Sets up the lists of trusted HTTP Host headers.
*
* Since the HTTP Host header can be set by the user making the request, it
* is possible to create an attack vectors against a site by overriding this.
* Symfony provides a mechanism for creating a list of trusted Host values.
*
* Host patterns (as regular expressions) can be configured throught
* settings.php for multisite installations, sites using ServerAlias without
* canonical redirection, or configurations where the site responds to default
* requests. For example,
*
* @code
* $settings['trusted_host_patterns'] = array(
* '^example\.com$',
* '^*.example\.com$',
* );
* @endcode
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param array $host_patterns
* The array of trusted host patterns.
*
* @return boolean
* TRUE if the Host header is trusted, FALSE otherwise.
*
* @see https://www.drupal.org/node/1992030
* @see \Drupal\Core\Http\TrustedHostsRequestFactory
*/
protected static function setupTrustedHosts(Request $request, $host_patterns)
{
$request->setTrustedHosts($host_patterns);
// Get the host, which will validate the current request.
try {
$host = $request->getHost();
// Fake requests created through Request::create() without passing in the
// server variables from the main request have a default host of
// 'localhost'. If 'localhost' does not match any of the trusted host
// patterns these fake requests would fail the host verification. Instead,
// TrustedHostsRequestFactory makes sure to pass in the server variables
// from the main request.
$request_factory = new TrustedHostsRequestFactory($host);
Request::setFactory([$request_factory, 'createRequest']);
} catch (\UnexpectedValueException $e) {
return FALSE;
}
return TRUE;
}
示例5: tearDown
public function tearDown()
{
if ($this->hasFactory) {
Request::setFactory(null);
}
}