-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 92f0f00
Showing
21 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.env | ||
vendor | ||
composer.lock | ||
yarn.lock | ||
node_modules |
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
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> |
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
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; | ||
} | ||
} | ||
} |
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
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); | ||
} | ||
} |
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
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) | ||
{ | ||
} | ||
} |
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
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); | ||
} |
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
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) | ||
{ | ||
} | ||
} |
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
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.