Class ModuleCollection
A module collection.
- ICanBoogie\Module\ModuleCollection implements ArrayAccess, IteratorAggregate uses ICanBoogie\Accessor\AccessorTrait (not available)
Methods summary
public static
format_model_name( string $module_id
, string $model_id
= 'primary' )
: string
Formats a SQL table name given the module id and the model id.
Formats a SQL table name given the module id and the model id.
Parameters
$module_id
$model_id
Returns
public
__construct( array $paths
, ICanBoogie\Storage\Storage $cache
= null )
The index for the available modules is created with the accessor object.
The index for the available modules is created with the accessor object.
Parameters
$paths
- The paths to look for modules.
$cache
- The cache to use for the module indexes.
protected
revoke_constructions( void )
Revokes constructions.
Revokes constructions.
The following properties are revoked:
- $enabled_modules_descriptors
- $disabled_modules_descriptors
- $catalog_paths
- $config_paths
The method is usually invoked when modules state changes, in order to reflect these changes.
public
enable( string $module_id
)
Enables a module.
Enables a module.
Parameters
$module_id
- Module identifier.
public
disable( string $module_id
)
Disables a module.
Disables a module.
Parameters
$module_id
- Module identifier.
public
offsetSet( string $module_id
, boolean $enable
)
Used to enable or disable a module using the specified offset as a module identifier.
Used to enable or disable a module using the specified offset as a module identifier.
Parameters
$module_id
- Identifier of the module.
$enable
- Status of the module:
true
for enabled,false
for disabled.
Implementation of
ArrayAccess::offsetSet()
public
offsetUnset( string $module_id
)
Disables a module by setting the Descriptor::DISABLED key of its descriptor to true
.
Disables a module by setting the Descriptor::DISABLED key of its descriptor to true
.
Parameters
$module_id
- Module identifier.
Implementation of
ArrayAccess::offsetUnset()
public
offsetExists( string $module_id
)
: boolean
Checks the availability of a module.
Checks the availability of a module.
A module is considered available when its descriptor is defined, and the Descriptor::DISABLED key of its descriptor is empty.
Note: empty()
will call ICanBoogie\Module\ModuleCollection::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.
Parameters
$module_id
- Module identifier.
Returns
Whether or not the module is available.
Implementation of
ArrayAccess::offsetExists()
public
offsetGet( string $module_id
)
: ICanBoogie\Module
Returns a module object.
Returns a module object.
If the autorun property is true
, the ICanBoogie\Module\Module::run() method of the module
is invoked upon its first loading.
Parameters
$module_id
- Module identifier.
Returns
Throws
ICanBoogie\Module\ModuleNotDefined
when the requested module is not defined.
ICanBoogie\Module\ModuleIsDisabled
when the module is disabled.
ICanBoogie\Module\ModuleConstructorMissing
when the class that should be used to create its instance is not defined.
Implementation of
ArrayAccess::offsetGet()
public
getIterator( void )
: ArrayIterator
Returns an iterator for instantiated modules.
Returns an iterator for instantiated modules.
Returns
Implementation of
IteratorAggregate::getIterator()
public
filter_descriptors( callable $filter
)
: array
Filter descriptors.
Filter descriptors.
Parameters
$filter
Returns
public
filter_descriptors_by_users( string $module_id
, boolean $all
= self::ONLY_ENABLED_MODULES )
: array
Returns the modules using a module.
Returns the modules using a module.
Parameters
$module_id
- Used module identifier.
$all
One of
ICanBoogie\Module\ModuleCollection::ONLY_ENABLED_MODULES
orICanBoogie\Module\ModuleCollection::ALL_MODULES
. Default:ICanBoogie\Module\ModuleCollection::ONLY_ENABLED_MODULES
.
Returns
A array of filtered descriptors.
protected
lazy_get_index( void )
: array
Indexes the modules found in the paths specified during construct.
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
.
Returns
protected
index_modules( void )
: array
Construct the index for the 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.
Returns
protected
index_descriptors( array $paths
)
: array
Indexes descriptors.
Indexes descriptors.
The descriptors are extended with the following default values:
- (string) category: null.
- (string) class: ModuleCollection\
- (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.
Parameters
$paths
Returns
protected
collect_descriptors( array $paths
)
: array
Collects descriptors from paths.
Collects descriptors from paths.
Parameters
$paths
Returns
protected
read_descriptor( string $module_id
, string $path
)
: array
Reads the descriptor file.
Reads the descriptor file.
The descriptor file is extended with private values and default values.
Parameters
$module_id
- The identifier of the module.
$path
- The path to the directory where the descriptor is located.
Returns
Throws
in the following situations: - The descriptor is not an array - The Descriptor::TITLE key is empty. - The Descriptor::NS key is empty.
protected
alter_descriptor( array $descriptor
)
: array
Alters the module descriptor.
Alters the module descriptor.
Parameters
$descriptor
- Descriptor of the module to index.
Returns
The altered descriptor.
protected
lazy_get_disabled_modules_descriptors( void )
: array
Returns the descriptors of the disabled modules.
Returns the descriptors of the disabled modules.
This method is the getter for the $disabled_modules_descriptors magic property.
Returns
protected
lazy_get_enabled_modules_descriptors( void )
: array
Returns the descriptors of the enabled modules.
Returns the descriptors of the enabled modules.
This method is the getter for the $enabled_modules_descriptors magic property.
Returns
protected
lazy_get_locale_paths( void )
: array
Returns the paths of the enabled modules which have a locale
folder.
Returns the paths of the enabled modules which have a locale
folder.
Returns
protected
lazy_get_config_paths( void )
: array
Returns the paths of the enabled modules which have a config
folder.
Returns the paths of the enabled modules which have a config
folder.
Returns
public
order_ids( array $ids
, array $descriptors
= null )
: array
Orders the module ids provided according to module inheritance and weight.
Orders the module ids provided according to module inheritance and weight.
Parameters
$ids
- The module ids to order.
$descriptors
- Module descriptors.
Returns
public
usage( string $module_id
, boolean $all
= self::ONLY_ENABLED_MODULES )
: integer
Returns the usage of a module by other modules.
Returns the usage of a module by other modules.
Parameters
$module_id
- The identifier of the module.
$all
One of
ICanBoogie\Module\ModuleCollection::ONLY_ENABLED_MODULES
orICanBoogie\Module\ModuleCollection::ALL_MODULES
. Default:ICanBoogie\Module\ModuleCollection::ONLY_ENABLED_MODULES
.
Returns
public
is_inheriting( string $module_id
, string $parent_id
)
: boolean
Checks if a module inherits from another.
Checks if a module inherits from another.
Parameters
$module_id
- Module identifier.
$parent_id
- Identifier of the parent module.
Returns
true
if the module inherits from the other.
public
install( ICanBoogie\ErrorCollection $errors
= null )
: ICanBoogie\ErrorCollection
Install all the enabled modules.
Install all the enabled modules.
Parameters
$errors
Returns
Throws
ICanBoogie\Module\ModuleCollectionInstallFailed
if an error occurs.
public
resolve_classname( string $unqualified_classname
, string|ICanBoogie\Module
$module_id
, array & $tried
= [] )
: string|false
Resolves a class name using module inheritance.
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.
Parameters
$unqualified_classname
$module_id
$tried
Returns
The resolved file name, or
false
if it could not be resolved.
Throws
ICanBoogie\Module\ModuleNotDefined
if the specified module, or the module specified by Descriptor::INHERITS is not defined.
protected
change_module_availability( string $module_id
, boolean $available
)
Changes a module availability.
Changes a module availability.
Parameters
$module_id
$available
protected
ensure_modules_are_indexed( void )
Ensures that modules are indexed, index them if not.
Ensures that modules are indexed, index them if not.
protected
assert_module_is_defined( string $module_id
)
Asserts that a module is defined.
Asserts that a module is defined.
Parameters
$module_id
- Module identifier.
Throws
ICanBoogie\Module\ModuleNotDefined
if the module is not defined.
protected
assert_module_is_enabled( string $module_id
)
Asserts that a module is enabled.
Asserts that a module is enabled.
Parameters
$module_id
Throws
ICanBoogie\Module\ModuleIsDisabled
if the module is disabled.
protected
assert_constructor_exists( string $module_id
, string $class
)
Asserts that a module constructor exists.
Asserts that a module constructor exists.
Parameters
$module_id
- Module identifier.
$class
- Constructor class.
Constants summary
ONLY_ENABLED_MODULES
: boolean
May be used with the ICanBoogie\Module\ModuleCollection::filter_descriptors_by_users()
method to filter the descriptors
of enabled modules.
May be used with the ICanBoogie\Module\ModuleCollection::filter_descriptors_by_users()
method to filter the descriptors
of enabled modules.
false
ALL_MODULES
: boolean
May be used with the ICanBoogie\Module\ModuleCollection::filter_descriptors_by_users()
method to filter the descriptors
of all modules, enabled or not.
May be used with the ICanBoogie\Module\ModuleCollection::filter_descriptors_by_users()
method to filter the descriptors
of all modules, enabled or not.
true
Properties summary
protected
$paths
: array
The paths where modules can be found.
The paths where modules can be found.
[]