Skip to content

Commit

Permalink
for cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Jan 21, 2018
1 parent 11358cb commit 64ca52e
Show file tree
Hide file tree
Showing 23 changed files with 188 additions and 171 deletions.
29 changes: 17 additions & 12 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
Copyright 2018 Yuuki Takezawa
The MIT License (MIT)

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Copyright (c) 2017-2018 Yuuki Takezawa

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Nazg Micro Fremawork Core Repository
# Nazg Micro Framework Core Repository

[![Build Status](https://travis-ci.org/nazg-hack/framework.svg?branch=master)](https://travis-ci.org/nazg-hack/framework)

Http Application / Microframework for HHVM/Hack

## Usage
## Usage

[Skeleton](https://github.com/ytake/nazg-skeleton)

```bash
$ hhvm -d xdebug.enable=0 -d hhvm.jit=0 -d hhvm.php7.all=1 -d hhvm.hack.lang.auto_typecheck=0 $(which composer) update
```

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"psr-11",
"psr-15"
],
"license": "BSD-3-Clause",
"license": "MIT",
"authors": [
{
"name": "Yuuki Takezawa",
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/NotFoundHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
*/
namespace Nazg\Exceptions;

final class NotFoundHttpException extends \Facebook\HackRouter\NotFoundException {}
final class NotFoundHttpException
extends \Facebook\HackRouter\NotFoundException {}
75 changes: 37 additions & 38 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use Facebook\HackRouter\BaseRouter;
use Nazg\Http\HttpMethod;
use Nazg\Heredity\{ MiddlewareStack, PsrContainerResolver };
use Nazg\Heredity\{MiddlewareStack, PsrContainerResolver};
use Nazg\Response\Emitter;
use Nazg\RequestHandler\FallbackHandler;
use Nazg\Foundation\Middleware\Dispatcher;
Expand All @@ -32,63 +32,61 @@
use Psr\Container\ContainerInterface;

class Application {
protected ImmVector<\Nazg\Types\TMiddlewareClass> $im = ImmVector{};

protected ImmVector<\Nazg\Types\TMiddlewareClass> $im = ImmVector {};

protected ?RequestHandlerInterface $requestHandler;

protected ?BootstrapRegister $bootstrapRegister;

public function __construct(
protected DependencyInterface $dependency
) {}
public function __construct(protected DependencyInterface $dependency) {}

public function run(
ServerRequestInterface $serverRequest
): void {
public function run(ServerRequestInterface $serverRequest): void {
$container = $this->getContainer();
// register bootstrap for framework application
// register bootstrap for framework application
$this->bootstrap($container);
$router = $container->get(BaseRouter::class);
invariant(
$router instanceof BaseRouter,
"%s class must extend %s",
get_class($router),
BaseRouter::class
$router instanceof BaseRouter,
"%s class must extend %s",
get_class($router),
BaseRouter::class,
);
list($middleware, $attributes) = $router->routePsr7Request($serverRequest);
list($middleware, $attributes) =
$router->routePsr7Request($serverRequest);
if ($attributes->count()) {
foreach($attributes as $key => $attribute) {
foreach ($attributes as $key => $attribute) {
$serverRequest = $serverRequest->withAttribute($key, $attribute);
}
}
$heredity = $this->middlewareProcessor($middleware, $container);
$this->send(
$heredity->handle(
$this->marshalAttributes($serverRequest, $attributes)
)
$this->marshalAttributes($serverRequest, $attributes),
),
);
}

protected function marshalAttributes(
ServerRequestInterface $request,
ImmMap<string, string> $attributes
ServerRequestInterface $request,
ImmMap<string, string> $attributes,
): ServerRequestInterface {
if ($attributes->count()) {
foreach($attributes as $key => $attribute) {
foreach ($attributes as $key => $attribute) {
$request = $request->withAttribute($key, $attribute);
}
}
return $request;
}

private function bootstrap(ContainerInterface $container): void {
$bootstrap = $this->bootstrapRegister ?: new BootstrapRegister($container);
$bootstrap =
$this->bootstrapRegister ?: new BootstrapRegister($container);
$bootstrap->register();
}

public function setBootstrap(BootstrapRegister $br): void {
$this->bootstrapRegister = $br;
$this->bootstrapRegister = $br;
}

public function setRequestHandler(RequestHandlerInterface $handler): void {
Expand All @@ -106,26 +104,26 @@ public function setApplicationConfig(array<mixed, mixed> $config): void {
public function getContainer(): ContainerInterface {
return $this->dependency->getContainer();
}

/**
* Middleware always executed by the application
* must override application class
*
* <code>
* <<__Override>>
* <<__Override>>
* protected function middleware(): ImmVector<\Nazg\Types\TMiddlewareClass> {
* return ImmVector{};
* }
* </code>
*/
protected function middleware(): ImmVector<\Nazg\Types\TMiddlewareClass> {
return ImmVector{};
return ImmVector {};
}

private function registerDependencies(mixed $config): void {
if(is_array($config)) {
if(array_key_exists(Service::MODULES, $config)) {
if($this->dependency instanceof \Nazg\Foundation\Dependency\Dependency) {
if (is_array($config)) {
if (array_key_exists(Service::MODULES, $config)) {
if ($this->dependency instanceof \Nazg\Foundation\Dependency\Dependency) {
$vModule = $config[Service::MODULES];
if ($vModule instanceof ImmVector) {
$this->dependency->appendModules($vModule->toVector());
Expand All @@ -136,8 +134,8 @@ private function registerDependencies(mixed $config): void {
}

private function registerMiddlewares(mixed $config): void {
if(is_array($config)) {
if(array_key_exists(Service::MIDDLEWARES, $config)) {
if (is_array($config)) {
if (array_key_exists(Service::MIDDLEWARES, $config)) {
if ($config[Service::MIDDLEWARES] instanceof ImmVector) {
$this->im = $config[Service::MIDDLEWARES];
}
Expand All @@ -147,21 +145,22 @@ private function registerMiddlewares(mixed $config): void {

protected function middlewareProcessor(
ImmVector<\Nazg\Types\TMiddlewareClass> $middleware,
ContainerInterface $container
ContainerInterface $container,
): RequestHandlerInterface {
$appMiddleware = $this->im->concat($this->middleware())
|>$$->concat($middleware)->toArray();
$appMiddleware =
$this->im->concat($this->middleware())
|> $$->concat($middleware)->toArray();
$dispatcher = new Dispatcher(
new MiddlewareStack(
$appMiddleware,
new PsrContainerResolver($container)
new PsrContainerResolver($container),
),
$this->requestHandler ?: new FallbackHandler()
$this->requestHandler ?: new FallbackHandler(),
);
$dispatcher->setContainer($container);
return $dispatcher;
}

protected function send(ResponseInterface $response): void {
(new Emitter())->emit($response);
}
Expand Down
17 changes: 7 additions & 10 deletions src/Foundation/Bootstrap/BootstrapRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
type Bootstrap = classname<BootstrapRegisterInterface>;

class BootstrapRegister implements BootstrapRegisterInterface {

protected ImmVector<Bootstrap> $ibr = ImmVector{
\Nazg\Foundation\Exception\ExceptionRegister::class,
};

public function __construct(
protected ContainerInterface $container
) {}

protected ImmVector<Bootstrap>
$ibr = ImmVector {\Nazg\Foundation\Exception\ExceptionRegister::class};

public function __construct(protected ContainerInterface $container) {}

public function register(): void {
foreach($this->ibr->getIterator() as $i) {
if($this->container->has($i)) {
foreach ($this->ibr->getIterator() as $i) {
if ($this->container->has($i)) {
$instance = $this->container->get($i);
if ($instance instanceof BootstrapRegisterInterface) {
$instance->register();
Expand Down
21 changes: 11 additions & 10 deletions src/Foundation/Dependency/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,38 @@
use Nazg\Routing\RouteServiceModule;

class Dependency implements DependencyInterface {

protected FactoryContainer $container;

protected Vector<\Nazg\Types\TServiceModule> $modules = Vector{
RouteServiceModule::class,
};

protected Vector<\Nazg\Types\TServiceModule>
$modules = Vector {RouteServiceModule::class};

public function __construct() {
$this->container = new \Ytake\HHContainer\FactoryContainer();
}

public function registerConfig(array<mixed, mixed> $config): void {
$this->container->set(
Service::CONFIG,
Service::CONFIG,
$container ==> $config,
\Ytake\HHContainer\Scope::SINGLETON
\Ytake\HHContainer\Scope::SINGLETON,
);
}

protected function registerServiceModule(): void {
foreach($this->modules->getIterator() as $i) {
foreach ($this->modules->getIterator() as $i) {
$this->container->register($i);
}
$this->container->lockModule();
}

public function register(): void {
$this->registerServiceModule();
}

public function appendModules(Vector<\Nazg\Types\TServiceModule> $modules): void {
public function appendModules(
Vector<\Nazg\Types\TServiceModule> $modules,
): void {
$this->modules->addAll($modules);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/Dependency/DependencyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use Psr\Container\ContainerInterface;

interface DependencyInterface {
interface DependencyInterface {

public function registerConfig(array<mixed, mixed> $config): void;

Expand Down
34 changes: 18 additions & 16 deletions src/Foundation/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?hh
<?hh

/**
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Expand Down Expand Up @@ -26,18 +26,18 @@
type ExceptionMap = ImmMap<string, mixed>;

class ExceptionHandler implements ExceptionHandleInterface {

public function __construct(protected Emitter $emitter) {}

/**
* @see https://github.com/zendframework/zend-diactoros/blob/master/doc/book/custom-responses.md
*/
public function render(ExceptionMap $em, \Exception $e): void {
$this->emitter->emit(
new JsonResponse(
$em->toArray(),
StatusCode::StatusInternalServerError
)
$em->toArray(),
StatusCode::StatusInternalServerError,
),
);
}

Expand All @@ -46,15 +46,17 @@ public function handleException(\Exception $e): void {
}

protected function toImmMap(\Exception $e): ExceptionMap {
return new ImmMap([
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => map(
$e->getTrace(),
$v ==> (new Map($v))|>$$->removeKey('args')|>$$->toArray()
),
]);
return new ImmMap(
[
'message' => $e->getMessage(),
'exception' => get_class($e),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => map(
$e->getTrace(),
$v ==> (new Map($v)) |> $$->removeKey('args') |> $$->toArray(),
),
],
);
}
}
4 changes: 1 addition & 3 deletions src/Foundation/Exception/ExceptionRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

class ExceptionRegister implements BootstrapRegisterInterface {

public function __construct(
protected ExceptionHandleInterface $handler
) {}
public function __construct(protected ExceptionHandleInterface $handler) {}

public function register(): void {
set_exception_handler([$this->handler, 'handleException']);
Expand Down
Loading

0 comments on commit 64ca52e

Please sign in to comment.