本文整理汇总了PHP中CakeRequest::addDetector方法的典型用法代码示例。如果您正苦于以下问题:PHP CakeRequest::addDetector方法的具体用法?PHP CakeRequest::addDetector怎么用?PHP CakeRequest::addDetector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CakeRequest
的用法示例。
在下文中一共展示了CakeRequest::addDetector方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _apiRequest
/**
* Helper method to create an test API request (with the appropriate detector)
*/
protected function _apiRequest($params)
{
$request = new CakeRequest();
$request->addParams($params);
$request->addDetector('api', array('callback' => array('CroogoRouter', 'isApiRequest')));
return $request;
}
示例2: _mockClasses
/**
* Helper method to generate and mock all the required
* classes
*
* `$hasField` is a field => bool array with what
* fields should exist according to 'hasField' model check
*
* @param array $hasField
* @return array
*/
protected function _mockClasses($hasField = array())
{
$CrudSubject = new CrudSubject();
$Crud = $this->CrudMock->disableOriginalConstructor()->setMethods(array('action'))->getMock();
$Model = $this->ModelMock->setConstructorArgs(array(array('table' => 'models', 'name' => 'Model', 'ds' => 'test')))->setMethods(array('hasField', 'getAssociated'))->getMock();
$Model->expects($this->any())->method('getAssociated')->will($this->returnValue(array('Sample' => array(), 'Demo' => array(), 'User' => array())));
$Model->alias = 'Model';
$Controller = $this->ControllerMock->disableOriginalConstructor()->setMethods(null)->getMock();
$Controller->Components = new StdClass();
$Request = new CakeRequest();
$Request->addDetector('api', array('callback' => function () {
return true;
}));
$Paginator = $this->PaginatorMock->disableOriginalConstructor()->setMethods(null)->getMock();
$Controller->Paginator = $Paginator;
$CrudSubject->set(array('crud' => $Crud, 'request' => $Request, 'controller' => $Controller, 'action' => 'add', 'model' => $Model, 'modelClass' => $Model->name, 'args' => array(), 'query' => array('fields' => null, 'contain' => null)));
$Action = $this->ActionMock->setConstructorArgs(array($CrudSubject))->setMethods(null)->getMock();
$Listener = new ApiFieldFilterListener($CrudSubject);
$Event = new CakeEvent('Test', $CrudSubject);
$Crud->expects($this->any())->method('action')->will($this->returnValue($Action));
$i = 0;
foreach ($hasField as $field => $has) {
$Model->expects($this->at($i))->method('hasField')->with($field)->will($this->returnValue($has));
$i++;
}
return compact('Crud', 'Model', 'Controller', 'Paginator', 'Request', 'CrudSubject', 'Listener', 'Action', 'Event');
}
示例3: configureRequestDetectors
/**
* Configure detectors for API requests
*
* Add detectors for ->is('api') and ->is('json') on CakeRequest
*
* @return void
*/
protected function configureRequestDetectors()
{
// Add detector for json
$this->request->addDetector('json', array('callback' => function (CakeRequest $request) {
// The sure solution is to check if the extension is "json"
if (isset($request->params['ext']) && $request->params['ext'] === 'json') {
return true;
}
// Or try to sniff out the accept header
return $request->accepts('application/json');
}));
// Generic API check
$this->request->addDetector('api', array('callback' => function (CakeRequest $request) {
// Currently only checks if a request is JSON - but allows us to easily add other request formats
return $request->is('json');
}));
}
示例4: testAddDetector
/**
* Test adding detectors and having them work.
*
* @return void
*/
public function testAddDetector()
{
$request = new CakeRequest('some/path');
$request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
$_SERVER['TEST_VAR'] = 'something';
$this->assertTrue($request->is('compare'), 'Value match failed.');
$_SERVER['TEST_VAR'] = 'wrong';
$this->assertFalse($request->is('compare'), 'Value mis-match failed.');
$request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
$_SERVER['TEST_VAR'] = 'foo';
$this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
$this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
$this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
$_SERVER['TEST_VAR'] = 'not foo';
$this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
$this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
$this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
$request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
$_SERVER['TEST_VAR'] = 'banana';
$this->assertTrue($request->isBanana());
$_SERVER['TEST_VAR'] = 'wrong value';
$this->assertFalse($request->isBanana());
$request->addDetector('mobile', array('options' => array('Imagination')));
$_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
$this->assertTrue($request->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
$this->assertTrue($request->isMobile());
$request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
$request->addDetector('index', array('param' => 'action', 'value' => 'index'));
$request->params['action'] = 'index';
$this->assertTrue($request->isIndex());
$request->params['action'] = 'add';
$this->assertFalse($request->isIndex());
$request->return = true;
$this->assertTrue($request->isCallMe());
$request->return = false;
$this->assertFalse($request->isCallMe());
$request->addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
$request->params['ext'] = 'pdf';
$this->assertTrue($request->is('extension'));
$request->params['ext'] = 'exe';
$this->assertFalse($request->isExtension());
}
示例5: testHeadHandled
/**
* testHeadHandled
*
* Simulate app code having handled the head request appropriately
*
* @return void
*/
public function testHeadHandled()
{
$filter = new HttpMethodFilter();
$response = $this->getMock('CakeResponse', array('_sendHeader'));
$response->header('Content-length', 123);
$request = new CakeRequest('controller/action/1');
$request->addDetector('head', array('callback' => function () {
return true;
}));
$event = new CakeEvent('HttpMethodFilterTest', $this, compact('request', 'response'));
$this->assertSame($response, $filter->afterDispatch($event), 'The HttpMethod filter should return a response');
$expected = array('Content-length' => '123');
$this->assertSame($expected, $response->header(), 'The content header should be set');
$this->assertSame('', $response->body(), 'The body should remain empty');
}
示例6: testAddDetector
/**
* test adding detectors and having them work.
*
* @return void
*/
public function testAddDetector()
{
$request = new CakeRequest('some/path');
$request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
$_SERVER['TEST_VAR'] = 'something';
$this->assertTrue($request->is('compare'), 'Value match failed.');
$_SERVER['TEST_VAR'] = 'wrong';
$this->assertFalse($request->is('compare'), 'Value mis-match failed.');
$request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
$_SERVER['TEST_VAR'] = 'banana';
$this->assertTrue($request->isBanana());
$_SERVER['TEST_VAR'] = 'wrong value';
$this->assertFalse($request->isBanana());
$request->addDetector('mobile', array('options' => array('Imagination')));
$_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
$this->assertTrue($request->isMobile());
$_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
$this->assertTrue($request->isMobile());
$request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback')));
$request->addDetector('index', array('param' => 'action', 'value' => 'index'));
$request->params['action'] = 'index';
$this->assertTrue($request->isIndex());
$request->params['action'] = 'add';
$this->assertFalse($request->isIndex());
$request->return = true;
$this->assertTrue($request->isCallMe());
$request->return = false;
$this->assertFalse($request->isCallMe());
}
示例7: addDetectors
/**
* リクエスト検出器を追加する
*
* @param CakeRequest $request リクエスト
* @return void
*/
public function addDetectors(CakeRequest $request)
{
foreach ($this->getDetectorConfigs() as $name => $callback) {
$request->addDetector($name, $callback);
}
}
示例8: testFlashMessageSupressed
/**
* testFlashMessageSupressed
*
* The API listener should suppress flash messages
* if the request is "API"
*
* @return void
*/
public function testFlashMessageSupressed()
{
$Request = new CakeRequest();
$Request->addDetector('api', array('callback' => function () {
return true;
}));
$subject = new CrudSubject(array('request' => $Request));
$apiListener = new ApiListener($subject);
$event = new CakeEvent('Crud.setFlash', $subject);
$apiListener->setFlash($event);
$stopped = $event->isStopped();
$this->assertTrue($stopped, 'Set flash event is expected to be stopped');
}
示例9: addDetectorsToRequest
/**
* add an array of detectors to the current CakeRequest
*
* @param $request CakeRequest
* @param $detectors Array. Keys are the detector names. Values will be the detector value
* @return void
*/
public function addDetectorsToRequest(CakeRequest $request, $detectors = array())
{
foreach ($detectors as $detectorName => $detector) {
$request->addDetector($detectorName, $detector);
}
}