ICanBoogie/Module 2.3.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Module

Classes

  • DeleteOperation
  • Module
  • SaveOperation
  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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 
<?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 Core $app
 */
class Module extends Object
{
    /**
     * Defines the category for the module.
     *
     * When modules are listed they are usually grouped by category. The category is also often
     * used to create the main navigation menu of the admin interface.
     *
     * The category of the module is translated within the `module_category` scope.
     *
     * @var string
     *
     * @deprecated
     */
    const T_CATEGORY = 'category';

    /**
     * Defines the PHP class of the module.
     *
     * If the class is not defined it is resolved during indexing using the {@link T_NAMESPACE}
     * tag and the following pattern : `<namespace>\Module`.
     *
     * @var string
     *
     * @deprecated
     */
    const T_CLASS = 'class';

    /**
     * Defines a short description of what the module do.
     *
     * @var string
     *
     * @deprecated
     */
    const T_DESCRIPTION = 'description';

    /**
     * Defines the state of the module.
     *
     * @var bool
     *
     * @deprecated
     */
    const T_DISABLED = 'disabled';

    /**
     * Defines the module that the module extends.
     *
     * @var string|\ICanBoogie\Module
     *
     * @deprecated
     */
    const T_EXTENDS = 'extends';

    /**
     * Defines the identifier of the module.
     *
     * If the identifier is not defined the name of the module directory is used instead.
     *
     * @var string
     *
     * @deprecated
     */
    const T_ID = 'id';

    /**
     * Defines the state of the module.
     *
     * Required modules are always enabled.
     *
     * @var bool
     *
     * @deprecated
     */
    const T_REQUIRED = 'required';

    /**
     * Defines the modules that the module requires.
     *
     * The required modules are defined using an array where each key/value pair is the identifier
     * of the module and the minimum version required.
     *
     * @var array[string]string
     *
     * @deprecated
     */
    const T_REQUIRES = 'requires';

    /**
     * Defines the models of the module.
     *
     * @var array[string]array|string
     *
     * @deprecated
     */
    const T_MODELS = 'models';

    /**
     * Defines the namespace of the module.
     *
     * This attribute must be defined at construct time.
     *
     * @var string
     *
     * @deprecated
     */
    const T_NAMESPACE = 'namespace';

    /**
     * Path to the module's directory.
     *
     * This tag is resolved when the module is indexed.
     *
     * @var string
     *
     * @deprecated
     */
    const T_PATH = 'path';

    /**
     * General permission of the module.
     *
     * @var string|int
     *
     * @deprecated
     */
    const T_PERMISSION = 'permission';

    /**
     * Defines the permissions added by the module.
     *
     * @var array[]string
     *
     * @deprecated
     */
    const T_PERMISSIONS = 'permissions';

    /**
     * Defines the title of the module.
     *
     * The title of the module is translated within the `module_title` scope.
     *
     * @var string
     *
     * @deprecated
     */
    const T_TITLE = 'title';

    /**
     * Defines the version (and revision) of the module.
     *
     * @var string
     *
     * @deprecated
     */
    const T_VERSION = 'version';

    /**
     * Defines the weight of the module.
     *
     * The weight of the module is resolved during modules indexing according to the
     * {@link T_EXTENDS} and {@link T_REQUIRES} tags.
     *
     * @var int
     *
     * @deprecated
     */
    const T_WEIGHT = 'weight';

    /*
     * 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.
     *
     * @var string
     */
    const OPERATION_SAVE = 'save';

    /**
     * Defines the name of the operation used to delete the records of the module.
     *
     * @var string
     */
    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 = [];

    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
     */
    protected function get_title()
    {
        $default = isset($this->descriptor[Descriptor::TITLE]) ? $this->descriptor[Descriptor::TITLE] : 'Undefined';

        return I18n\t($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 Errors $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(Errors $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[$this->id] = $errors->format("The model %name is not installed.", [

                    'name' => $name

                ]);

                $rc = false;
            }
        }

        return $rc;
    }

    /**
     * Install the module.
     *
     * If the module has models they are installed.
     *
     * @param Errors $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(Errors $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[$this->id] = $errors->format('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(\ICanBoogie\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];
    }

    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(\ICanBoogie\format('Model %module/%model implements itself !', [

                            '%module' => $id,
                            '%model' => $which

                        ]));
                    }

                    $module = ($implement_id == $id) ? $this : \ICanBoogie\app()->modules[$implement_id];

                    $implement['table'] = $module->model($implement_which);
                }
                else if (is_string($implement['table']))
                {
                    throw new \RuntimeException(\ICanBoogie\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(\ICanBoogie\format('The %method method is missing from the %module module to create block %type.', [

                '%method' => $callback,
                '%module' => $this->id,
                '%type' => $name

            ]));
        }

        return call_user_func_array([ $this, $callback ], $args);
    }
}
ICanBoogie/Module 2.3.x API documentation generated by ApiGen