addDetector() public method

There are several different formats and types of detectors that can be set. ### Environment value comparison An environment value comparison, compares a value fetched from env() to a known value the environment value is equality checked against the provided value. e.g addDetector('post', array('env' => 'REQUEST_METHOD', 'value' => 'POST')) ### Pattern value comparison Pattern value comparison allows you to compare a value fetched from env() to a regular expression. e.g addDetector('iphone', array('env' => 'HTTP_USER_AGENT', 'pattern' => '/iPhone/i')); ### Option based comparison Option based comparisons use a list of options to create a regular expression. Subsequent calls to add an already defined options detector will merge the options. e.g addDetector('mobile', array('env' => 'HTTP_USER_AGENT', 'options' => array('Fennec'))); ### Callback detectors Callback detectors allow you to provide a 'callback' type to handle the check. The callback will receive the request object as its only parameter. e.g addDetector('custom', array('callback' => array('SomeClass', 'somemethod'))); ### Request parameter detectors Allows for custom detectors on the request parameters. e.g addDetector('requested', array('param' => 'requested', 'value' => 1) You can also make parameter detectors that accept multiple values using the options key. This is useful when you want to check if a request parameter is in a list of options. addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'csv'))
public addDetector ( string $name, array $options ) : void
$name string The name of the detector.
$options array The options for the detector definition. See above.
return void
Esempio n. 1
0
 /**
  * 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;
 }
 /**
  * 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');
 }
 /**
  * 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');
     }));
 }
 /**
  * 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());
 }
 /**
  * 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');
 }
Esempio n. 6
0
 /**
  * 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());
 }
Esempio n. 7
0
 /**
  * リクエスト検出器を追加する
  *
  * @param CakeRequest $request リクエスト
  * @return void
  */
 public function addDetectors(CakeRequest $request)
 {
     foreach ($this->getDetectorConfigs() as $name => $callback) {
         $request->addDetector($name, $callback);
     }
 }
 /**
  * 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');
 }
 /**
  * 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);
     }
 }