Last active
March 6, 2017 00:18
-
-
Save casimiroarruda/f599ee580fe135eca05bd87364f9523c to your computer and use it in GitHub Desktop.
Gist to support article on blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const BASE_PATH = __DIR__; | |
$loader = require __DIR__ . '/vendor/autoload.php'; | |
use Duodraco\Foundation\Kernel; | |
use Symfony\Component\HttpFoundation\Request; | |
$kernel = new Kernel('prod', true); | |
$request = Request::createFromGlobals(); | |
$response = $kernel->handle($request); | |
$response->send(); | |
$kernel->terminate($request, $response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Duodraco\Foundation\Command; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
abstract class Command extends Controller | |
{ | |
/** | |
* @var Service | |
*/ | |
protected $service; | |
final public function go(Request $request, array $attributes = []) : Response | |
{ | |
$this->service = $this->buildService($request->attributes->get("service")); | |
return $this->execute($request, $attributes); | |
} | |
protected function buildService(string $serviceName): Service | |
{ | |
$reflectionClass = new \ReflectionClass($serviceName); | |
$service = $reflectionClass->newInstanceArgs([$this->container]); | |
if($service instanceof Service){ | |
return $service; | |
} | |
throw new \Exception("Service not configured"); | |
} | |
abstract public function execute(Request $request, array $attributes): Response; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "duodraco/project-skeleton", | |
"description": "Dummy PHP Project Skeleton based upon Symfony Microkernel + PDS/Skeleton (Teaching Purposes)", | |
"minimum-stability": "stable", | |
"license": "MIT", | |
"authors": [ | |
{ | |
"name": "Anderson Casimiro", | |
"email": "[email protected]" | |
} | |
], | |
"require": { | |
"php":"^7.0", | |
"symfony/symfony": "^3.2", | |
"sensio/framework-extra-bundle": "^3.0" | |
}, | |
"require-dev": { | |
"pds/skeleton": "^1.0" | |
}, | |
"autoload":{ | |
"psr-4":{ | |
"Duodraco\\":"src/Duodraco" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
framework: | |
secret: ItsAK1nd0fM@gik... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__.'/../bootstrap.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Duodraco\Foundation; | |
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle; | |
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | |
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\Routing\RouteCollectionBuilder; | |
use Symfony\Component\HttpKernel\Kernel as HttpKernel; | |
class Kernel extends HttpKernel | |
{ | |
use MicroKernelTrait; | |
public function registerBundles() | |
{ | |
$bundles = [ | |
new FrameworkBundle(), | |
new SensioFrameworkExtraBundle() | |
]; | |
return $bundles; | |
} | |
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) | |
{ | |
$loader->load(BASE_PATH . '/config/config.yml'); | |
} | |
protected function configureRoutes(RouteCollectionBuilder $routes) | |
{ | |
$routes->import(BASE_PATH . '/resources/routes/main.yml'); | |
} | |
public function getCacheDir() | |
{ | |
return sys_get_temp_dir() . '/base-app/cache'; | |
} | |
public function getLogDir() | |
{ | |
return sys_get_temp_dir() . '/base-app/logs'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Duodraco\Command; | |
use Duodraco\Foundation\Command\Command; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
class Main extends Command | |
{ | |
public function execute(Request $request, array $attributes): Response | |
{ | |
$response = $this->service->peopleToFollow(); | |
return new JsonResponse($response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
index: | |
path: '/' | |
defaults: | |
_controller: 'Duodraco\Command\Main::go' | |
service: 'Duodraco\Service\PeopleProvider' | |
requirements: { } | |
methods: [GET] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Duodraco\Service; | |
use Duodraco\Foundation\Command\Service; | |
class PeopleProvider extends Service | |
{ | |
public function peopleToFollow(): array | |
{ | |
return [ | |
"@gabidavila" => "Gabriela D'Ávila", | |
"@eminetto" => "Elton Minetto", | |
"@abdala" => "Abdala Cerqueira", | |
"@dianaarnos" => "Diana Arnos" | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Duodraco\Foundation\Command; | |
use Symfony\Component\DependencyInjection\Container; | |
use Symfony\Component\DependencyInjection\ContainerAwareTrait; | |
abstract class Service | |
{ | |
use ContainerAwareTrait; | |
public function __construct(Container $container) | |
{ | |
$this->container = $container; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment