Skip to content

Commit

Permalink
Merge pull request #1 from Arul-/dev
Browse files Browse the repository at this point in the history
Merging dev
  • Loading branch information
Arul- authored Dec 24, 2020
2 parents 1f5d855 + 7f0cad9 commit 0a6eb47
Show file tree
Hide file tree
Showing 125 changed files with 4,883 additions and 1,754 deletions.
45 changes: 5 additions & 40 deletions api/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@

use Auth\Client;
use Auth\Server;
use GraphQL\Type\Definition\Type as GraphQLType;
use improved\Authors as ImprovedAuthors;
use Luracast\Restler\Cache\HumanReadable;
use Luracast\Restler\Data\ErrorResponse;
use Luracast\Restler\Defaults;
use Luracast\Restler\Filters\RateLimiter;
use Luracast\Restler\GraphQL\GraphQL;
use Luracast\Restler\MediaTypes\Html;
use Luracast\Restler\MediaTypes\Json;
use Luracast\Restler\MediaTypes\Upload;
use Luracast\Restler\MediaTypes\Xml;
use Luracast\Restler\Middleware\SessionMiddleware;
use Luracast\Restler\Middleware\StaticFiles;
use Luracast\Restler\OpenApi3\Explorer;
use Luracast\Restler\Restler;
use Luracast\Restler\Router;
use Luracast\Restler\UI\Forms;
use Luracast\Restler\Utils\Text;
use ratelimited\Authors as RateLimitedAuthors;
use SomeVendor\v1\BMI as VendorBMI1;
use v1\BodyMassIndex as BMI1;
Expand All @@ -36,11 +32,6 @@
Defaults::$implementations[HttpClientInterface::class] = [SimpleHttpClient::class];
Router::setApiVersion(2);
Html::$template = 'blade'; //'handlebar'; //'twig'; //'php';
if (!Text::endsWith($_SERVER['SCRIPT_NAME'], 'index.php')) {
//when serving through apache or nginx, static files will be served direcly by apache / nginx
Restler::$middleware[] = new StaticFiles(BASE . '/' . 'public');
}
//Restler::$middleware[] = new SessionMiddleware('RESTLERSESSID', new ArrayCache(), [0, '', '', false, false]);
Restler::$middleware[] = new SessionMiddleware();

try {
Expand Down Expand Up @@ -84,7 +75,7 @@
Router::mapApiClasses(
[
//utility api for running behat tests
'-storage-' => Storage::class,
'examples/-storage-' => Storage::class,
//examples
'examples/_001_helloworld/say' => Say::class,
'examples/_002_minimal/math' => Math::class,
Expand Down Expand Up @@ -115,39 +106,13 @@
//Explorer
'explorer' => Explorer::class,
//GraphQL
GraphQL::class,
//GraphQL::class,
]
);
//
//---------------------------- GRAPHQL API ----------------------------
//
GraphQL::$queries['echo'] = [
'type' => GraphQLType::string(),
'args' => [
'message' => GraphQLType::nonNull(GraphQLType::string()),
],
'resolve' => function ($root, $args) {
return $root['prefix'] . $args['message'];
}
];
GraphQL::$mutations['sum'] = [
'type' => GraphQLType::int(),
'args' => [
'x' => ['type' => GraphQLType::int()],
'y' => ['type' => GraphQLType::int()],
],
'resolve' => function ($calc, $args) {
return $args['x'] + $args['y'];
},
];
GraphQL::mapApiClasses([
RateLimitedAuthors::class,
Say::class,
]);
GraphQL::addMethod('', new ReflectionMethod(Math::class, 'add'));
require __DIR__ . '/examples/_017_graphql/routes.php';

} catch (Throwable $t) {
die($t->getMessage());
die(json_encode((new ErrorResponse($t, true))->jsonSerialize(), JSON_PRETTY_PRINT));
}


Expand Down
23 changes: 9 additions & 14 deletions api/common/ReactHttpClient.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php


use Psr\Http\Message\ResponseInterface;
use React\EventLoop\LoopInterface;
use React\Http\Browser;
use React\HttpClient\Client;
use React\HttpClient\Response;

Expand All @@ -20,24 +22,17 @@ public static function request(
array $headers = [],
string $body = '',
callable $callback = null
)
{
) {
if (!static::$loop) {
throw new Error('Please call ReactHttpClient::setLoop before calling ReactHttpClient::request');
}
$client = new Client(static::$loop);
$browser = new Browser(static::$loop);
$headers['Content-Length'] = strlen($body);
$req = $client->request($method, $uri, $headers, '1.1');
$req->on('response', function (Response $response) use ($callback) {
$body = '';
$headers = $response->getHeaders();
$response->on('data', function (string $chunk) use (&$body) {
$body .= $chunk;
});
$response->on('end', function () use (&$body, $headers, $callback) {
$callback(null, new SimpleHttpResponse($body, $headers));
});
$req = $browser->request($method, $uri, $headers, $body);
$req->then(function (ResponseInterface $response) use ($callback) {
$callback(null, new SimpleHttpResponse((string)$response->getBody(), $response->getHeaders()));
}, function (Exception $exception) use ($callback) {
$callback($exception);
});
$req->end($body);
}
}
11 changes: 11 additions & 0 deletions api/examples/-storage-/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class_exists(ClassName::get('HttpClientInterface'));
*
* Removes the cache files to begin testing on a clean slate
*/
public function delete()
public function deleteAll()
{
$this->deleteCache();
$this->deletePackage();
Expand Down
19 changes: 19 additions & 0 deletions api/examples/-storage-/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Luracast\Restler\Defaults;
use Luracast\Restler\Restler;
use Luracast\Restler\Router;

define('BASE', __DIR__ . '/../../..');

require BASE . '/vendor/autoload.php';

Defaults::$cacheDirectory = BASE . '/api/common/store';
Defaults::$implementations[DataProviderInterface::class] = [SerializedFileDataProvider::class];
Defaults::$implementations[HttpClientInterface::class] = [SimpleHttpClient::class];

Router::mapApiClasses([
'' => Storage::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_001_helloworld/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
2 changes: 1 addition & 1 deletion api/examples/_001_helloworld/Say.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
class Say
{
function hello(string $to = 'world'): string
function hello($to = 'world'):string
{
return "Hello $to!";
}
Expand Down
12 changes: 12 additions & 0 deletions api/examples/_001_helloworld/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::mapApiClasses([
Say::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_002_minimal/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
15 changes: 12 additions & 3 deletions api/examples/_002_minimal/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ function add($n1 = 1, $n2 = 1)
*/
function multiply($n1, $n2)
{
return array(
return [
'result' => ($n1 * $n2)
);
];
}

/**
* @url GET sum/*
*/
function sum()
function _sum()
{
return array_sum(func_get_args());
}

/**
* @param int ...$numbers {@from path}
* @return int
*/
function sum2(int ...$numbers):int
{
return array_sum($numbers);
}
}
13 changes: 13 additions & 0 deletions api/examples/_002_minimal/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::mapApiClasses([
Math::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_003_multiformat/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
17 changes: 17 additions & 0 deletions api/examples/_003_multiformat/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php


use Luracast\Restler\MediaTypes\Json;
use Luracast\Restler\MediaTypes\Xml;
use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::setOverridingResponseMediaTypes(Json::class, Xml::class);

Router::mapApiClasses([
BMI::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_004_error_response/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
3 changes: 1 addition & 2 deletions api/examples/_004_error_response/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function format($number = null)
}

// let's format it as US currency
$m = new NumberFormatter("en-US", NumberFormatter::CURRENCY);
return $m->format($number);
return'$' . number_format($number, 2);
}
}
13 changes: 13 additions & 0 deletions api/examples/_004_error_response/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::mapApiClasses([
Currency::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_005_protected_api/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
15 changes: 15 additions & 0 deletions api/examples/_005_protected_api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::addAuthenticator(SimpleAuth::class);
Router::mapApiClasses([
'' => Simple::class,
Secured::class
]);

(new Restler())->handle();
11 changes: 11 additions & 0 deletions api/examples/_006_routing/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
13 changes: 13 additions & 0 deletions api/examples/_006_routing/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


use Luracast\Restler\Restler;
use Luracast\Restler\Router;

require __DIR__ . '/../../../vendor/autoload.php';

Router::mapApiClasses([
Api::class
]);

(new Restler())->handle();
Loading

0 comments on commit 0a6eb47

Please sign in to comment.