1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\Module;
use ICanBoogie\ActiveRecord;
use ICanBoogie\Autoconfig\Config;
use ICanBoogie\Facets\Fetcher;
use ICanBoogie\Binding\Routing\BeforeSynthesizeRoutesEvent;
use ICanBoogie\Core;
use ICanBoogie\Facets\RecordCollection;
use ICanBoogie\HTTP\Request;
use ICanBoogie\Module;
use ICanBoogie\PropertyNotDefined;
use ICanBoogie\Prototype;
use ICanBoogie\Render\TemplateResolver;
use ICanBoogie\Routing;
use ICanBoogie\Routing\Controller;
use ICanBoogie\View\View;
/**
* Hook callbacks.
*/
class Hooks
{
/**
* Returns the application's module collection.
*
* @return ModuleCollection
*/
static private function get_app_modules()
{
static $modules;
if ($modules === null)
{
/* @var $app CoreBindings */
$app = \ICanBoogie\app();
$modules = $app->modules;
}
return $modules;
}
/*
* Config
*/
/**
* Adds "modules" directories found in the app directories to `module-path`.
*
* @param array $autoconfig
*/
static public function filter_autoconfig(array &$autoconfig)
{
foreach ($autoconfig['app-paths'] as $directory)
{
if (file_exists($directory . 'modules'))
{
$autoconfig['module-path'][] = $directory . 'modules';
}
}
}
/*
* Events
*/
/**
* Extends application configuration according to modules features.
*
* The method may extend the `locale-path` configuration value and the configuration paths
* according to the modules features.
*
* @param Core\ConfigureEvent $event
* @param Core|CoreBindings $app
*/
static public function on_core_configure(Core\ConfigureEvent $event, Core $app)
{
$modules = $app->modules;
$modules->index;
#
# Add locale paths
#
$app->config['locale-path'] = array_merge($app->config['locale-path'], $modules->locale_paths);
#
# Add modules config paths to the configs path.
#
$modules_config_paths = $modules->config_paths;
if ($modules_config_paths)
{
$app->configs->add($modules->config_paths, Config::CONFIG_WEIGHT_MODULE);
}
}
/**
* Boot enabled modules.
*
* Before the modules are actually booted up, their index is used to alter the I18n load
* paths and the config paths.
*
* @param Core\BootEvent $event
* @param Core|CoreBindings $app
*/
static public function on_core_boot(Core\BootEvent $event, Core $app)
{
#
# Revoke prototypes and events.
#
Prototype::configure($app->configs['prototype']);
$app->events->attach_many($app->configs['event']);
}
/**
* Alter routes defined by modules by adding a `module` key that holds the identifier of the
* module that defines the route.
*
* @param BeforeSynthesizeRoutesEvent $event
*/
static public function before_synthesize_routes(BeforeSynthesizeRoutesEvent $event)
{
$module_roots = [];
foreach (self::get_app_modules()->descriptors as $module_id => $descriptor)
{
$module_roots[$descriptor[Descriptor::PATH]] = $module_id;
}
foreach ($event->fragments as $module_root => &$fragment)
{
$module_root = dirname(dirname($module_root)) . DIRECTORY_SEPARATOR;
if (empty($module_roots[$module_root]))
{
continue;
}
$module_id = $module_roots[$module_root];
foreach ($fragment as $route_id => &$route)
{
$route += [
'via' => Request::METHOD_ANY,
'module' => $module_id
];
}
}
}
/**
* Decorates the template resolver with a {@link ModuleTemplateResolver} instance.
*
* @param TemplateResolver\AlterEvent $event
*/
static public function on_template_resolver_alter(TemplateResolver\AlterEvent $event)
{
$event->instance = new ModuleTemplateResolver($event->instance, self::get_app_modules());
}
/**
* If the view renders a module's route, the "template" directory of that module is added
* to the list of templates locations. Also, the module is set as `module` view variable.
*
* @param View\AlterEvent $event
* @param View $target
*/
static public function on_view_alter(View\AlterEvent $event, View $target)
{
try
{
/* @var $controller ControllerBindings */
$controller = $target->controller;
$module = $controller->module;
}
catch (PropertyNotDefined $e)
{
return;
}
$target['module'] = $module;
$target->template_resolver->add_path($module->descriptor[Descriptor::PATH] . 'templates');
}
/*
* Prototypes
*/
/**
* Return the {@link ModuleCollection} instance used to manage the modules attached to the _core_.
*
* @param Core $app
*
* @return ModuleCollection The modules provider.
*/
static public function get_modules(Core $app)
{
$config = $app->config;
return new ModuleCollection($config['module-path'], $config['cache modules'] ? $app->vars : null);
}
/**
* Returns the {@link ModelCollection} instance used to obtain the models defined by the modules.
*
* @param Core|CoreBindings|\ICanBoogie\Binding\ActiveRecord\CoreBindings $app
*
* @return ModelCollection The models accessor.
*/
static public function get_models(Core $app)
{
return new ModelCollection($app->connections, $app->modules);
}
/**
* Return the {@link Module} instance associated with the route handled by the controller.
*
* @param Controller|ControllerBindings $controller
*
* @return Module
*/
static public function controller_get_module(Controller $controller)
{
return $controller->app->modules[$controller->route->module];
}
/**
* Return the primary model of the module associated with the route handled by the controller.
*
* @param Controller|ControllerBindings $controller
*
* @return \ICanBoogie\ActiveRecord\Model
*
* @see controller_get_module()
*/
static public function controller_get_model(Controller $controller)
{
return $controller->module->model;
}
/**
* Return a record fetcher for the controller `model`.
*
* **Note:** The "icanboogie/facets" package is required.
*
* @param Controller|ControllerBindings $controller
*
* @return Fetcher
*/
static public function controller_lazy_get_records_fetcher(Controller $controller)
{
return new Fetcher($controller->model);
}
/**
* Fetch records using the controller `records_fetcher`.
*
* @param Controller|ControllerBindings $controller
* @param array $modifiers
*
* @return RecordCollection
*/
static public function controller_fetch_records(Controller $controller, array $modifiers)
{
$fetcher = $controller->records_fetcher;
return $fetcher($modifiers);
}
/**
* Fetch records using the controller `records_fetcher`.
*
* @param Controller|ControllerBindings $controller
* @param array $modifiers
* @param Fetcher|null $fetcher Reference to a variable where the fetcher should be stored.
*
* @return ActiveRecord
*/
static public function controller_fetch_record(Controller $controller, array $modifiers, &$fetcher = null)
{
$fetcher = $controller->records_fetcher;
$records = $fetcher($modifiers);
if (!$records) {
return null;
}
return $records->one;
}
}