## Class mr_helper_load

Description

MR Helper Load

This helper assists with dynamically including class files and also with dynamically instantiating those classes.

This class is namespace driven. A namespace is a relative directory path from Moodle's directory root. Examples:

- local/mr/framework
- blocks/helloworld

Files are loaded based on the passed path and current or passed namespace. Once combined, a file name is generated. Example: path = foo/bar, namespace = blocks/helloworld, derived file path would be /path/to/moodle/blocks/helloworld/foo/bar.php

Class names are derived in the same way, but no ".php" and the forwardslahes are replaced with underscores. So, the above example would load the class: blocks_helloworld_foo_bar.

There are currently two exceptions to the generation of class names:

1. For class names, blocks can be block as Moodle very often ignores the "s", but the namespace MUST be blocks
2. lib_mr gets switched to just mr. This is so all the mr classes are not lib_mr_*

- author: Mark Nielsen

Located in /helper/load.php (line 59)

    mr_helper_abstract
       |
       --mr_helper_load

Variable Summary
string $namespace
Method Summary
string classname (string $path, [string $namespace = NULL])
mixed direct (string $path, [array $arguments = array()], [string $namespace = NULL])
void file (string $path, [string $namespace = NULL])
string get_namespace ()
mixed instance (string $classname, [array $arguments = array()])
mixed plugin ([string $plugin = '*'])
string resolve_namespace (string $path, string $namespace)
mr_helper_load set_namespace (string $namespace)
void _set_helper_namespace (string $namespace)
Variables
string $namespace = 'local/mr/framework' (line 65)

Default name space for loading files/classes

- access: protected

#### Inherited Variables

Inherited from mr_helper_abstract

> mr_helper_abstract::$_helper_namespace

Methods
classname (line 275)

Generate a class name from path

- throws: coding_exception
- access: public

string classname (string $path, [string $namespace = NULL])

- string $path: Relative file path to class definition, EG: controller/mycontroller
- string $namespace: Alter namespace

direct (line 111)

Load an instance from a file

- throws: coding_exception
- access: public

mixed direct (string $path, [array $arguments = array()], [string $namespace = NULL])

- string $path: Relative file path to class definition, EG: controller/mycontroller Include "/*" at the end of path to load all files in path Include "/**" at the end of path to load all files in path and sub directories
- array $arguments: Arguments to pass to the constructor
- string $namespace: Alter namespace

file (line 254)

Load a file

- throws: coding_exception
- access: public

void file (string $path, [string $namespace = NULL])

- string $path: Relative file path to file, EG: controller/mycontroller
- string $namespace: Alter namespace

get_namespace (line 96)

Get the current namespace

- access: public

string get_namespace ()
instance (line 309)

Generate an instance of a class

- throws: coding_exception
- access: public

mixed instance (string $classname, [array $arguments = array()])

- string $classname: Name of class
- array $arguments: Arguments to pass to the class constructor

plugin (line 202)

Load plugins

This method does not augment the namespace, use set_namespace if it needs to be modified.

Based on the following layout:

- /namespace/plugin/PLUGINPATH/class.php

Where PLUGINPATH is a the plugin's directory or directory path, meaning you could have multiple levels of plugins.

Examples:

- /namespace/plugin/foo/class.php
- /namespace/plugin/foo/bar/class.php
- /namespace/plugin/foo/bar/baz/class.php

Do not name any plugins "base". This is reserved for any plugins that want to use /namespace/plugin/base/class.php as their plugin's base class.

Example Calls to this method

1. <?php
2. // If you have the single plugin type layout
3. $helper = new mr_helper('namespace/path');
4. $helper->load->plugin(); // Loads all plugins
5. $helper->load->plugin('*'); // Loads all plugins
6. $helper->load->plugin('bar'); // Loads a plugin with name 'bar'
8. // If you have the Multiple plugin type layout
9. $helper = new mr_helper('namespace/path');
10. $helper->load->plugin('foo/*'); // This will load all plugins of type foo
11. $helper->load->plugin('foo/bar'); // This will load the bar plugin of type foo
13. // If you have arguments, you can use any of the above and then
14. // keep passing arguments. The arguments will be passed to every
15. // plugin that gets created
16. $helper = new mr_helper('namespace/path');
18. // Loads plugin with name 'one' and passes $arg1 and $arg2 to its constructor
19. $helper->load->plugin('one', $arg1, $arg2);
21. // Loads all plugins and passes $arg1 and $arg2 to each plugin's constructor
22. $helper->load->plugin('*', $arg1, $arg2);
23. ?>

- throws: coding_exception
- access: public
- example: example

mixed plugin ([string $plugin = '*'])

- string $plugin: The plugin path

resolve_namespace (line 330)

Combine path with either passed namespace or with currently set namesapce.

- access: protected

string resolve_namespace (string $path, string $namespace)

- string $path: Relative file path to class definition, EG: controller/mycontroller
- string $namespace: Alter namespace

set_namespace (line 86)

Set namespace

- access: public

mr_helper_load set_namespace (string $namespace)

- string $namespace: New namespace, EG: blocks/reports

_set_helper_namespace (line 75)

Set the namespace of the mr_helper that created this instance

Also, set our current namespace

- access: public

void _set_helper_namespace (string $namespace)

- string $namespace: The namespace, EG: blocks/helloworld

Redefinition of:

mr_helper_abstract::_set_helper_namespace()
  Set the namespace of the mr_helper that created this instance

#### Inherited Methods

Inherited From mr_helper_abstract

> mr_helper_abstract::_set_helper_namespace()

Documentation generated on Thu, 28 Jun 2012 16:33:49 -0700 by phpDocumentor 1.4.3