/** * Test how stuff goes if a plugin returns an object that needs to be * matched not to the object itself, but to an interface implemented by * the object. */ public function testPluginInterfaceCallbackMatch() { $stubFoo = $this->getMockBuilder('fkooman\\Rest\\ServicePluginInterface')->setMockClassName('StubFoo')->getMock(); $stubFoo->expects($this->any())->method('execute')->will($this->returnValue('foo')); $stubPlugin = $this->getMockBuilder('fkooman\\Rest\\ServicePluginInterface')->setMockClassName('StubPlugin')->getMock(); $stubPlugin->expects($this->any())->method('execute')->will($this->returnValue($stubFoo)); $service = new Service(); $service->getPluginRegistry()->registerDefaultPlugin($stubPlugin); $service->get('/', function (Request $request, ServicePluginInterface $i) { return $i->execute($request, array()); }); $request = new Request(array('SERVER_NAME' => 'www.example.org', 'SERVER_PORT' => 80, 'QUERY_STRING' => '', 'REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET')); $response = $service->run($request); $this->assertSame(array('HTTP/1.1 200 OK', 'Content-Type: text/html;charset=UTF-8', 'Content-Length: 3', '', 'foo'), $response->toArray()); }
public function init(Service $service) { $service->get(sprintf('/%s', $this->routeName), function (Request $request) { return $this->routeName; }); }
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once dirname(__DIR__) . '/vendor/autoload.php'; use fkooman\Rest\Service; use fkooman\Http\Response; use fkooman\Http\Session; $session = new Session('foo'); $service = new Service(); $service->get('/', function () { $response = new Response(200, 'text/plain'); $response->setBody('Welcome!'); return $response; }); $service->get('/:key', function ($key) use($session) { $newCount = $session->has($key) ? $session->get($key) + 1 : 1; $session->set($key, $newCount); $response = new Response(200, 'text/plain'); $response->setBody(sprintf('count: %d', $newCount)); return $response; }); $service->run()->send();
public function init(Service $service) { $service->get('/foo', function () { return 'foo'; }); }
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once dirname(__DIR__) . '/vendor/autoload.php'; use fkooman\Http\JsonResponse; use fkooman\Rest\Service; use fkooman\Http\Exception\BadRequestException; $service = new Service(); $service->get('/hello/:str', function ($str) { $response = new JsonResponse(); $response->setBody(array('type' => 'GET', 'response' => sprintf('hello %s', $str))); return $response; }); $service->post('/hello/:str', function ($str) { if ('foo' === $str) { throw new BadRequestException('you cannot say "foo!"'); } $response = new JsonResponse(); $response->setBody(array('type' => 'POST', 'response' => sprintf('hello %s', $str))); return $response; }); $service->run()->send();
<?php /** * Copyright 2015 François Kooman <*****@*****.**>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once dirname(__DIR__) . '/vendor/autoload.php'; use fkooman\Rest\Service; $service = new Service(); $service->get('/', function () { return '<html><head><title>Test</title></head><body><form method="post"><input type="submit" value="Test"></form></body></html>'; }); $service->post('/', function () { return '<html><head><title>Test</title></head><body>OK</body></html>'; }); $service->run()->send();