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

Commit

Permalink
global upgrade skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Sep 29, 2017
1 parent d41707c commit 7469b5c
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 64 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
],
"require": {
"php": "^7.0",
"ext-phalcon": "^3.2",
"bavix/config": "^1.0"
"doctrine/inflector": "~1.0",
"bavix/config": "^1.0",
"ext-phalcon": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion etc/_bavix.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

return [
'database' => [
'adapter' => 'Mysql',
'adapter' => Phalcon\Db\Adapter\Pdo\Mysql::class,
'host' => 'localhost',
'username' => 'root',
'password' => '',
Expand Down
6 changes: 3 additions & 3 deletions etc/configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
'application' => [
'appDir' => BASE_PATH . 'src/App/',
'controllersDir' => BASE_PATH . 'src/App/Controller/',
'modelsDir' => BASE_PATH . 'var/models/',
'migrationsDir' => BASE_PATH . 'var/migrations/',
'observersDir' => BASE_PATH . 'var/observers/',
'eventsDir' => BASE_PATH . 'var/events/',
'middlewareDir' => BASE_PATH . 'var/middleware/',
'observersDir' => BASE_PATH . 'var/observers/',
'queuesDir' => BASE_PATH . 'var/queues/',
'modelsDir' => BASE_PATH . 'var/models/',
'viewsDir' => BASE_PATH . 'var/views/',
'pluginsDir' => BASE_PATH . 'var/plugins/',
'libraryDir' => BASE_PATH . 'var/library/',
Expand Down
2 changes: 1 addition & 1 deletion initd/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
$loader->registerNamespaces([
'Observers' => $configure->application->observersDir,
'Events' => $configure->application->eventsDir,
'Middleware' => $configure->application->middlewareDir,
'Models' => $configure->application->modelsDir,
]);

/**
Expand Down
7 changes: 4 additions & 3 deletions initd/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
$di->setShared('db', function () {
$configure = $this->getConfigure();

$class = 'Phalcon\Db\Adapter\Pdo\\' . $configure->database->adapter;
$params = [
'host' => $configure->database->host,
'username' => $configure->database->username,
Expand All @@ -83,11 +82,13 @@
'charset' => $configure->database->charset
];

if ($configure->database->adapter === 'Postgresql') {
if ($configure->database->adapter === \Phalcon\Db\Adapter\Pdo\Postgresql::class) {
unset($params['charset']);
}

return new $class($params);
$adapter = $configure->database->adapter;

return new $adapter($params);
});


Expand Down
12 changes: 10 additions & 2 deletions src/App/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
namespace App\Controller;

use Framework\Controller;
use Observers\TestObserver;
use Observers\TestQueue;
use Models\User;

class IndexController extends Controller
{
public function indexAction()
{
$this->builder->app()->queue(TestObserver::class, [
// $user = new User();
//
// $user->update();
//
// var_dump($user->toArray());
// die;

$this->builder->app()->queue(TestQueue::class, [
'hello' => 'world',
'time' => \date('d.m.Y H:i:s', \time())
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function startQueue($class, $data)
{
$object = new $class();

if (!($object instanceof Observer))
if (!($object instanceof QueueInterface))
{
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function beforeExecuteRoute(Dispatcher $dispatcher)
{
$object = new $middleware();

if (!($object instanceof Middleware))
if (!($object instanceof MiddlewareInterface))
{
throw new \RuntimeException('Middleware `' . $middleware . '` not found');
}

$result = $object->handle($dispatcher, $this);
$result = $object->next($dispatcher, $this);

if (!$result || !\is_bool($result))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Phalcon\Mvc\Dispatcher;

interface Middleware
interface MiddlewareInterface
{
/**
* @param Dispatcher $dispatcher
* @param Controller $controller
*
* @return mixed
*/
public function handle(Dispatcher $dispatcher, Controller $controller);
public function next(Dispatcher $dispatcher, Controller $controller);
}
85 changes: 58 additions & 27 deletions src/Framework/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,98 @@

namespace Framework;

use Doctrine\Common\Inflector\Inflector;

class Model extends \Phalcon\Mvc\Model
{

/**
* @var array
* @var string
*/
protected $_observerClass;

/**
* @var Observer
*/
private $_observerObject;

/**
* @var string
*/
protected $events = [];
protected $_tableName;

protected function initEvent($name)
/**
* @param $name
* @param $data
* @param $whiteList
*
* @return void
*/
protected function observer($name, $data = null, $whiteList = null)
{
if (!($di = $this->getDI()))

$class = $this->_observerClass;

if (!$class)
{
return;
}

if (empty($this->events[$name]))
if (!$this->_observerObject)
{
return;
global $builder;
$this->_observerObject = new $class($this, $builder->di());
}

$event = $this->events[$name];
$object = $this->_observerObject;

if (\is_string($event))
if (\method_exists($object, $name))
{
$event = [$event, 'handle'];
$object->$name($data, $whiteList);
}

$event($this->getDI(), $this);
}

public function update($data = null, $whiteList = null)
public function getSource()
{
$function = \ucfirst(__FUNCTION__);
if (!$this->_tableName)
{
$ref = new \ReflectionClass(static::class);
$table = $ref->getShortName();
$table = lcfirst($table);

$this->initEvent('before' . $function);
$result = parent::update($data, $whiteList);
$this->initEvent('after' . $function);
$this->_tableName = Inflector::pluralize($table);
}

return $result;
return $this->_tableName;
}

public function create($data = null, $whiteList = null)
{
$function = \ucfirst(__FUNCTION__);
$this->observer(__FUNCTION__, $data, $whiteList);

return parent::create($data, $whiteList);
}

public function update($data = null, $whiteList = null)
{
$this->observer(__FUNCTION__, $data, $whiteList);

return parent::update($data, $whiteList);
}

$this->initEvent('before' . $function);
$result = parent::create($data, $whiteList);
$this->initEvent('after' . $function);
public function save($data = null, $whiteList = null)
{
$this->observer(isset($this->id) ? 'update' : 'create', $data, $whiteList);

return $result;
return parent::save($data, $whiteList);
}

public function delete()
{
$function = \ucfirst(__FUNCTION__);

$this->initEvent('before' . $function);
$result = parent::delete();
$this->initEvent('after' . $function);
$this->observer(__FUNCTION__);

return $result;
return parent::delete();
}

}
10 changes: 0 additions & 10 deletions src/Framework/ModelEvent.php

This file was deleted.

28 changes: 26 additions & 2 deletions src/Framework/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@

namespace Framework;

interface Observer
use Phalcon\DiInterface;

abstract class Observer
{
public function handle(Builder $builder, $data);

/**
* @var Model
*/
protected $model;

/**
* @var DiInterface
*/
protected $di;

/**
* Observer constructor.
*
* @param Model $model
* @param DiInterface $di
*/
public function __construct(Model $model, DiInterface $di)
{
$this->model = $model;
$this->di = $di;
}

}
8 changes: 8 additions & 0 deletions src/Framework/QueueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Framework;

interface QueueInterface
{
public function handle(Builder $builder, $data);
}
6 changes: 3 additions & 3 deletions var/middleware/DefaultMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Middleware;

use Framework\Controller;
use Framework\Middleware;
use Framework\MiddlewareInterface;
use Phalcon\Mvc\Dispatcher;

class DefaultMiddleware implements Middleware
class DefaultMiddleware implements MiddlewareInterface
{

public function handle(Dispatcher $dispatcher, Controller $controller)
public function next(Dispatcher $dispatcher, Controller $controller)
{
$uri = $controller->request->getURI();

Expand Down
6 changes: 3 additions & 3 deletions var/middleware/NoIndexMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Middleware;

use Framework\Controller;
use Framework\Middleware;
use Framework\MiddlewareInterface;
use Phalcon\Mvc\Dispatcher;

class NoIndexMiddleware implements Middleware
class NoIndexMiddleware implements MiddlewareInterface
{

public function handle(Dispatcher $dispatcher, Controller $controller)
public function next(Dispatcher $dispatcher, Controller $controller)
{
$uri = $controller->request->getURI();

Expand Down
12 changes: 12 additions & 0 deletions var/models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Models;

use Framework\Model;

class User extends Model
{

protected $_observerClass = \Observers\User::class;

}
File renamed without changes.
34 changes: 34 additions & 0 deletions var/observers/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Observers;

use Framework\Observer;

class User extends Observer
{

public function save($data = null, $whiteList = null)
{
var_dump(__FUNCTION__, $data, $whiteList);
die;
}

public function create($data = null, $whiteList = null)
{
var_dump(__FUNCTION__, $data, $whiteList);
die;
}

public function update($data = null, $whiteList = null)
{
var_dump(__FUNCTION__, $data, $whiteList);
die;
}

public function delete()
{
var_dump(__FUNCTION__);
die;
}

}
Loading

0 comments on commit 7469b5c

Please sign in to comment.