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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
<?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;
use ICanBoogie\ActiveRecord\Connection;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelNotDefined;
use ICanBoogie\I18n;
use ICanBoogie\Module\Descriptor;
use ICanBoogie\Module\ModuleCollection;
/**
* A module of the framework.
*
* @property-read array $descriptor The descriptor of the module.
* @property-read string $flat_id Underscored identifier.
* @property-read string $id The identifier of the module, defined by {@link Descriptor::ID}.
* @property-read Model $model The primary model of the module.
* @property-read Module $parent The parent module, defined by {@link Descriptor::INHERITS}.
* @property-read string $path The path to the module, defined by {@link Descriptor::PATH}.
* @property-read string $title The localized title of the module.
* @property-read ModuleCollection $collection
* @property-read Application $app
*/
class Module extends Prototyped
{
/*
* PERMISSIONS:
*
* NONE: Well, you can't do anything
*
* ACCESS: You can access the module and view its records
*
* CREATE: You can create new records
*
* MAINTAIN: You can edit the records you created
*
* MANAGE: You can delete the records you created
*
* ADMINISTER: You have complete control over the module
*
*/
const PERMISSION_NONE = 0;
const PERMISSION_ACCESS = 1;
const PERMISSION_CREATE = 2;
const PERMISSION_MAINTAIN = 3;
const PERMISSION_MANAGE = 4;
const PERMISSION_ADMINISTER = 5;
/**
* Defines the name of the operation used to save the records of the module.
*/
const OPERATION_SAVE = 'save';
/**
* Defines the name of the operation used to delete the records of the module.
*/
const OPERATION_DELETE = 'delete';
/**
* Returns the identifier of the module as defined by its descriptor.
*
* This method is the getter for the {@link $id} magic property.
*
* @return string
*/
protected function get_id()
{
return $this->descriptor[Descriptor::ID];
}
/**
* Returns the path of the module as defined by its descriptor.
*
* This method is the getter for the {@link $path} magic property.
*
* @return string
*/
protected function get_path()
{
return $this->descriptor[Descriptor::PATH];
}
/**
* The descriptor of the module.
*
* @var array
*/
protected $descriptor;
/**
* Returns the descriptor of the module.
*
* This method is the getter for the {@link $descriptor} magic property.
*
* @return array
*/
protected function get_descriptor()
{
return $this->descriptor;
}
/**
* Cache for loaded models.
*
* @var ActiveRecord\Model[]
*/
private $models = [];
/**
* @var ModuleCollection
*/
private $collection;
/**
* @return ModuleCollection
*/
protected function get_collection()
{
return $this->collection;
}
/**
* Constructor.
*
* Initializes the {@link $descriptor} property.
*
* @param ModuleCollection $collection
* @param array $descriptor
*/
public function __construct(ModuleCollection $collection, array $descriptor)
{
$this->collection = $collection;
$this->descriptor = $descriptor;
}
/**
* Returns the identifier of the module.
*
* @return string
*/
public function __toString()
{
return $this->id;
}
/**
* Returns the _flat_ version of the module's identifier.
*
* This method is the getter for the {@link $flat_id} magic property.
*
* @return string
*/
protected function get_flat_id()
{
return strtr($this->id, [
'.' => '_',
'-' => '_'
]);
}
/**
* Returns the primary model of the module.
*
* This is the getter for the {@link $model} magic property.
*
* @return ActiveRecord\Model
*/
protected function get_model()
{
return $this->model();
}
/**
* Returns the module title, translated to the current language.
*
* @return string
*
* @deprecated
*/
protected function get_title()
{
$default = isset($this->descriptor[Descriptor::TITLE]) ? $this->descriptor[Descriptor::TITLE] : 'Undefined';
return $this->app->translate($this->flat_id, [], [ 'scope' => 'module_title', 'default' => $default ]);
}
/**
* Returns the parent module.
*
* @return Module|null
*/
protected function get_parent()
{
return $this->descriptor[Descriptor::INHERITS];
}
/**
* Checks if the module is installed.
*
* @param ErrorCollection $errors Error collection.
*
* @return mixed `true` if the module is installed, `false` if the module
* (or parts of) is not installed, `null` if the module has no installation.
*/
public function is_installed(ErrorCollection $errors)
{
if (empty($this->descriptor[Descriptor::MODELS]))
{
return null;
}
$rc = true;
foreach ($this->descriptor[Descriptor::MODELS] as $name => $tags)
{
if (!$this->model($name)->is_installed())
{
$errors->add($this->id, "The model %name is not installed.", [
'name' => $name
]);
$rc = false;
}
}
return $rc;
}
/**
* Install the module.
*
* If the module has models they are installed.
*
* @param ErrorCollection $errors Error collection.
*
* @return boolean|null true if the module has successfully been installed, false if the
* module (or parts of the module) fails to install or null if the module has
* no installation process.
*/
public function install(ErrorCollection $errors)
{
if (empty($this->descriptor[Descriptor::MODELS]))
{
return null;
}
$rc = true;
foreach ($this->descriptor[Descriptor::MODELS] as $name => $tags)
{
$model = $this->model($name);
if ($model->is_installed())
{
continue;
}
try
{
$model->install();
}
catch (\Exception $e)
{
$errors->add($this->id, "Unable to install model %model: !message", [
'model' => $name,
'message' => $e->getMessage()
]);
$rc = false;
}
}
return $rc;
}
/**
* Uninstall the module.
*
* Basically it uninstall the models installed by the module.
*
* @return boolean|null `true` if the module was successfully uninstalled. `false` if the module
* (or parts of the module) failed to uninstall. `null` if there is no uninstall process.
*/
public function uninstall()
{
if (empty($this->descriptor[Descriptor::MODELS]))
{
return null;
}
$rc = true;
foreach ($this->descriptor[Descriptor::MODELS] as $name => $tags)
{
$model = $this->model($name);
if (!$model->is_installed())
{
continue;
}
if (!$model->uninstall())
{
$rc = false;
}
}
return $rc;
}
/**
* Get a model from the module.
*
* If the model has not been created yet, it is created on the fly.
*
* @param string $which The identifier of the model to get.
*
* @return Model The requested model.
*
* @throws ModelNotDefined when the model is not defined by the module.
* @throws \RuntimeException when the class of the model does not exists.
*/
public function model($which = 'primary')
{
if (empty($this->models[$which]))
{
if (empty($this->descriptor[Descriptor::MODELS][$which]))
{
throw new ModelNotDefined($which);
}
#
# resolve model tags
#
$callback = "resolve_{$which}_model_tags";
if (!method_exists($this, $callback))
{
$callback = 'resolve_model_tags';
}
$attributes = $this->$callback($this->descriptor[Descriptor::MODELS][$which], $which);
#
# COMPATIBILITY WITH 'inherit'
#
if ($attributes instanceof Model)
{
$this->models[$which] = $attributes;
return $attributes;
}
#
# create model
#
$class = $attributes[Model::CLASSNAME];
if (!class_exists($class))
{
throw new \RuntimeException(format("Unable to instantiate model %model, the class %class does not exists.", [
'model' => "$this->id/$which",
'class' => $class
]));
}
$this->models[$which] = new $class($this->app->models, $attributes);
}
#
# return cached model
#
return $this->models[$which];
}
/**
* Resolves model tags.
*
* @param array|string $tags
* @param string $which
*
* @return array
*/
protected function resolve_model_tags($tags, $which)
{
$app = $this->app;
#
# The model may use another model, in which case the model to use is defined using a
# string e.g. 'contents' or 'terms/nodes'
#
if (is_string($tags))
{
$model_name = $tags;
if ($model_name == 'inherit')
{
$class = get_parent_class($this);
foreach ($app->modules->descriptors as $module_id => $descriptor)
{
if ($class != $descriptor['class'])
{
continue;
}
$model_name = $app->models[$module_id];
break;
}
}
$tags = [ Model::EXTENDING => $model_name ];
}
#
# defaults
#
$id = $this->id;
$tags += [
Model::CONNECTION => 'primary',
Model::ID => $which == 'primary' ? $id : $id . '/' . $which,
Model::EXTENDING => null
];
if (empty($tags[Model::NAME]))
{
$tags[Model::NAME] = ModuleCollection::format_model_name($id, $which);
}
#
# relations
#
if (isset($tags[Model::EXTENDING]))
{
$extends = &$tags[Model::EXTENDING];
if (is_string($extends))
{
$extends = $this->app->models[$extends];
}
if (!$tags[Model::CLASSNAME])
{
$tags[Model::CLASSNAME] = get_class($extends);
}
}
#
#
#
if (isset($tags[Model::IMPLEMENTING]))
{
$implements =& $tags[Model::IMPLEMENTING];
foreach ($implements as &$implement)
{
if (isset($implement['model']))
{
list($implement_id, $implement_which) = explode('/', $implement['model']) + [ 1 => 'primary' ];
if ($id == $implement_id && $which == $implement_which)
{
throw new \RuntimeException(format('Model %module/%model implements itself !', [
'%module' => $id,
'%model' => $which
]));
}
$module = ($implement_id == $id) ? $this : app()->modules[$implement_id];
$implement['table'] = $module->model($implement_which);
}
else if (is_string($implement['table']))
{
throw new \RuntimeException(format('Model %model of module %module implements a table: %table', [
'%model' => $which,
'%module' => $id,
'%table' => $implement['table']
]));
}
}
}
#
# default class, if none was defined.
#
if (empty($tags[Model::CLASSNAME]))
{
$tags[Model::CLASSNAME] = 'ICanBoogie\ActiveRecord\Model';
}
#
# connection
#
$connection = $tags[Model::CONNECTION];
if (!($connection instanceof Connection))
{
$tags[Model::CONNECTION] = $this->app->connections[$connection];
}
return $tags;
}
/**
* Get a block.
*
* @param string $name The name of the block to get.
*
* @return mixed Depends on the implementation. Should return a string or an object
* implementing `__toString`.
*
* @throws \RuntimeException if the block is not defined.
*/
public function getBlock($name)
{
$args = func_get_args();
array_shift($args);
$callback = 'block_' . $name;
if (!method_exists($this, $callback))
{
throw new \RuntimeException(format('The %method method is missing from the %module module to create block %type.', [
'%method' => $callback,
'%module' => $this->id,
'%type' => $name
]));
}
return $this->$callback(...$args);
}
}