Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
schpill committed Mar 25, 2017
1 parent 95cb183 commit 7395913
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 45 deletions.
175 changes: 143 additions & 32 deletions lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
} elseif (fnmatch('*Model', $class) && strlen($class) > 5) {
$humanized = Inflector::uncamelize($class);
list($dbTable, $dummy) = explode($humanized, '_model', 2);

dd('ici');
}
}
}
Expand Down Expand Up @@ -632,25 +630,25 @@ function user($k = null, $d = null)
return $d;
}

function log($message, $type = 'INFO', $ns = 'core')
function log($message, $type = 'INFO')
{
if (is_array($message)) $message = implode(PHP_EOL, $message);

$type = Strings::upper($type);

$db = System::Log();
$db = em('systemLog');

$db->create([
'message' => $message,
'type' => $type
])->save();
}

function logs($type = 'INFO', $ns = 'core')
function logs($type = 'INFO')
{
$type = Strings::upper($type);

$db = engine('logs', $ns);
$db = em('systemLog');

return $db->where(['type', '=', $type])->sortByDesc('id')->get();
}
Expand Down Expand Up @@ -1348,6 +1346,17 @@ function lib($lib, $args = [])
return (new App)->make($class, $args);
}

function str()
{
static $stringInstance;

if (!$stringInstance) {
$stringInstance = new Inflector;
}

return $stringInstance;
}

function factory()
{
$factory = lib('OctaliaFactory')->construct(\Faker\Factory::create('fr_FR'));
Expand All @@ -1371,8 +1380,8 @@ function auth($ns = 'web')

function status($code = 200)
{
$headerMessage = Api::getMessage($code);
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
$headerMessage = Api::getMessage($code);
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';

if (!headers_sent()) {
header($protocol . " $code $headerMessage");
Expand Down Expand Up @@ -1567,9 +1576,14 @@ function systemBoot($dir = null)
Queue::listen();
Later::shutdown();
middlewares('after');
eventer()->listen('shutdown');
});
});

loadEvents();

eventer()->listen('bootstrap');

services();

middlewares();
Expand Down Expand Up @@ -1623,6 +1637,7 @@ function make(array $array = [], $instance = null)

function rights()
{
eventer()->listen('rights');
$acl = path('app') . '/config/acl.php';

$rights = make([], 'rights');
Expand Down Expand Up @@ -1871,45 +1886,78 @@ function eventer()
Registry::set('eventer.made', true);
$eventer = classify('eventer');

$eventer->on(function ($event, callable $cb) use ($eventer) {
$eventer->on(function () use ($eventer) {
$args = func_get_args();
$event = array_shift($args);
$cb = array_shift($args);
$priority = array_shift($args);
$priority = $priority || 0;

$events = Registry::get('eventer.events', []);
$events[$event] = $cb;
Registry::set('eventer.events', $events);

return $eventer;
});
if (!$cb instanceof \Closure) {
$cb = resolverClass($cb);
}

$eventer->service(function ($service) use ($eventer) {
$events = Registry::get('eventer.events', []);
$priorities = isAke($events, $event, []);

$cb = function ($event) use ($service) {
$app = provider();
$segment = isset($priorities[$priority]) ? $priorities[$priority] : [];

return call_user_func_array($app[$service], [$event]);
};
$segment[] = $cb;

$events[$event][$priority] = $segment;

$events[$service] = $cb;
Registry::set('eventer.events', $events);

return $eventer;
});

$eventer->forget(function ($event) use ($eventer) {
$eventer->listen(function () {
$events = Registry::get('eventer.events', []);
unset($events[$event]);
Registry::set('eventer.events', $events);

return $eventer;
$args = func_get_args();

$event = array_shift($args);

$fireEvents = isAke($events, $event, []);

$results = [];

if (!empty($fireEvents)) {
foreach ($fireEvents as $priority => $eventsLoaded) {
$key = $event . '_' . $priority;

$results[$key] = [];

foreach ($eventsLoaded as $eventLoaded) {
if ($eventLoaded && is_callable($eventLoaded)) {
$result = call_user_func_array($eventLoaded, $args);

if ($result instanceof Object && $result->getStopPropagation() == 1) {
return $results;
}

$results[$key][] = $result;
}
}
}
}

return $results;
});

$eventer->listen(function ($event, array $args = []) {
$eventer->forget(function ($event, $priority = 'octodummy') use ($eventer) {
$events = Registry::get('eventer.events', []);

$eventToFire = isAke($events, $event, null);

if ($eventToFire && is_callable($eventToFire)) {
return call_user_func_array($eventToFire, $args);
if ('octodummy' == $priority) {
unset($events[$event]);
} else {
unset($events[$event][$priority]);
}

Registry::set('eventer.events', $events);

return $eventer;
});

return $eventer;
Expand All @@ -1919,6 +1967,18 @@ function eventer()
return single('eventer', $cb);
}

function stopPropagation()
{
$o = o();

return $o->setStopPropagation(1);
}

function emit()
{
return call_user_func_array([eventer(), 'listen'], func_get_args());
}

function setting()
{
$made = Registry::get('settings.made', false);
Expand Down Expand Up @@ -2031,6 +2091,8 @@ function ionly()

function middlewares($when = 'before')
{
eventer()->listen('middlewares');

$middlewares = Registry::get('core.middlewares', []);
$request = make($_REQUEST, "request");
$middlewaresFile = path('app') . '/config/middlewares.php';
Expand Down Expand Up @@ -2071,8 +2133,45 @@ function aliases($className)
return false;
}

function loadEvents()
{
$events = path('app') . '/config/events.php';

if (File::exists($events)) {
require_once $events;
}

subscribers();
}

function subscriber($subscriberClass)
{
$subscriber = app($subscriberClass);

$events = $subscriber->getEvents();

foreach ($events as $event => $method) {
eventer()->on($event, $subscriberClass . '@' . $method);
}
}

function subscribers()
{
$subscribersFile = path('app') . '/config/subscribers.php';

if (File::exists($subscribersFile)) {
$ubscribers = include $subscribersFile;
}

foreach ($ubscribers as $subscriberClass) {
subscriber($subscriberClass);
}
}

function services()
{
eventer()->listen('services');

$services = Registry::get('core.services', []);

require_once __DIR__ . DS . 'serviceprovider.php';
Expand Down Expand Up @@ -3085,21 +3184,21 @@ function array_pop(&$array, $d = null)
return $val ?: $d;
}

function int($number)
function toInt($number)
{
settype($number, 'integer');

return (int) $number;
}

function float($number)
function toFloat($number)
{
settype($number, 'float');

return (float) $number;
}

function string($string)
function toString($string)
{
settype($string, 'string');

Expand Down Expand Up @@ -4886,3 +4985,15 @@ function notify($model, $instance, array $args = [])
}
}
}

function resolverClass($class, $sep = '@')
{
return function() use ($class, $sep) {
$segments = explode($sep, $class);
$method = count($segments) == 2 ? end($segments) : 'supply';
$callable = [app()->make(current($segments)), $method];
$data = func_get_args();

return call_user_func_array($callable, $data);
};
}
Loading

0 comments on commit 7395913

Please sign in to comment.