Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code restructure to classes #394

Merged
merged 10 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
!/config/
!/external/
!/src/
!/templates/
!/vendor/
!/webroot/
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ RUN composer install $COMPOSER_FLAGS --no-scripts --no-autoloader
# copy rest of the project. copy in order that is least to most changed
COPY --from=source /app/webroot ./webroot
COPY --from=source /app/external ./external
COPY --from=source /app/templates ./templates
COPY --from=source /app/src ./src
COPY --from=source /app/config ./config

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"autoload": {
"psr-4": {
"XHGui\\": "src/Xhgui/"
"XHGui\\": "src/"
}
},
"autoload-dev": {
Expand Down
4 changes: 1 addition & 3 deletions external/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use XHGui\Saver\SaverInterface;
use XHGui\ServiceContainer;

if (!defined('XHGUI_ROOT_DIR')) {
require dirname(__DIR__) . '/src/bootstrap.php';
}
require __DIR__ . '/../vendor/autoload.php';

$options = getopt('f:');

Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace XHGui;

/**
* Loads and reads config file.
*/
class Config
{
private static $config = [];

/**
* Load a config file, it will replace
* all the currently loaded configuration.
*/
public static function load($file)
{
$config = include $file;
self::$config = array_merge(self::$config, $config);
}

/**
* Get all the configuration options.
*
* @return array
*/
public static function all()
{
return self::$config;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 24 additions & 6 deletions src/Xhgui/ServiceContainer.php → src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use XHGui\Saver\NormalizingSaver;
use XHGui\Searcher\MongoSearcher;
use XHGui\Searcher\PdoSearcher;
use XHGui\ServiceProvider\ConfigProvider;
use XHGui\ServiceProvider\RouteProvider;
use XHGui\Twig\TwigExtension;

class ServiceContainer extends Container
Expand All @@ -31,6 +33,7 @@ public static function instance()
{
if (empty(static::$_instance)) {
static::$_instance = new self();
static::$_instance->boot();
}

return static::$_instance;
Expand All @@ -39,26 +42,43 @@ public static function instance()
public function __construct()
{
parent::__construct();
$this->setupPaths($this);
$this->register(new ConfigProvider());
$this->_slimApp();
$this->_services();
$this->storageDriverPdo($this);
$this->storageDriverMongoDb($this);
$this->_controllers();
}

public function boot()
{
$this->register(new RouteProvider());
}

private function setupPaths(self $app)
{
$app['app.dir'] = dirname(__DIR__);
$app['app.template_dir'] = dirname(__DIR__) . '/templates';
$app['app.config_dir'] = dirname(__DIR__) . '/config';
$app['app.cache_dir'] = static function ($c) {
return $c['config']['cache'] ?? dirname(__DIR__) . '/cache';
};
}

// Create the Slim app.
protected function _slimApp()
{
$this['view'] = static function ($c) {
$cacheDir = $c['config']['cache'] ?? XHGUI_ROOT_DIR . '/cache';

// Configure Twig view for slim
$view = new Twig();

$view->twigTemplateDirs = [dirname(__DIR__) . '/templates'];
$view->twigTemplateDirs = [
$c['app.template_dir'],
];
$view->parserOptions = [
'charset' => 'utf-8',
'cache' => $cacheDir,
'cache' => $c['app.cache_dir'],
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true,
Expand Down Expand Up @@ -97,8 +117,6 @@ protected function _slimApp()
*/
protected function _services()
{
$this['config'] = Config::all();

$this['searcher'] = static function ($c) {
$saver = $c['config']['save.handler'];

Expand Down
29 changes: 29 additions & 0 deletions src/ServiceProvider/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace XHGui\ServiceProvider;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use XHGui\Config;

class ConfigProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['config'] = static function ($app) {
// @deprecated
// define XHGUI_ROOT_DIR constant, config files may use it
if (!defined('XHGUI_ROOT_DIR')) {
define('XHGUI_ROOT_DIR', $app['app.dir']);
}

Config::load($app['app.config_dir'] . '/config.default.php');

if (file_exists($app['app.config_dir'] . '/config.php')) {
Config::load($app['app.config_dir'] . '/config.php');
}

return Config::all();
};
}
}
219 changes: 219 additions & 0 deletions src/ServiceProvider/RouteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php

namespace XHGui\ServiceProvider;

use Exception;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Slim\Slim as App;
use Slim\Views\Twig;
use XHGui\Controller;
use XHGui\Twig\TwigExtension;

class RouteProvider implements ServiceProviderInterface
{
public function register(Container $di)
{
$this->registerRoutes($di, $di['app']);
}

private function registerRoutes(Container $di, App $app)
{
$app->error(static function (Exception $e) use ($di, $app) {
/** @var Twig $view */
$view = $di['view'];
$view->parserOptions['cache'] = false;
$view->parserExtensions = [
new TwigExtension($app),
];

// Remove the controller so we don't render it.
unset($app->controller);

$app->view($view);
$app->render('error/view.twig', [
'message' => $e->getMessage(),
'stack_trace' => $e->getTraceAsString(),
]);
});

// Profile Runs routes
$app->get('/', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();
$response = $app->response();

$controller->index($request, $response);
})->setName('home');

$app->get('/run/view', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();
$response = $app->response();

$controller->view($request, $response);
})->setName('run.view');

$app->get('/run/delete', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->deleteForm($request);
})->setName('run.delete.form');

$app->post('/run/delete', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $di['runController'];
$request = $app->request();

$controller->deleteSubmit($request);
})->setName('run.delete.submit');

$app->get('/run/delete_all', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$controller->deleteAllForm();
})->setName('run.deleteAll.form');

$app->post('/run/delete_all', static function () use ($di) {
/** @var Controller\RunController $controller */
$controller = $di['runController'];
$controller->deleteAllSubmit();
})->setName('run.deleteAll.submit');

$app->get('/url/view', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->url($request);
})->setName('url.view');

$app->get('/run/compare', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->compare($request);
})->setName('run.compare');

$app->get('/run/symbol', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->symbol($request);
})->setName('run.symbol');

$app->get('/run/symbol/short', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->symbolShort($request);
})->setName('run.symbol-short');

$app->get('/run/callgraph', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $app->controller = $di['runController'];
$request = $app->request();

$controller->callgraph($request);
})->setName('run.callgraph');

$app->get('/run/callgraph/data', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $di['runController'];
$request = $app->request();
$response = $app->response();

$controller->callgraphData($request, $response);
})->setName('run.callgraph.data');

$app->get('/run/callgraph/dot', static function () use ($di, $app) {
/** @var Controller\RunController $controller */
$controller = $di['runController'];
$request = $app->request();
$response = $app->response();

$controller->callgraphDataDot($request, $response);
})->setName('run.callgraph.dot');

// Import route
$app->post('/run/import', static function () use ($di, $app) {
/** @var Controller\ImportController $controller */
$controller = $di['importController'];
$request = $app->request();
$response = $app->response();

$controller->import($request, $response);
})->setName('run.import');

// Watch function routes.
$app->get('/watch', static function () use ($di, $app) {
/** @var Controller\WatchController $controller */
$controller = $app->controller = $di['watchController'];
$controller->get();
})->setName('watch.list');

$app->post('/watch', static function () use ($di, $app) {
/** @var Controller\WatchController $controller */
$controller = $di['watchController'];
$request = $app->request();

$controller->post($request);
})->setName('watch.save');

// Custom report routes.
$app->get('/custom', static function () use ($di, $app) {
/** @var Controller\CustomController $controller */
$controller = $app->controller = $di['customController'];
$controller->get();
})->setName('custom.view');

$app->get('/custom/help', static function () use ($di, $app) {
/** @var Controller\CustomController $controller */
$controller = $app->controller = $di['customController'];
$request = $app->request();

$controller->help($request);
})->setName('custom.help');

$app->post('/custom/query', static function () use ($di, $app) {
/** @var Controller\CustomController $controller */
$controller = $di['customController'];
$request = $app->request();
$response = $app->response();

$controller->query($request, $response);
})->setName('custom.query');

// Waterfall routes
$app->get('/waterfall', static function () use ($di, $app) {
/** @var Controller\WaterfallController $controller */
$controller = $app->controller = $di['waterfallController'];
$controller->index();
})->setName('waterfall.list');

$app->get('/waterfall/data', static function () use ($di, $app) {
/** @var Controller\WaterfallController $controller */
$controller = $di['waterfallController'];
$request = $app->request();
$response = $app->response();

$controller->query($request, $response);
})->setName('waterfall.data');

// Metrics
$app->get('/metrics', static function () use ($di, $app) {
/** @var Controller\MetricsController $controller */
$controller = $di['metricsController'];
$response = $app->response();

$controller->metrics($response);
})->setName('metrics');
}
}
File renamed without changes.
File renamed without changes.
Loading