Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ut4 committed May 3, 2020
2 parents d59922a + f0b3dd2 commit c964fab
Show file tree
Hide file tree
Showing 53 changed files with 1,505 additions and 299 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
.vscode
/docs
/internal
vendor
composer.lock
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2019 ut4
Copyright 2020 ut4

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Pike

A tight set of classes that helps you write killer applications. Does not try to build everything from sratch, but uses existing, high-quality, bloat-free libraries instead.
A tight set of classes that helps you write killer applications. Does not try to build everything from scratch, but uses existing, high-quality, bloat-free libraries instead.

# Dependencies

- PHP ^7.1
- PHP ^7.2
- [altorouter/altorouter ^1.2](https://github.com/dannyvankooten/AltoRouter)
- [rdlowrey/auryn ^1.4](https://github.com/rdlowrey/auryn) (dependency injection)
- [phpmailer/phpmailer ^6.1](https://github.com/PHPMailer/PHPMailer) (password reset)
- [rdlowrey/auryn ^1.4](https://github.com/rdlowrey/auryn) (Dependency injection)

# Optional dependencies

- [phpmailer/phpmailer ^6.1](https://github.com/PHPMailer/PHPMailer) (Password reset)

# Documentation

[Suomeksi](https://ut4.github.io/pike/index.html).

# License

Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
"type": "library",
"license": "Apache-2.0",
"require": {
"php": "~7.1",
"php": "^7.2",
"ext-pdo_mysql": "*",
"ext-mbstring": "*",
"ext-ctype": "*",
"altorouter/altorouter": "^1.2",
"rdlowrey/auryn": "^1.4",
"phpmailer/phpmailer": "^6.1"
"rdlowrey/auryn": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "7.*"
"phpunit/phpunit": "8.*"
},
"suggest": {
"phpmailer/phpmailer": "To use Authenticator->requestPasswordReset()."
},
"autoload": {
"psr-4": {"Pike\\": "src/"}
},
"autoload-dev": {
"psr-4": { "Pike\\Tests\\": "tests/" }
},
"scripts": {
"test": "\"vendor/bin/phpunit\" --bootstrap ./tests/bootstrap.php"
}
Expand Down
3 changes: 0 additions & 3 deletions example/index.php

This file was deleted.

69 changes: 69 additions & 0 deletions examples/AuthorizingRoutes/src/MyAuthModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Me\AuthorizingRoutes;

use Pike\Auth\ACL;
use Auryn\Injector;

abstract class MyAuthModule {
private static $ctx;
/**
* @param \stdClass $ctx {\Pike\Router router}
*/
public static function init(\stdClass $ctx) {
$ctx->acl = new ACL;
$ctx->acl->setRules(self::makeMyAclRules());
//
$ctx->router->on('*', function ($req, $res, $next) use ($ctx) {
// Tämä tulisi normaalisti sessiosta ($ctx->auth->getIdentity()->role)
$userRole = LOGGED_IN_USER_ROLE;
// ks. Step 1
[$action, $resource] = explode(':', $req->routeInfo->myCtx);
if (!$ctx->acl->can($userRole, $action, $resource))
$res->status(403)->json(['err' => 'Not permitted']);
else
$next();
});
//
self::$ctx = $ctx;
}
/**
* @return \stdClass
*/
private static function makeMyAclRules(): \stdClass {
// Nämä tulisi normaalisti esim. tiedostosta tai tietokannasta.
$resources = (object) [
'products' => (object) [
'create' => 1 << 1,
'edit' => 1 << 2,
'comment' => 1 << 3,
],
'reviews' => (object) [
'post' => 1 << 1,
'approveOrReject' => 1 << 2,
]
];
$userPermissions = (object) [
ACL::ROLE_EDITOR => (object) [
'products' => ACL::makePermissions(['comment', 'edit'], $resources->products),
'reviews' => ACL::makePermissions('*', $resources->reviews),
],
ACL::ROLE_CONTRIBUTOR => (object) [
'products' => ACL::makePermissions(['comment'], $resources->products),
'reviews' => ACL::makePermissions(['post'], $resources->reviews),
]
];
return (object) [
'resources' => $resources,
'userPermissions' => $userPermissions
];
}
/**
* @param \Auryn\Injector $container
*/
public static function alterIoc(Injector $container) {
$container->share(self::$ctx->acl);
}
}
51 changes: 51 additions & 0 deletions examples/AuthorizingRoutes/src/Product/ProductController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Me\AuthorizingRoutes\Product;

use Pike\Response;

class ProductController {
/**
* POST /products
*
* @param \Pike\Response $res
*/
public function handleCreateProduct(Response $res): void {
// Validoi $req->body, ja insertoi data tietokantaan ...
//
if ('jokinEhto')
$res->json(['insertId' => 1]);
else
$res->status(500)->json(['err' => 'Foo']);
}
/**
* PUT /products/[i:productId]
*
* @param \Pike\Response $res
*/
public function handleEditProduct(Response $res): void {
// Validoi $req->body, päivitä data tietokantaan id:llä
// $req->params->productId ...
//
if ('jokinEhto')
$res->json(['ok' => 'ok']);
else
$res->status(500)->json(['err' => 'Foo']);
}
/**
* POST /products/[i:productId]/comment
*
* @param \Pike\Response $res
*/
public function handleAddComment(Response $res): void {
// Validoi $req->body, ja insertoi kommentti tietokantaan tuotteelle
// $req->params->productId ...
//
if ('jokinEhto')
$res->json(['ok' => 'ok']);
else
$res->status(500)->json(['err' => 'Foo']);
}
}
22 changes: 22 additions & 0 deletions examples/AuthorizingRoutes/src/Product/ProductModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Me\AuthorizingRoutes\Product;

abstract class ProductModule {
/**
* @param \stdClass $ctx {\Pike\Router router}
*/
public static function init(\stdClass $ctx) {
$ctx->router->map('POST', '/products',
[ProductController::class, 'handleCreateProduct', 'create:products']
);
$ctx->router->map('PUT', '/products/[i:productId]',
[ProductController::class, 'handleEditProduct', 'edit:products']
);
$ctx->router->map('POST', '/products/[i:productId]/comment',
[ProductController::class, 'handleAddComment', 'comment:products']
);
}
}
36 changes: 36 additions & 0 deletions examples/AuthorizingRoutes/src/Review/ReviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Me\AuthorizingRoutes\Review;

use Pike\Response;

class ReviewController {
/**
* POST /reviews
*
* @param \Pike\Response $res
*/
public function handleCreateReview(Response $res): void {
// Validoi $req->body, ja insertoi data tietokantaan ...
//
if ('jokinEhto')
$res->json(['insertId' => 1]);
else
$res->status(500)->json(['err' => 'Foo']);
}
/**
* PUT /reviews/[i:reviewId]/approve-or-reject
*
* @param \Pike\Response $res
*/
public function handleApproveOrRejectReview(Response $res): void {
// Validoi $req->body, ja päivitä review $req->params->reviewId tietokantaan ...
//
if ('jokinEhto')
$res->json(['ok' => 'ok']);
else
$res->status(500)->json(['err' => 'Foo']);
}
}
19 changes: 19 additions & 0 deletions examples/AuthorizingRoutes/src/Review/ReviewModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Me\AuthorizingRoutes\Review;

abstract class ReviewModule {
/**
* @param \stdClass $ctx {\Pike\Router router}
*/
public static function init(\stdClass $ctx) {
$ctx->router->map('POST', '/reviews',
[ReviewController::class, 'handleCreateReview', 'post:reviews']
);
$ctx->router->map('PUT', '/reviews/[i:reviewId]/approve-or-reject',
[ReviewController::class, 'handleApproveOrRejectReview', 'approveOrReject:reviews']
);
}
}
14 changes: 14 additions & 0 deletions examples/HelloWorld/src/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Me\HelloWorld;

class SomeClass {
/**
* @return string|null
*/
public function doSomething(): ?string {
return 'Hello';
}
}
30 changes: 30 additions & 0 deletions examples/HelloWorld/src/SomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Me\HelloWorld;

use Pike\Response;
use Pike\Request;

class SomeController {
/**
* @param \Me\HelloWorld\SomeClass $myClass
* @param \Pike\Response $res
*/
public function handleSomeRoute(SomeClass $myClass, Response $res): void {
$data = $myClass->doSomething();
if ($data)
$res->json([$data]);
else
$res->status(500)->json(['err' => 1]);
}
/**
* @param \Pike\Request $req
* @param \Pike\Response $res
*/
public function handleAnotherRoute(Request $req, Response $res): void {
$res->json(['yourParamWas' => $req->params->someParam,
'requestBodyWas' => $req->body]);
}
}
19 changes: 19 additions & 0 deletions examples/HelloWorld/src/SomeModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Me\HelloWorld;

abstract class SomeModule {
/**
* @param \stdClass $ctx {\Pike\Router router}
*/
public static function init(\stdClass $ctx) {
$ctx->router->map('GET', '/some-route',
[SomeController::class, 'handleSomeRoute']
);
$ctx->router->map('POST', '/another-route/[*:someParam]',
[SomeController::class, 'handleAnotherRoute']
);
}
}
27 changes: 27 additions & 0 deletions examples/MappingRoutes/src/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Me\MappingRoutes;

use Pike\Response;
use Pike\Request;

class Controller {
public function handleRouteA(Request $req, Response $res): void {
$res->json((object) [
'params' => $req->params,
'body' => $req->body,
'routeInfo' => $req->routeInfo,
]);
}
public function handleRouteB(Request $req, Response $res): void {
$this->handleRouteA($req, $res);
}
public function handleRouteC(Request $req, Response $res): void {
$this->handleRouteA($req, $res);
}
public function handleRouteD(Request $req, Response $res): void {
$this->handleRouteA($req, $res);
}
}
26 changes: 26 additions & 0 deletions examples/MappingRoutes/src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Me\MappingRoutes;

abstract class Module {
/**
* @param \stdClass $ctx {\Pike\Router router}
*/
public static function init(\stdClass $ctx) {
$ctx->router->map('GET', '/route-a',
[Controller::class, 'handleRouteA']
);
$ctx->router->map('GET', '/route-b/[i:myNumber]/[w:myOptionalSlug]?',
[Controller::class, 'handleRouteB']
);
$ctx->router->map('GET', '/route-c/[foo|bar:fooOrBar]',
[Controller::class, 'handleRouteC'],
'nameOfRouteC'
);
$ctx->router->map('POST', '/route-d/[i:id]',
[Controller::class, 'handleRouteC', ['my' => 'context']]
);
}
}
Loading

0 comments on commit c964fab

Please sign in to comment.