Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
slince committed Aug 10, 2019
1 parent ec1826b commit 17149c4
Show file tree
Hide file tree
Showing 25 changed files with 1,319 additions and 68 deletions.
31 changes: 8 additions & 23 deletions src/Jade/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@

namespace Jade;

use Jade\Routing\RouteCollector;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Jade\HttpKernel\HttpKernelProvider;
use Jade\Routing\Collection as RouteCollection;
use Jade\Routing\RouteCollection as RouteCollection;
use Jade\Routing\Route;
use Jade\Routing\RouteBuilderTrait;
use Jade\Middleware\RouteMiddleware;
use Zend\Diactoros\ServerRequestFactory;

class App implements RequestHandlerInterface
class App extends RouteCollector implements RequestHandlerInterface
{
use RouteBuilderTrait;

/**
* 是否已经初始化
*
Expand All @@ -39,11 +37,6 @@ class App implements RequestHandlerInterface
*/
protected $container;

/**
* @var RouteCollection
*/
protected $routes;

/**
* @var array
*/
Expand All @@ -58,7 +51,7 @@ public function __construct(ContainerInterface $container = null)
$this->container = $container;
$this->container['app'] = $this;
$this->register(new CoreServiceProvider());
$this->routes = new RouteCollection();
parent::__construct(new RouteCollection());
}

/**
Expand Down Expand Up @@ -111,9 +104,10 @@ public function serve()
/**
* 注册服务提供者
*
* @param ServiceProviderInterface|EventProviderInterface|CommandProviderInterface $provider
* @param object $provider
* @param array $values
*/
public function register($provider)
public function register($provider, array $values = [])
{
// 注册服务
if ($provider instanceof ServiceProviderInterface) {
Expand All @@ -123,19 +117,10 @@ public function register($provider)
if ($provider instanceof EventProviderInterface) {
$provider->subscribe($this->container->get('event_dispatcher'), $this->container);
}
$this->container->merge($values);
$this->providers[] = $provider;
}

/**
* 返回该应用对应的路由集合
*
* @return RouteCollection
*/
public function getRoutes()
{
return $this->routes;
}

/**
* 获取服务容器
*
Expand Down
3 changes: 2 additions & 1 deletion src/Jade/CommandProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface CommandProviderInterface
* 注册命令
*
* @param Application $app
* @param ContainerInterface $container
*/
public function provide(Application $app);
public function provide(Application $app, ContainerInterface $container);
}
2 changes: 1 addition & 1 deletion src/Jade/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function registerCommands()
{
foreach ($this->decorated->getProviders() as $provider) {
if ($provider instanceof CommandProviderInterface) {
$provider->provide($this);
$provider->provide($this, $this->decorated->getContainer());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Jade/Console/CommandDiscovererProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Jade\Console;

use Jade\CommandProviderInterface;
use Psr\Container\ContainerInterface;
use Zend\Stdlib\Glob;

class CommandDiscovererProvider implements CommandProviderInterface
Expand All @@ -35,7 +36,7 @@ public function __construct($namespace, $dstDir)
/**
* {@inheritdoc}
*/
public function provide(Application $app)
public function provide(Application $app, ContainerInterface $container)
{
foreach (Glob::glob("{$this->dstDir}/*Command.php") as $file) {
$commands = [];
Expand Down
22 changes: 20 additions & 2 deletions src/Jade/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
namespace Jade;

use Pimple\Container as PimpleContainer;
use Psr\Container\ContainerInterface;
use Jade\Exception\ContainerException;
use Jade\Exception\ContainerValueNotFoundException;

class Container extends PimpleContainer implements ContainerInterface
{

/**
* 从容器中获取实例对象或者其它资源
*
Expand Down Expand Up @@ -51,4 +49,24 @@ public function has($id)
{
return $this->offsetExists($id);
}

/**
* {@inheritdoc}
*/
public function merge(array $values)
{
foreach ($values as $key => $value) {
$this[$key] = $value;
}
}

/**
* {@inheritdoc}
*/
public function add(array $values)
{
foreach ($values as $key => $value) {
$this->offsetExists($key) || $this[$key] = $value;
}
}
}
22 changes: 22 additions & 0 deletions src/Jade/ContainerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Cube package.
*
* (c) Slince <taosikai@yeah.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Jade;

interface ContainerAwareInterface
{
/**
* Sets the container.
*
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container);
}
25 changes: 25 additions & 0 deletions src/Jade/ContainerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the jade/jade package.
*
* (c) Slince <taosikai@yeah.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Jade;

trait ContainerAwareTrait
{
/**
* @var ContainerInterface
*/
protected $container;

public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
}
31 changes: 31 additions & 0 deletions src/Jade/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Cube package.
*
* (c) Slince <taosikai@yeah.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Jade;

use Psr\Container\ContainerInterface as PsrContainerInterface;

interface ContainerInterface extends PsrContainerInterface
{
/**
* 批量添加服务/参数
*
* @param array $values
*/
public function add(array $values);

/**
* 批量合并参数
*
* @param array $values
*/
public function merge(array $values);
}
69 changes: 69 additions & 0 deletions src/Jade/Controller/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the jade/jade package.
*
* (c) Slince <taosikai@yeah.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Jade\Controller;

use Jade\ContainerAwareInterface;
use Jade\ContainerAwareTrait;
use Zend\Diactoros\Response;

class Controller implements ContainerAwareInterface
{
use ContainerAwareTrait;

/**
* 渲染模板
* @param string $view
* @param array $parameters
* @return string
*/
protected function renderView($view, array $parameters = [])
{
return $this->container->get('twig')->render($view, $parameters);
}

/**
* 渲染模板
*
* @param string $view
* @param array $parameters
* @return Response\HtmlResponse
*/
protected function render($view, array $parameters = [])
{
$content = $this->renderView($view, $parameters);
return new Response\HtmlResponse($content);
}

/**
* @param string $id
* @return mixed
*/
protected function get($id)
{
return $this->container->get($id);
}

/**
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
*
* @final
*/
protected function json($data, int $status = 200, array $headers = [], array $context = [])
{
if ($this->container->has('serializer')) {
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge([
'json_encode_options' => Response\JsonResponse::DEFAULT_JSON_FLAGS,
], $context));
}
return new Response\JsonResponse($data, $status, $headers);
}
}
3 changes: 2 additions & 1 deletion src/Jade/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Jade;

use Jade\Middleware\MiddlewareFactory;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Zend\Stratigility\MiddlewarePipe;

Expand All @@ -32,5 +31,7 @@ public function register(ContainerInterface $container)
$container['middleware_factory'] = function(){
return new MiddlewareFactory();
};
// route collector
$container['route_collector'] = $container->get('app');
}
}
4 changes: 2 additions & 2 deletions src/Jade/EventProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface EventProviderInterface
* 注册一组事件
*
* @param EventDispatcherInterface $eventDispatcher
* @param Container $container
* @param ContainerInterface $container
*/
public function subscribe(EventDispatcherInterface $eventDispatcher, Container $container);
public function subscribe(EventDispatcherInterface $eventDispatcher, ContainerInterface $container);
}
Loading

0 comments on commit 17149c4

Please sign in to comment.