Skip to content

Commit

Permalink
Inject models
Browse files Browse the repository at this point in the history
  • Loading branch information
Timot Tarjani committed Mar 19, 2024
1 parent 1c35735 commit b1ff2c2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 50 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"AbstractControllerTest::testControllerCreation":4,"AbstractControllerTest::testControllerRedirectMethods":4,"AbstractControllerTest::testIfVariablesSet":4,"AppTest::testAppCreation":4,"AppTest::testAppRootDir":4,"AppTest::testIsInstalled":4,"ExceptionHandler::testRenderAuthorizationException":4,"ExceptionHandler::testRenderAuthenticationException":4,"ExceptionHandler::testRenderAllExceptions":4,"AdminMiddlewareTest::testNonAdminsAreRedirected":4,"AdminMiddlewareTest::testAdminsAreNotRedirected":4,"AdminMiddlewareTest::testNonActiveAdminsAreRedirected":4,"InstallerMiddlewareTest::testNotRedirectIfInstalled":4,"InstallerMiddlewareTest::testInstallerIsNotAvailableAfterInstalled":4,"SettingsMiddlewareTest::testSettingsAreSet":4,"PluginModelTest::testPluginInitiation":4,"PluginModelTest::testInfoUsage":4,"PluginModelTest::testRootDirSetter":4,"PluginModelTest::testAllGetter":4,"PluginModelTest::testMethodsDependingOnRegister":4,"RouteResolverTest::testRoutingInitiation":4,"RouteResolverTest::testRoutingMethodExistance":4,"RouteResolverTest::testRouteNamespacing":4,"RouteResolverTest::testControllerResolving":4,"RouteResolverTest::testControllerNotExsistException":4,"RouteResolverTest::testRouteNotExsistsExceptionIsThrown":4,"SearchEngineTest::testSearchEngineInitiation":4,"SearchEngineTest::testRoutingMethodExistance":4,"SearchEngineTest::testGetterAndSetterModel":4,"SearchEngineTest::testExecuteSearch":4,"SearchEngineTest::testClearResults":4,"SearchEngineTest::testSearchKey":4,"ViewResolverTest::testDataArraysSet":4,"ViewResolverTest::testSetters":4,"ViewResolverTest::testRender":4,"WidgetResolverTest::testSetterAndGetter":4,"WidgetResolverTest::testWidgetResolving":4},"times":{"AbstractControllerTest::testControllerCreation":0.078,"AbstractControllerTest::testControllerRedirectMethods":0.004,"AbstractControllerTest::testIfVariablesSet":0.004,"AppTest::testAppCreation":0.004,"AppTest::testAppRootDir":0.005,"AppTest::testIsInstalled":0.004,"ExceptionHandler::testRenderAuthorizationException":0.011,"ExceptionHandler::testRenderAuthenticationException":0.004,"ExceptionHandler::testRenderAllExceptions":0.004,"AdminMiddlewareTest::testNonAdminsAreRedirected":0.004,"AdminMiddlewareTest::testAdminsAreNotRedirected":0.004,"AdminMiddlewareTest::testNonActiveAdminsAreRedirected":0.004,"InstallerMiddlewareTest::testNotRedirectIfInstalled":0.004,"InstallerMiddlewareTest::testInstallerIsNotAvailableAfterInstalled":0.004,"SettingsMiddlewareTest::testSettingsAreSet":0.004,"PluginModelTest::testPluginInitiation":0.004,"PluginModelTest::testInfoUsage":0.003,"PluginModelTest::testRootDirSetter":0.004,"PluginModelTest::testAllGetter":0.004,"PluginModelTest::testMethodsDependingOnRegister":0.004,"RouteResolverTest::testRoutingInitiation":0.005,"RouteResolverTest::testRoutingMethodExistance":0.006,"RouteResolverTest::testRouteNamespacing":0.004,"RouteResolverTest::testControllerResolving":0.004,"RouteResolverTest::testControllerNotExsistException":0.01,"RouteResolverTest::testRouteNotExsistsExceptionIsThrown":0.004,"SearchEngineTest::testSearchEngineInitiation":0.005,"SearchEngineTest::testRoutingMethodExistance":0.006,"SearchEngineTest::testGetterAndSetterModel":0.004,"SearchEngineTest::testExecuteSearch":0.004,"SearchEngineTest::testClearResults":0.006,"SearchEngineTest::testSearchKey":0.005,"ViewResolverTest::testDataArraysSet":0.003,"ViewResolverTest::testSetters":0.005,"ViewResolverTest::testRender":0.006,"WidgetResolverTest::testSetterAndGetter":0.004,"WidgetResolverTest::testWidgetResolving":0.003}}
4 changes: 2 additions & 2 deletions app/HorizontCMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class HorizontCMS extends \Illuminate\Foundation\Application
{

// TODO Settings should be added here
public $plugins;

public function __construct($basePath = null)
Expand All @@ -19,12 +20,11 @@ public function __construct($basePath = null)

public static function isInstalled()
{
return file_exists(base_path(".env")) || env("INSTALLED", "") != "";
return file_exists(base_path(".env")) || env("INSTALLED", false);
}

public function publicPath()
{

return $this->basePath . DIRECTORY_SEPARATOR;
}

Expand Down
17 changes: 13 additions & 4 deletions app/Http/Middleware/SettingsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

class SettingsMiddleware
{

private $settings;

public function __construct(\App\Model\Settings $settings)
{
$this->settings = $settings;
}


/**
* Handle an incoming request.
*
Expand All @@ -18,11 +27,11 @@ public function handle($request, Closure $next)
{

if(\App\HorizontCMS::isInstalled()){
$settings = new \App\Model\Settings();
$settings->assignAll();
$request->settings = json_decode(json_encode($settings->settings), true);

$this->settings->assignAll();
$request->settings = json_decode(json_encode($this->settings->settings), true);

View::share('settings', \App\Model\Settings::getAll());
View::share('settings', $this->settings::getAll());
}


Expand Down
17 changes: 13 additions & 4 deletions app/Http/Middleware/WebsiteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class WebsiteMiddleware
{

private $widgets;

public function __construct(\App\Libs\ShortCode $shortcode_engine){
$this->widgets = $shortcode_engine;
}


/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);

$widgets = new \App\Libs\ShortCode();
$widgets->initalize(app()->plugins);
$this->widgets->initalize(app()->plugins);

$response->setContent($widgets->compile($response->getContent()));
$response->setContent($this->widgets->compile($response->getContent()));

return $response;
}
Expand Down
44 changes: 21 additions & 23 deletions app/Libs/ShortCode.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
<?php
<?php

namespace App\Libs;

use \App\Model\Plugin as Plugin;

class ShortCode{
class ShortCode
{

private $widgets = array();

public function initalize($plugins){
public function initalize($plugins)
{

foreach($plugins as $plugin){
foreach ($plugins as $plugin) {


if( $plugin->hasRegister('widget') ){
if ($plugin->hasRegister('widget')) {

\View::addNamespace('plugin', [
$plugin->getPath()."/app/View",
$plugin->getPath()."/app/resources/views"
]);
$plugin->getPath() . "/app/View",
$plugin->getPath() . "/app/resources/views"
]);

$this->addWidget($plugin->getShortCode(),$plugin->getRegister('widget'));
$this->addWidget($plugin->getShortCode(), $plugin->getRegister('widget'));
}


}

}

public function addWidget($key,$value){
public function addWidget($key, $value)
{
$this->widgets[$key] = $value;
}

public function getWidget($key){
public function getWidget($key)
{
return $this->widgets[$key];
}

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

public function compile($page){

return count($this->widgets) === 0? $page : str_replace(array_keys($this->widgets), array_values($this->widgets), $page);
public function compile($page)
{
return count($this->widgets) === 0 ? $page : str_replace(array_keys($this->widgets), array_values($this->widgets), $page);
}


}
}
34 changes: 18 additions & 16 deletions app/Libs/ViewResolver.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
<?php

namespace App\Libs;

use Config;

class ViewResolver{
class ViewResolver
{

public $data = [];

Expand All @@ -15,40 +16,41 @@ class ViewResolver{
public $meta = [];


public function __construct(){
public function __construct()
{
$this->data['title'] = null;
$this->data['meta'][] = ['viewport','width=device-width, initial-scale=1'];
$this->data['meta'][] = ['viewport', 'width=device-width, initial-scale=1'];
$this->data['css'] = Config::get('horizontcms.css');
$this->data['js'] = Config::get('horizontcms.js');
$this->data['jsplugins'] = [];
}

public function title($title){
public function title($title)
{
$this->data['title'] = $title;
}

public function render($view_file,array $data = []){
public function render($view_file, array $data = [])
{

return view($view_file,array_merge($this->data,$data));
return view($view_file, array_merge($this->data, $data));
}


public function css($file){
public function css($file)
{
$this->data['css'][] = $file;
}


public function js($file){
public function js($file)
{
$this->data['js'][] = $file;
}


public function meta($name,$content){
$this->data['meta'][] = [$name,$content];
public function meta($name, $content)
{
$this->data['meta'][] = [$name, $content];
}




}

4 changes: 4 additions & 0 deletions app/Model/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function __construct($root_dir = null){

}

public function scopeActive($query){
return $query->where('active','1');
}

public function setRootDir($root_dir){
$this->root_dir = $root_dir;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/PluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function boot(\Illuminate\Contracts\Http\Kernel $kernel)
try{

if($this->app->isInstalled()){
$this->app->plugins = \App\Model\Plugin::where('active','1')->get()->keyBy('root_dir');
$this->app->plugins = \App\Model\Plugin::active()->get()->keyBy('root_dir');

$this->registerPluginAutoloaders();

Expand Down

0 comments on commit b1ff2c2

Please sign in to comment.