Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
global upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Nov 7, 2017
1 parent c7884fa commit 7f987c8
Show file tree
Hide file tree
Showing 36 changed files with 330 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.phar
composer.lock
.idea/
config/_bavix.php
cache/*.php
File renamed without changes.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
},
"autoload": {
"psr-4": {
"": "src/",
"Observers\\": "var/observers/",
"Queues\\": "var/queues/",
"Models\\": "var/models/"
"": "src/"
}
},
"autoload-dev": {
Expand Down
15 changes: 3 additions & 12 deletions config/configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
return [
'database' => '%database%',
'application' => [
'appDir' => BASE_PATH . 'src/App/',
'controllersDir' => BASE_PATH . 'src/App/Controller/',
'migrationsDir' => BASE_PATH . 'var/migrations/',
'middlewareDir' => BASE_PATH . 'var/middleware/',
'observersDir' => BASE_PATH . 'var/observers/',
'queuesDir' => BASE_PATH . 'var/queues/',
'modelsDir' => BASE_PATH . 'var/models/',
'modulesDir' => BASE_PATH . 'var/modules/',
'viewsDir' => BASE_PATH . 'var/views/',
'pluginsDir' => BASE_PATH . 'var/plugins/',
'libraryDir' => BASE_PATH . 'var/library/',
'cacheDir' => BASE_PATH . 'var/cache/',
'migrationsDir' => BASE_PATH . 'migrations/',
'viewsDir' => BASE_PATH . 'views/',
'cacheDir' => BASE_PATH . 'cache/',
'baseUri' => BASE_PATH,
]
];
14 changes: 14 additions & 0 deletions config/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'app' => [
'namespace' => Modules\App\Controllers::class,
'className' => Modules\App\Module::class,
'path' => BASE_PATH . 'src/Modules/App/Module.php'
],
'foo' => [
'namespace' => Modules\Foo\Controllers::class,
'className' => Modules\Foo\Module::class,
'path' => BASE_PATH . 'src/Modules/Foo/Module.php'
],
];
6 changes: 6 additions & 0 deletions config/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

//$router->add('/', [
// 'controller' => 'index',
// 'action' => 'index'
//]);
14 changes: 1 addition & 13 deletions initd/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@

$configure = $di->get('configure');

/**
* namespaces
*/
$loader->registerNamespaces([
'Observers' => $configure->application->observersDir,
'Middleware' => $configure->application->middlewareDir,
'Models' => $configure->application->modelsDir,
]);

/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs([
$configure->application->controllersDir
]);

$loader->registerDirs([]);
$loader->register();

return $loader;
56 changes: 51 additions & 5 deletions initd/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,61 @@
/**
* @var $router \Phalcon\Mvc\Router
* @var $di \Phalcon\Di\FactoryDefault
* @var $this \Framework\Builder
*/
$router = $di->get('router');

$router->setDefaultNamespace('App\Controller');
/**
* @var \Closure $loadRouter
*
* @param \Phalcon\Di\FactoryDefault $di
* @param \Phalcon\Mvc\Router\Group $group
*
* @return \Phalcon\Mvc\Router\Group
*/
$loadRouter = function (
\Phalcon\Di\FactoryDefault $di,
\Phalcon\Mvc\Router\Group $router,
\Bavix\Slice\Slice $slice) {

$path = $slice->getRequired('path');
$data = $slice->getRequired('data');

require_once $path;

return $router;
};

$loadRouter->bindTo($this);
$modules = $this->app()->getModules();
$router->setDefaultModule(key($modules));

foreach ($modules as $prefix => $data)
{
$path = dirname($data['path']) . '/config/router.php';

if (!file_exists($path))
{
throw new \Bavix\Exceptions\NotFound\Path('File `' . $path . '` not found');
}

$group = new \Phalcon\Mvc\Router\Group();
$group->setPrefix('/' . $prefix);
$group->setPaths([
'module' => $prefix,
'namespace' => $data['namespace']
]);

$loadRouter($di, $group, new \Bavix\Slice\Slice([
'path' => $path,
'data' => $data
]));

$router->mount($group);
}

//$router->add('/test', [
// 'controller' => 'index',
// 'action' => 'test'
//]);
unset($group, $prefix, $data);
require_once $this->root() . 'config/router.php';

$router->handle();

Expand Down
33 changes: 0 additions & 33 deletions initd/services.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Php as PhpEngine;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;
Expand Down Expand Up @@ -38,36 +35,6 @@
return $url;
});

/**
* Setting up the view component
*/
$di->setShared('view', function () {
$configure = $this->getConfigure();

$view = new View();
$view->setDI($this);
$view->setViewsDir($configure->application->viewsDir);

$view->registerEngines([
'.volt' => function ($view) {
$configure = $this->getConfigure();

$volt = new VoltEngine($view, $this);

$volt->setOptions([
'compiledPath' => $configure->application->cacheDir,
'compiledSeparator' => '_'
]);

return $volt;
},
'.phtml' => PhpEngine::class

]);

return $view;
});

/**
* Database connection is created based in the parameters defined in the configuration file
*/
Expand Down
File renamed without changes.
26 changes: 0 additions & 26 deletions src/App/Controller/IndexController.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function __construct(Builder $builder)
parent::__construct($builder->di());
$this->builder = $builder;
$this->config = $builder->config();

$this->loadModules();
}

/**
Expand All @@ -58,12 +60,22 @@ public function queue($class, $data)
return $this;
}

/**
* register modules
*/
protected function loadModules()
{
$modules = $this->config->get('modules');
$this->registerModules($modules->asArray());
}

/**
* Terminate application
*/
public function terminate()
{
$response = $this->handle($this->builder->urlPath());

echo $response->getContent();

if (function_exists('\fastcgi_finish_request'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Phalcon\Mvc\Dispatcher;

class Controller extends \Phalcon\Mvc\Controller
class ControllerBase extends \Phalcon\Mvc\Controller
{

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Framework/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
interface MiddlewareInterface
{
/**
* @param Dispatcher $dispatcher
* @param Controller $controller
* @param Dispatcher $dispatcher
* @param ControllerBase $controller
*
* @return mixed
*/
public function next(Dispatcher $dispatcher, Controller $controller);
public function next(Dispatcher $dispatcher, ControllerBase $controller);
}
101 changes: 101 additions & 0 deletions src/Framework/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Framework;

use Phalcon\Config;
use Phalcon\DiInterface;
use Phalcon\Loader;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Mvc\View;

abstract
class Module implements ModuleDefinitionInterface
{

/**
* @var Builder
*/
public $builder;

/**
* Module constructor.
*/
public function __construct()
{
global $builder;
$this->builder = $builder;
}

/**
* Registers an autoloader related to the module
*
* @param DiInterface $di
*/
public function registerAutoloaders(DiInterface $di = null)
{
$loader = new Loader();

$loader->registerNamespaces([
$this->namespace() => $this->moduleDir() . '/src'
]);

$loader->register();
}

/**
* Registers services related to the module
*
* @param DiInterface $di
*/
public function registerServices(DiInterface $di)
{
$module = $this;

/**
* Setting up the view component
*/
$di->setShared('view', function () use ($module) {

/**
* @var $this \Phalcon\Di\FactoryDefault
* @var $configure Config
*/
$configure = $this->getConfigure();

$view = new View();
$view->setDI($this);
$view->setViewsDir($module->moduleDir() . '/views');

$view->registerEngines([
'.volt' => function ($view) use ($configure) {
$volt = new View\Engine\Volt($view, $this);

$volt->setOptions([
'compiledPath' => $configure->application->cacheDir,
'compiledSeparator' => '_'
]);

return $volt;
},

'.phtml' => View\Engine\Php::class

]);

return $view;
});


}

/**
* @return string
*/
abstract public function namespace(): string;

/**
* @return string
*/
abstract public function moduleDir(): string;

}
Loading

0 comments on commit 7f987c8

Please sign in to comment.