ICanBoogie/Module 4.0.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Module
      • ModuleCollection
      • Operation

Classes

  • Descriptor
  • ForwardedOperationDispatcher
  • Hooks
  • ModelCollection
  • ModuleCollection
  • ModuleOperationDispatcher
  • ModuleRouteDefinition
  • ModuleTemplateResolver

Traits

  • ApplicationBindings
  • ControllerBindings
  • ModuleRoute

Exceptions

  • ModuleCollectionInstallFailed
  • ModuleConstructorMissing
  • ModuleIsDisabled
  • ModuleNotDefined
   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  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 
<?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\Accessor\AccessorTrait;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ErrorCollection;
use ICanBoogie\Module;
use ICanBoogie\Storage\Storage;
use ICanBoogie\Module\ModuleCollection\InstallableModulesFilter;

use function ICanBoogie\format;
use function ICanBoogie\camelize;
use function ICanBoogie\singularize;
use function ICanBoogie\stable_sort;

/**
 * A module collection.
 *
 * @property-read array $config_paths Paths of the enabled modules having a `config` directory.
 * @property-read array $locale_paths Paths of the enabled modules having a `locale` directory.
 * @property-read array $disabled_modules_descriptors Descriptors of the disabled modules.
 * @property-read array $enabled_modules_descriptors Descriptors of the enabled modules.
 * @property-read array $index Index for the modules.
 */
class ModuleCollection implements \ArrayAccess, \IteratorAggregate
{
    use AccessorTrait;

    /**
     * May be used with the {@link filter_descriptors_by_users()} method to filter the descriptors
     * of enabled modules.
     */
    const ONLY_ENABLED_MODULES = false;

    /**
     * May be used with the {@link filter_descriptors_by_users()} method to filter the descriptors
     * of all modules, enabled or not.
     */
    const ALL_MODULES = true;

    /**
     * Formats a SQL table name given the module id and the model id.
     *
     * @param string $module_id
     * @param string $model_id
     *
     * @return string
     */
    static public function format_model_name($module_id, $model_id = 'primary')
    {
        return preg_replace('#[^0-9a-zA-Z$_]#', '_', $module_id) . ($model_id == 'primary' ? '' : '__' . $model_id);
    }

    /**
     * The descriptors for the modules.
     *
     * @var array
     */
    public $descriptors = [];

    /**
     * The paths where modules can be found.
     *
     * @var array
     */
    protected $paths = [];

    /**
     * A cache for the modules index.
     *
     * @var Storage
     */
    protected $cache;

    /**
     * Instantiated modules.
     *
     * @var Module[]
     */
    protected $modules = [];

    /**
     * The index for the available modules is created with the accessor object.
     *
     * @param array $paths The paths to look for modules.
     * @param Storage $cache The cache to use for the module indexes.
     */
    public function __construct($paths, Storage $cache = null)
    {
        $this->paths = $paths;
        $this->cache = $cache;
    }

    /**
     * Revokes constructions.
     *
     * The following properties are revoked:
     *
     * - {@link $enabled_modules_descriptors}
     * - {@link $disabled_modules_descriptors}
     * - {@link $catalog_paths}
     * - {@link $config_paths}
     *
     * The method is usually invoked when modules state changes, in order to reflect these
     * changes.
     */
    protected function revoke_constructions()
    {
        unset($this->enabled_modules_descriptors);
        unset($this->disabled_modules_descriptors);
        unset($this->catalog_paths);
        unset($this->config_paths);
    }

    /**
     * Enables a module.
     *
     * @param string $module_id Module identifier.
     */
    public function enable($module_id)
    {
        $this->change_module_availability($module_id, false);
    }

    /**
     * Disables a module.
     *
     * @param string $module_id Module identifier.
     */
    public function disable($module_id)
    {
        $this->change_module_availability($module_id, true);
    }

    /**
     * Used to enable or disable a module using the specified offset as a module identifier.
     *
     * @param string $module_id Identifier of the module.
     * @param bool $enable Status of the module: `true` for enabled, `false` for disabled.
     */
    public function offsetSet($module_id, $enable)
    {
        $this->change_module_availability($module_id, $enable);
    }

    /**
     * Disables a module by setting the {@link Descriptor::DISABLED} key of its descriptor to `true`.
     *
     * @param string $module_id Module identifier.
     */
    public function offsetUnset($module_id)
    {
        $this->change_module_availability($module_id, false);
    }

    /**
     * Checks the availability of a module.
     *
     * A module is considered available when its descriptor is defined, and the
     * {@link Descriptor::DISABLED} key of its descriptor is empty.
     *
     * Note: `empty()` will call {@link offsetGet()} to check if the value is not empty. So, unless
     * you want to use the module you check, better check using `!isset()`, otherwise the module
     * you check is loaded too.
     *
     * @param string $module_id Module identifier.
     *
     * @return boolean Whether or not the module is available.
     */
    public function offsetExists($module_id)
    {
        $this->ensure_modules_are_indexed();

        $descriptors = $this->descriptors;

        return isset($descriptors[$module_id])
        && empty($descriptors[$module_id][Descriptor::DISABLED]);
    }

    /**
     * Returns a module object.
     *
     * If the {@link autorun} property is `true`, the {@link Module::run()} method of the module
     * is invoked upon its first loading.
     *
     * @param string $module_id Module identifier.
     *
     * @throws ModuleNotDefined when the requested module is not defined.
     *
     * @throws ModuleIsDisabled when the module is disabled.
     *
     * @throws ModuleConstructorMissing when the class that should be used to create its instance
     * is not defined.
     *
     * @return Module
     */
    public function offsetGet($module_id)
    {
        $this->ensure_modules_are_indexed();

        if (isset($this->modules[$module_id]))
        {
            return $this->modules[$module_id];
        }

        return $this->modules[$module_id] = $this->instantiate_module($module_id);
    }

    /**
     * Returns an iterator for instantiated modules.
     *
     * @return \ArrayIterator
     */
    public function getIterator()
    {
        $this->ensure_modules_are_indexed();

        return new \ArrayIterator($this->modules);
    }

    /**
     * Filter descriptors.
     *
     * @param callable $filter
     *
     * @return array
     */
    public function filter_descriptors(callable $filter)
    {
        return array_filter($this->descriptors, $filter);
    }

    /**
     * Returns the modules using a module.
     *
     * @param string $module_id Used module identifier.
     * @param bool $all One of {@link ONLY_ENABLED_MODULES} or {@link ALL_MODULES}.
     * Default: {@link ONLY_ENABLED_MODULES}.
     *
     * @return array A array of filtered descriptors.
     */
    public function filter_descriptors_by_users($module_id, $all = self::ONLY_ENABLED_MODULES)
    {
        $users = [];
        $descriptors = $all ? $this->descriptors : $this->enabled_modules_descriptors;

        foreach ($descriptors as $user_id => $descriptor)
        {
            if ($descriptor[Descriptor::INHERITS] == $module_id
            || in_array($module_id, $descriptor[Descriptor::REQUIRES]))
            {
                $users[$user_id] = $descriptor;
            }
        }

        return $users;
    }

    /**
     * Indexes the modules found in the paths specified during construct.
     *
     * The index is made of an array of descriptors, an array of catalogs paths, an array of
     * configs path, and finally an array of configs constructors.
     *
     * The method also creates a `DIR` constant for each module. The constant is defined in the
     * namespace of the module e.g. `Icybee\ModuleCollection\Nodes\DIR`.
     *
     * @return array
     */
    protected function lazy_get_index()
    {
        $index = $this->obtain_index();
        $this->descriptors = $descriptors = $index['descriptors'];
        $this->define_constants($descriptors);

        return $index;
    }

    /**
     * Obtain index either from cache or by building it.
     *
     * @return array|mixed|null
     */
    private function obtain_index()
    {
        $cache = $this->cache;

        if ($cache)
        {
            $key = 'cached_modules_' . substr(sha1(implode('#', $this->paths)), 0, 8);
            $index = $cache->retrieve($key);

            if ($index)
            {
                return $index;
            }

            $index = $this->index_modules();
            $cache->store($key, $index);

            return $index;
        }

        return $this->index_modules();
    }

    /**
     * Construct the index for the modules.
     *
     * The index contains the following values:
     *
     * - (array) descriptors: The descriptors of the modules, ordered by weight.
     * - (array) catalogs: Absolute paths to locale catalog directories.
     * - (array) configs: Absolute paths to config directories.
     * - (array) classes aliases: An array of _key/value_ pairs where _key_ is the alias of a class
     * and _value_ if the real class.
     *
     * @return array
     */
    protected function index_modules()
    {
        $descriptors = $this->paths ? $this->index_descriptors($this->paths) : [];
        $catalogs = [];
        $configs = [];

        foreach ($descriptors as $id => $descriptor)
        {
            $path = $descriptor[Descriptor::PATH];

            if ($descriptor['__has_locale'])
            {
                $catalogs[] = $path . 'locale';
            }

            if ($descriptor['__has_config'])
            {
                $configs[] = $path . 'config';
            }
        }

        return [

            'descriptors' => $descriptors,
            'catalogs' => $catalogs,
            'configs' => $configs

        ];
    }

    /**
     * Indexes descriptors.
     *
     * The descriptors are extended with the following default values:
     *
     * - (string) category: null.
     * - (string) class: ModuleCollection\<normalized_module_part>
     * - (string) description: null.
     * - (bool) disabled: false if required, true otherwise.
     * - (string) extends: null.
     * - (string) id: The module's identifier.
     * - (array) models: Empty array.
     * - (string) path: The absolute path to the module directory.
     * - (string) permission: null.
     * - (array) permissions: Empty array.
     * - (bool) startup: false.
     * - (bool) required: false.
     * - (array) requires: Empty array.
     * - (string) weight: 0.
     *
     * The descriptors are ordered according to their inheritance and weight.
     *
     * @param array $paths
     *
     * @return array
     */
    protected function index_descriptors(array $paths)
    {
        $descriptors = $this->collect_descriptors($paths);

        if (!$descriptors)
        {
            return [];
        }

        #
        # Compute inheritance.
        #

        $find_parents = function($id, &$parents = []) use (&$find_parents, &$descriptors)
        {
            $parent = $descriptors[$id][Descriptor::INHERITS];

            if ($parent)
            {
                $parents[] = $parent;

                $find_parents($parent, $parents);
            }

            return $parents;
        };

        foreach ($descriptors as $id => &$descriptor)
        {
            $descriptor['__parents'] = $find_parents($id);
        }

        #
        # Orders descriptors according to their weight.
        #

        $ordered_ids = $this->order_ids(array_keys($descriptors), $descriptors);
        $descriptors = array_merge(array_combine($ordered_ids, $ordered_ids), $descriptors);

        foreach ($descriptors as $id => &$descriptor)
        {
            foreach ($descriptor[Descriptor::MODELS] as $model_id => &$model_descriptor)
            {
                if ($model_descriptor != 'inherit')
                {
                    continue;
                }

                $parent_descriptor = $descriptors[$descriptor[Descriptor::INHERITS]];
                $model_descriptor = [

                    Model::EXTENDING => $parent_descriptor[Descriptor::ID] . '/' . $model_id

                ];
            }

            $descriptor = $this->alter_descriptor($descriptor);
        }

        return $descriptors;
    }

    /**
     * Collects descriptors from paths.
     *
     * @param array $paths
     *
     * @return array
     */
    protected function collect_descriptors(array $paths)
    {
        $descriptors = [];

        foreach ($paths as $root)
        {
            $root = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
            $descriptor_path = $root . 'descriptor.php';

            if (file_exists($descriptor_path))
            {
                $id = basename(realpath($root));
                $descriptor = $this->read_descriptor($id, $root);

                $descriptors[$descriptor[Descriptor::ID]] = $descriptor;
            }
            else
            {
                try
                {
                    $dir = new \DirectoryIterator($root);
                }
                catch (\Exception $e)
                {
                    throw new \RuntimeException(format('Unable to open directory %root', [

                        'root' => $root

                    ]));
                }

                foreach ($dir as $file)
                {
                    if ($file->isDot() || !$file->isDir())
                    {
                        continue;
                    }

                    $id = $file->getFilename();
                    $path = $root . $id . DIRECTORY_SEPARATOR;
                    $descriptor = $this->read_descriptor($id, $path);

                    $descriptors[$descriptor[Descriptor::ID]] = $descriptor;
                }
            }
        }

        return $descriptors;
    }

    /**
     * Reads the descriptor file.
     *
     * The descriptor file is extended with private values and default values.
     *
     * @param string $module_id The identifier of the module.
     * @param string $path The path to the directory where the descriptor is located.
     *
     * @throws \InvalidArgumentException in the following situations:
     * - The descriptor is not an array
     * - The {@link Descriptor::TITLE} key is empty.
     * - The {@link Descriptor::NS} key is empty.
     *
     * @return array
     */
    protected function read_descriptor($module_id, $path)
    {
        $descriptor_path = $path . 'descriptor.php';
        $descriptor = require $descriptor_path;

        if (!is_array($descriptor))
        {
            throw new \InvalidArgumentException(format
            (
                '%var should be an array: %type given instead in %path', [

                    'var' => 'descriptor',
                    'type' => gettype($descriptor),
                    'path' => $descriptor_path

                ]
            ));
        }

        if (empty($descriptor[Descriptor::TITLE]))
        {
            throw new \InvalidArgumentException(format
            (
                'The %name value of the %id module descriptor is empty in %path.', [

                    'name' => Descriptor::TITLE,
                    'id' => $module_id,
                    'path' => $descriptor_path

                ]
            ));
        }

        if (empty($descriptor[Descriptor::NS]))
        {
            throw new \InvalidArgumentException(format
            (
                '%name is required. Invalid descriptor for module %id in %path.', [

                    'name' => Descriptor::NS,
                    'id' => $module_id,
                    'path' => $descriptor_path

                ]
            ));
        }

        return Descriptor::normalize($descriptor + [

            Descriptor::ID => $module_id,
            Descriptor::PATH => $path,

            '__has_config' => is_dir($path . 'config'),
            '__has_locale' => is_dir($path . 'locale'),
            '__parents' => []

        ]);
    }

    /**
     * Alters the module descriptor.
     *
     * @param array $descriptor Descriptor of the module to index.
     *
     * @return array The altered descriptor.
     */
    protected function alter_descriptor(array $descriptor)
    {
        $id = $descriptor[Descriptor::ID];
        $namespace = $descriptor[Descriptor::NS];

        # models and active records

        foreach ($descriptor[Descriptor::MODELS] as $model_id => &$definition)
        {
            if (!is_array($definition))
            {
                throw new \InvalidArgumentException(format('Model definition must be array, given: %value.', [

                    'value' => $definition

                ]));
            }

            $basename = $id;
            $separator_position = strrpos($basename, '.');

            if ($separator_position)
            {
                $basename = substr($basename, $separator_position + 1);
            }

            if (empty($definition[Model::NAME]))
            {
                $definition[Model::NAME] = self::format_model_name($id, $model_id);
            }

            if (empty($definition[Model::ACTIVERECORD_CLASS]))
            {
                $definition[Model::ACTIVERECORD_CLASS] = $namespace . '\\' . camelize(singularize($model_id == 'primary' ? $basename : $model_id));
            }

            if (empty($definition[Model::CLASSNAME]))
            {
                $definition[Model::CLASSNAME] = $definition[Model::ACTIVERECORD_CLASS] . 'Model';
            }
        }

        return $descriptor;
    }

    /**
     * Traverses the descriptors and create two array of descriptors: one for the disabled modules
     * and the other for enabled modules. The {@link $disabled_modules_descriptors} magic property
     * receives the descriptors of the disabled modules, while the {@link $enabled_modules_descriptors}
     * magic property receives the descriptors of the enabled modules.
     */
    private function sort_modules_descriptors()
    {
        $enabled = [];
        $disabled = [];

        $this->ensure_modules_are_indexed();

        foreach ($this->descriptors as $module_id => &$descriptor)
        {
            if (isset($this[$module_id]))
            {
                $enabled[$module_id] = $descriptor;
            }
            else
            {
                $disabled[$module_id] = $descriptor;
            }
        }

        $this->enabled_modules_descriptors = $enabled;
        $this->disabled_modules_descriptors = $disabled;
    }

    /**
     * Returns the descriptors of the disabled modules.
     *
     * This method is the getter for the {@link $disabled_modules_descriptors} magic property.
     *
     * @return array
     */
    protected function lazy_get_disabled_modules_descriptors()
    {
        $this->sort_modules_descriptors();

        return $this->disabled_modules_descriptors;
    }

    /**
     * Returns the descriptors of the enabled modules.
     *
     * This method is the getter for the {@link $enabled_modules_descriptors} magic property.
     *
     * @return array
     */
    protected function lazy_get_enabled_modules_descriptors()
    {
        $this->sort_modules_descriptors();

        return $this->enabled_modules_descriptors;
    }

    /**
     * Returns the paths of the enabled modules which have a `locale` folder.
     *
     * @return array
     */
    protected function lazy_get_locale_paths()
    {
        $paths = [];

        foreach ($this->enabled_modules_descriptors as $module_id => $descriptor)
        {
            if (!$descriptor['__has_locale'])
            {
                continue;
            }

            $paths[] = $descriptor[Descriptor::PATH] . 'locale';
        }

        return $paths;
    }

    /**
     * Returns the paths of the enabled modules which have a `config` folder.
     *
     * @return array
     */
    protected function lazy_get_config_paths()
    {
        $paths = [];

        foreach ($this->enabled_modules_descriptors as $module_id => $descriptor)
        {
            if (!$descriptor['__has_config'])
            {
                continue;
            }

            $paths[$descriptor[Descriptor::PATH] . 'config'] = 0;
        }

        return $paths;
    }

    /**
     * Orders the module ids provided according to module inheritance and weight.
     *
     * @param array $ids The module ids to order.
     * @param array $descriptors Module descriptors.
     *
     * @return array
     */
    public function order_ids(array $ids, array $descriptors = null)
    {
        $ordered = [];
        $extends_weight = [];

        if ($descriptors === null)
        {
            $descriptors = $this->descriptors;
        }

        $count_extends = function($super_id) use (&$count_extends, &$descriptors)
        {
            $i = 0;

            foreach ($descriptors as $module_id => $descriptor)
            {
                if ($descriptor[Descriptor::INHERITS] !== $super_id)
                {
                    continue;
                }

                $i += 1 + $count_extends($module_id);
            }

            return $i;
        };

        $count_required = function($required_id) use (&$descriptors, &$extends_weight)
        {
            $i = 0;

            foreach ($descriptors as $module_id => $descriptor)
            {
                if (!in_array($required_id, $descriptor[Descriptor::REQUIRES]))
                {
                    continue;
                }

                $i += 1 + $extends_weight[$module_id];
            }

            return $i;
        };

        foreach ($ids as $module_id)
        {
            $extends_weight[$module_id] = $count_extends($module_id);
        }

        foreach ($ids as $module_id)
        {
            $ordered[$module_id] = -$extends_weight[$module_id] -$count_required($module_id) + $descriptors[$module_id][Descriptor::WEIGHT];
        }

        stable_sort($ordered);

        return array_keys($ordered);
    }

    /**
     * Returns the usage of a module by other modules.
     *
     * @param string $module_id The identifier of the module.
     * @param bool $all One of {@link ONLY_ENABLED_MODULES} or {@link ALL_MODULES}.
     * Default: {@link ONLY_ENABLED_MODULES}.
     *
     * @return int
     */
    public function usage($module_id, $all = self::ONLY_ENABLED_MODULES)
    {
        return count($this->filter_descriptors_by_users($module_id, $all));
    }

    /**
     * Checks if a module inherits from another.
     *
     * @param string $module_id Module identifier.
     * @param string $parent_id Identifier of the parent module.
     *
     * @return boolean `true` if the module inherits from the other.
     */
    public function is_inheriting($module_id, $parent_id)
    {
        while ($module_id)
        {
            if ($module_id == $parent_id)
            {
                return true;
            }

            $descriptor = $this->descriptors[$module_id];

            $module_id = empty($descriptor[Descriptor::INHERITS]) ? null : $descriptor[Descriptor::INHERITS];
        }

        return false;
    }

    /**
     * Install all the enabled modules.
     *
     * @param ErrorCollection|null $errors
     *
     * @return ErrorCollection
     *
     * @throws ModuleCollectionInstallFailed if an error occurs.
     */
    public function install(ErrorCollection $errors = null)
    {
        if (!$errors)
        {
            $errors = new ErrorCollection;
        }

        foreach (array_keys($this->filter_descriptors(new InstallableModulesFilter($this))) as $module_id)
        {
            try
            {
                $this[$module_id]->install($errors);
            }
            catch (\Exception $e)
            {
                $errors[$module_id] = $e;
            }
        }

        if ($errors->count())
        {
            throw new ModuleCollectionInstallFailed($errors);
        }

        return $errors;
    }

    /**
     * Resolves a class name using module inheritance.
     *
     * To resolve a given class name, the method checks in each module namespace—starting from the
     * specified module—if the class exists. If it does, it returns its fully qualified name.
     *
     * @param string $unqualified_classname
     * @param string|Module $module_id
     * @param array $tried
     *
     * @return string|false The resolved file name, or `false` if it could not be resolved.
     *
     * @throws ModuleNotDefined if the specified module, or the module specified by
     * {@link Descriptor::INHERITS} is not defined.
     */
    public function resolve_classname($unqualified_classname, $module_id, array &$tried = [])
    {
        if ($module_id instanceof Module)
        {
            $module_id = $module_id->id;
        }

        while ($module_id)
        {
            $this->assert_module_is_defined($module_id);

            $descriptor = $this->descriptors[$module_id];
            $fully_qualified_classname = $descriptor[Descriptor::NS] . '\\' . $unqualified_classname;
            $tried[] = $fully_qualified_classname;

            if (class_exists($fully_qualified_classname, true))
            {
                return $fully_qualified_classname;
            }

            $module_id = $descriptor[Descriptor::INHERITS];
        }

        return false;
    }

    /**
     * Changes a module availability.
     *
     * @param string $module_id
     * @param bool $available
     */
    protected function change_module_availability($module_id, $available)
    {
        $this->ensure_modules_are_indexed();

        if (empty($this->descriptors[$module_id]))
        {
            return;
        }

        $this->descriptors[$module_id][Descriptor::DISABLED] = $available;
        $this->revoke_constructions();
    }

    /**
     * Ensures that modules are indexed, index them if not.
     */
    protected function ensure_modules_are_indexed()
    {
        $this->index;
    }

    /**
     * Asserts that a module is defined.
     *
     * @param string $module_id Module identifier.
     *
     * @throws ModuleNotDefined if the module is not defined.
     */
    protected function assert_module_is_defined($module_id)
    {
        if (empty($this->descriptors[$module_id]))
        {
            throw new ModuleNotDefined($module_id);
        }
    }

    /**
     * Asserts that a module is enabled.
     *
     * @param string $module_id
     *
     * @throws ModuleIsDisabled if the module is disabled.
     */
    protected function assert_module_is_enabled($module_id)
    {
        if (!empty($this->descriptors[$module_id][Descriptor::DISABLED]))
        {
            throw new ModuleIsDisabled($module_id);
        }
    }

    /**
     * Asserts that a module constructor exists.
     *
     * @param string $module_id Module identifier.
     * @param string $class Constructor class.
     */
    protected function assert_constructor_exists($module_id, $class)
    {
        if (!class_exists($class, true))
        {
            throw new ModuleConstructorMissing($module_id, $class);
        }
    }

    /**
     * Instantiate a module.
     *
     * @param string $module_id Module identifier.
     *
     * @return Module
     */
    protected function instantiate_module($module_id)
    {
        $this->assert_module_is_defined($module_id);
        $this->assert_module_is_enabled($module_id);

        $descriptor = $this->descriptors[$module_id];
        $class = $descriptor[Descriptor::CLASSNAME];

        $this->assert_constructor_exists($module_id, $class);

        $parent = &$descriptor[Descriptor::INHERITS];

        if ($parent)
        {
            $parent = $this[$parent];
        }

        return new $class($this, $descriptor);
    }

    /**
     * Defines module constants.
     *
     * @param array $descriptors
     */
    protected function define_constants(array $descriptors)
    {
        foreach ($descriptors as $descriptor)
        {
            $namespace = $descriptor[Descriptor::NS];
            $constant = $namespace . '\DIR';

            if (!defined($constant))
            {
                define($constant, $descriptor[Descriptor::PATH]);
            }
        }
    }
}
ICanBoogie/Module 4.0.x API documentation generated by ApiGen