Skip to content

Commit

Permalink
Carpe Diem
Browse files Browse the repository at this point in the history
  • Loading branch information
adrorocker committed Oct 29, 2018
0 parents commit 92f0f00
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 0 deletions.
Empty file added .env.example
Empty file.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
vendor
composer.lock
yarn.lock
node_modules
21 changes: 21 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<IfModule mod_rewrite.c>
RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
164 changes: 164 additions & 0 deletions app/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
/**
* Shared Slim Skeleton.
*
* @link https://github.com/adrosoftware/shared-slim-skeleton
*
* @copyright Copyright (c) 2018 Adro Rocker
* @author Adro Rocker <[email protected]>
*/
namespace App;

use BadMethodCallException;
use Dotenv\Dotenv;
use App\Provider\ViewServicesProvider;
use App\Provider\RouteServicesProvider;
use App\Provider\ServiceProviderInterface;
use DI\Bridge\Slim\App as DiApp;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Application
{
protected $providers = [];
protected $container;
protected $configs = [];
public $version = '0.1.0';
protected $booted = false;

protected static $instance;

public static function getInstance()
{
return static::$instance;
}

public function __construct($root)
{
$this->base = $root . DIRECTORY_SEPARATOR . 'app';
$this->root = $root;

static::$instance = $this;

$this->bootstrap();
$this->registerDefaultServices();
}

/**
* Bootstrap the application.
*
* @return void
*/
protected function bootstrap()
{
$this->createConstants();
$dotenv = new Dotenv($this->root);
$dotenv->overload();
$this->setConfig();
$this->slim = $this->createApp();
$this->container = $this->slim->getContainer();
$this->container->set('app', $this);
$this->container->set('app.settings', $this->configs);
}

protected function createApp()
{
$settings = [
'settings' => $this->configs['slim'],
];

return new DiApp($settings);
}

public function getContainer()
{
return $this->container;
}

public function slim()
{
return $this->slim;
}

public function createConstants()
{
define('APP_PATH', realpath($this->root . '/app/'));
define('PUBLIC_PATH', realpath($this->root));
define('VIEWS_PATH', realpath($this->root . '/resources/views/'));
define('STORAGE_PATH', realpath($this->base . '/storage/'));
define('VERSION', $this->version);
}

public function setConfig()
{
$appConfig = [];

foreach (glob($this->root . '/config/*.php') as $configFile) {
$file = str_replace($this->root . '/config/', '', $configFile);
$name = str_replace('.php', '', $file);
$config = require $configFile;

$appConfig[$name] = $config;
}

$this->configs = $appConfig;

return $this;
}

/**
* This function registers the default services that Engine needs to run.
*
* @return void
*/
protected function registerDefaultServices()
{
$this
->register(new ViewServicesProvider)
->register(new RouteServicesProvider);
}

public function run()
{
if(!$this->booted){
$this->boot();
}

$this->slim->run();
}

/**
* Registers a service provider.
*
* @param ServiceProviderInterface $provider A ServiceProviderInterface instance
* @param array $values An array of values that customizes the provider
*
* @return Application
*/
public function register(ServiceProviderInterface $provider, array $values = array())
{
$provider->register($this->container);
foreach ($values as $key => $value) {
$this->container[$key] = $value;
}
$this->providers[] = $provider;
return $this;
}

/**
* Boots all service providers.
*
* This method is automatically called by run(), but you can use it
* to boot all service providers when not handling a request.
*/
public function boot()
{
if (!$this->booted) {
foreach ($this->providers as $provider) {
$provider->boot($this->container);
}
$this->booted = true;
}
}
}
23 changes: 23 additions & 0 deletions app/Http/Controller/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controller;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class HomeController
{
public function index(Request $request, Response $response)
{
$content = view()->render('home', ['name' => 'Adro']);;

return $response->getBody()->write($content);
}

public function contact(Request $request, Response $response)
{
$content = view()->render('home', ['name' => 'C']);;

return $response->getBody()->write($content);
}
}
31 changes: 31 additions & 0 deletions app/Provider/RouteServicesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Shared Slim Skeleton.
*
* @link https://github.com/adrosoftware/shared-slim-skeleton
*
* @copyright Copyright (c) 2018 Adro Rocker
* @author Adro Rocker <[email protected]>
*/
namespace App\Provider;

use App\Http\Controller;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class RouteServicesProvider implements ServiceProviderInterface
{
public function register(ContainerInterface $container)
{
$app = $container->get('app');

$slim = $app->slim();

$slim->get('/', action_path(Controller\HomeController::class, 'index'))->setName('home');
}

public function boot(ContainerInterface $container)
{
}
}
19 changes: 19 additions & 0 deletions app/Provider/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Shared Slim Skeleton.
*
* @link https://github.com/adrosoftware/shared-slim-skeleton
*
* @copyright Copyright (c) 2018 Adro Rocker
* @author Adro Rocker <[email protected]>
*/
namespace App\Provider;

use Psr\Container\ContainerInterface;

interface ServiceProviderInterface
{
public function register(ContainerInterface $container);

public function boot(ContainerInterface $container);
}
28 changes: 28 additions & 0 deletions app/Provider/ViewServicesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Shared Slim Skeleton.
*
* @link https://github.com/adrosoftware/shared-slim-skeleton
*
* @copyright Copyright (c) 2018 Adro Rocker
* @author Adro Rocker <[email protected]>
*/
namespace App\Provider;

use League\Plates\Engine;
use Psr\Container\ContainerInterface;

class ViewServicesProvider implements ServiceProviderInterface
{
public function register(ContainerInterface $container)
{
$viewConfigs = $container->get('app.settings')['view'];
$view = new Engine($viewConfigs['path']);

$container->set('view', $view);
}

public function boot(ContainerInterface $container)
{
}
}
65 changes: 65 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use App\Application;

if (!function_exists('container')) {
function container($service = false)
{
$container = Application::getInstance()->getContainer();

if ($service) {
return $container->get($service);
}
return $container;
}
}

if (!function_exists('make')) {
function make($class)
{
return container()->make($class);
}
}

function view()
{
return container('view');
}

function action_path($handler, $action)
{
return "$handler::$action";
}

// Dev functions

if (!function_exists('dd')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed
* @return void
*/
function dd()
{
foreach (func_get_args() as $value) {
d($value);
}
die(1);
}
}

if (!function_exists('d')) {
/**
* Dump the passed variables and end the script.
*
* @param mixed
* @return void
*/
function d()
{
foreach (func_get_args() as $value) {
dump($value);
}
}
}
Loading

0 comments on commit 92f0f00

Please sign in to comment.