Skip to content

Commit

Permalink
Update virtual fields transformation - Version bump to 3.0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Jan 20, 2020
1 parent 645d87e commit fd9cf0e
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Aeria/Aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
class Aeria extends Container
{
public const VERSION = '3.0.17';
public const VERSION = '3.0.18';
/**
* Constructs the Aeria container
*
Expand Down
49 changes: 25 additions & 24 deletions Aeria/Kernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,85 @@
use Aeria\Container\Container;
use Aeria\Kernel\AbstractClasses\Task;
use Aeria\Structure\Traits\DictionaryTrait;

/**
* The kernel is in charge of the boot of Aeria.
*
*
* @category Kernel
* @package Aeria
*
* @author Jacopo Martinelli <jacopo.martinelli@caffeina.com>
* @license https://github.com/caffeinalab/aeria/blob/master/LICENSE MIT license
* @link https://github.com/caffeinalab/aeria
*
* @see https://github.com/caffeinalab/aeria
*/
class Kernel
{
use DictionaryTrait;

/**
* This function registers a task to the kernel.
*
* @param Task $task the task we want to register
*
* @return void
*
* @access public
* @since Method available since Release 3.0.0
*/
public function register(Task $task)
{
$key = get_class($task);
$key = substr($key, strrpos($key, '\\')+1);
$key = substr($key, strrpos($key, '\\') + 1);
$key = toSnake($key);
$this->set($key, $task);
}

/**
* This function boots all of the services and tasks.
* This function boots all of the services and tasks.
*
* @param Container $container where we've bound the services
*
* @return void
*
* @access public
* @since Method available since Release 3.0.0
*/
*/
public function boot(Container $container)
{
// Services
$service_abstracts = ['meta', 'validator', 'query', 'router', 'controller',
'taxonomy', 'updater', 'render_engine', 'field', 'options', 'post_type'];
$service_abstracts = ['config', 'meta', 'validator', 'query', 'router', 'controller',
'taxonomy', 'updater', 'render_engine', 'field', 'options', 'post_type', ];
foreach ($service_abstracts as $abstract) {
$service[$abstract] = $container->make($abstract);
}

$args = [
'config' => $container->make('config')->all(),
'service' => $service,
'container' => $container
'container' => $container,
];
$tasks = $this->all();
uasort($tasks, array($this, 'compareTasks'));
foreach ($tasks as $task) {
if(is_admin() && $task->admin_only)
$task->do($args);
else if (!$task->admin_only)
$task->do($args);
if ((is_admin() && $task->admin_only) || !$task->admin_only) {
$new_args = $task->do($args);
if (isset($new_args)) {
$args = $new_args;
}
}
}
}

/**
* This function is required to order the tasks by their priorities
* This function is required to order the tasks by their priorities.
*
* @param Task $a the first task
* @param Task $b the second task
*
* @return 1 if a>b, -1 if not
*
* @access private
* @since Method available since Release 3.0.0
*/
*/
private function compareTasks(Task $a, Task $b)
{
if ($a->priority == $b->priority)
if ($a->priority == $b->priority) {
return 0;
}

return ($a->priority < $b->priority) ? -1 : 1;
}
}
}
41 changes: 19 additions & 22 deletions Aeria/Kernel/ServiceProviders/KernelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,43 @@
use Aeria\Container\Container;
use Aeria\Kernel\Kernel;
use Aeria\Config\Config;
use Aeria\PostType\PostType;
use Aeria\Taxonomy\Taxonomy;
use Aeria\Kernel\Loader;
use Aeria\Kernel\Tasks\{
CreateAdminScripts,
CreateControllers,
CreateField,
CreateMeta,
CreateOptions,
CreatePostType,
CreateRenderer,
CreateRouter,
CreateTaxonomy,
CreateUpdater
};
use Aeria\Kernel\Tasks\CreateAdminScripts;
use Aeria\Kernel\Tasks\CreateConfig;
use Aeria\Kernel\Tasks\CreateControllers;
use Aeria\Kernel\Tasks\CreateField;
use Aeria\Kernel\Tasks\CreateMeta;
use Aeria\Kernel\Tasks\CreateOptions;
use Aeria\Kernel\Tasks\CreatePostType;
use Aeria\Kernel\Tasks\CreateRenderer;
use Aeria\Kernel\Tasks\CreateRouter;
use Aeria\Kernel\Tasks\CreateTaxonomy;
use Aeria\Kernel\Tasks\CreateUpdater;

/**
* KernelServiceProvider is in charge of registering the Kernel to the container
* KernelServiceProvider is in charge of registering the Kernel to the container.
*
* @category Kernel
* @package Aeria
*
* @author Jacopo Martinelli <jacopo.martinelli@caffeina.com>
* @license https://github.com/caffeinalab/aeria/blob/master/LICENSE MIT license
* @link https://github.com/caffeinalab/aeria
*
* @see https://github.com/caffeinalab/aeria
*/
class KernelServiceProvider implements ServiceProviderInterface
{
/**
* Registers the service to the provided container, as a singleton
* Registers the service to the provided container, as a singleton.
*
* @param Container $container Aeria's container
*
* @return void
*
* @access public
* @since Method available since Release 3.0.0
*/
public function register(Container $container)
{
$container->singleton('kernel', Kernel::class);
}

/**
* In charge of booting the service. It loads the config,
* then registers the tasks to the kernel. Finally, it boots the
Expand All @@ -56,7 +52,6 @@ public function register(Container $container)
*
* @return bool true: service booted
*
* @access public
* @since Method available since Release 3.0.0
*/
public function boot(Container $container): bool
Expand All @@ -65,6 +60,7 @@ public function boot(Container $container): bool
$config = $container->make('config');
Loader::loadConfig($config, $container);
$kernel->register(new CreateAdminScripts());
$kernel->register(new CreateConfig());
$kernel->register(new CreateControllers());
$kernel->register(new CreateField());
$kernel->register(new CreateMeta());
Expand All @@ -76,6 +72,7 @@ public function boot(Container $container): bool
$kernel->register(new CreateUpdater());
$kernel->boot($container);
do_action('aeria_booted');

return true;
}
}
86 changes: 86 additions & 0 deletions Aeria/Kernel/Tasks/CreateConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Aeria\Kernel\Tasks;

use Aeria\Kernel\AbstractClasses\Task;

/**
* This task is in charge of creating fields.
*
* @category Kernel
*
* @author Alberto Parziale <alberto.parziale@caffeina.com>
* @license https://github.com/caffeinalab/aeria/blob/master/LICENSE MIT license
*
* @see https://github.com/caffeinalab/aeria
*/
class CreateConfig extends Task
{
public $priority = 2;
public $admin_only = false;

/**
* The main task method. It registers the fields to the field service.
*
* @param array $args the arguments to be passed to the Task
*
* @since Method available since Release 3.0.17
*/
public function do(array $args)
{
$config = $args['container']->make('config')->all();
$args['config'] = $this->manipulateConfig($config);
$args['config'] = $this->checkSectionIds($args['config']);

return $args;
}

private function checkSectionIds($tree)
{
foreach ($tree as $key => $value) {
$tree[$key]['id'] = $key;
}

return $tree;
}

private function manipulateConfig($tree)
{
foreach ($tree as $key => $value) {
if (is_array($tree[$key])) {
$tree[$key] = $this->manipulateConfig($tree[$key]);
}
if ($key === 'section') {
$tree[$key] = $this->checkSectionIds($tree[$key]);
}
if ($key === 'fields') {
foreach ($tree[$key] as $fieldKey => $field_config) {
$tree[$key][$fieldKey] = $this->getRealFields($field_config);
}
}
}

return $tree;
}

private function getRealFields($field_config)
{
$fields_registry = aeria('field');
if (isset($field_config['fields'])) {
$field_config['fields'] = $this->getRealFields($field_config['fields']);
}
if (!isset($field_config['type'])) {
return $field_config;
}
$type = $field_config['type'];
if (!$fields_registry->exists($type)) {
return $field_config;
}
$field_type_class = $fields_registry->get($type);
$new_field_config = is_array($field_config)
? $field_type_class::transformConfig($field_config)
: $field_config;

return $new_field_config;
}
}
14 changes: 6 additions & 8 deletions Aeria/Kernel/Tasks/CreateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
namespace Aeria\Kernel\Tasks;

use Aeria\Kernel\AbstractClasses\Task;

/**
* This task is in charge of creating fields.
*
* @category Kernel
* @package Aeria
*
* @author Simone Montali <simone.montali@caffeina.com>
* @license https://github.com/caffeinalab/aeria/blob/master/LICENSE MIT license
* @link https://github.com/caffeinalab/aeria
*
* @see https://github.com/caffeinalab/aeria
*/
class CreateField extends Task
{
public $priority = 2;
public $priority = 1;
public $admin_only = false;

/**
* The main task method. It registers the fields to the field service.
*
* @param array $args the arguments to be passed to the Task
*
* @return void
*
* @access public
* @since Method available since Release 3.0.0
*/
public function do(array $args)
Expand Down Expand Up @@ -52,8 +52,6 @@ public function do(array $args)
$args['service']['field']->register('url', \Aeria\Field\Fields\BaseField::class);
$args['service']['field']->register('date', \Aeria\Field\Fields\BaseField::class);


do_action('aeria_register_field', $args['service']['field'], $args['container']);
}

}
Loading

0 comments on commit fd9cf0e

Please sign in to comment.