Load a module.

@throws UnexpectedValueException
@param string $module
@param mixed|null $instance
@return mixed
552302
public function load($module, $instance = null)
    {
        if ( ! is_null($instance))
        {
            if ( ! $this->hasModule($module))
            {
                $this->addModule($module);
            }

            return ($this->instances[$module] = $instance);
        }

        if ($this->hasModule($module))
        {
            return ($this->instances[$module] = new $module);
        }

        throw new UnexpectedValueException("Module {$module} does not exist");
    }
422927
public static function instance($instance = null)
	{
		if ($instance !== null)
		{
			if ( ! array_key_exists($instance, static::$_instances))
			{
				return false;
			}

			return static::$_instances[$instance];
		}

		if (static::$_instance === null)
		{
			static::$_instance = static::forge();
		}

		return static::$_instance;
	}
919490
public function setModule($module) {
        if (!is_string($module) || empty($module)) {
            $errMessage = "The module value must be a string and not be empty";
            throw new \InvalidArgumentException($errMessage, GeneralErrors::VALUE_IS_NOT_OF_EXPECTED_TYPE);
        }

        $this->module = $module;
    }
671107
public static function load($arg)
    {
        if (is_object($arg) && $arg instanceof Entity) {
            $class = get_class($arg);
        } elseif (is_object($arg) && $arg instanceof Collection) {
            $class = $arg->getEntityClass();
        } elseif (is_object($arg) && $arg instanceof Reflection) {

            $class = $arg->getClassName();
            if (!isset(self::$registered[$class])) {
                return self::$registered[$class] = $arg;
            }
            return $arg;
        } elseif (is_string($arg)) {
            $class = $arg;
        } else {
            throw new Exception\InvalidArgumentException(
                "Entity identifier must be object, collection, class or name!",
                $arg
            );
        }

        if (!is_subclass_of($class, "UniMapper\Entity")) {
            $class = Convention::nameToClass($arg, Convention::ENTITY_MASK);
        }

        if (!class_exists($class)) {
            throw new Exception\InvalidArgumentException(
                "Entity class " . $class . " not found!"
            );
        }

        if (isset(self::$registered[$class])) {
            return self::$registered[$class];
        }

        return self::$registered[$class] = new Reflection($class);
    }
453264
public function setModule($name, $module, $parameters = null)
    {
        $name = strtolower($name);

        if (class_exists($module, true)) {

            /**
             * instantiation and initialization of
             * each module, return true on success
             * and false on error
             */
            $instance = new $module($parameters);
            $this->registerModule($name, $instance);
        }
        return $this;
    }
830102
public function module($module)
    {
        if (isset($this->modules[$module])) {
            return;
        }

        $this->log->write(Log::DEBUG, 'ModuleHandler', 'Loading module: %s', $module);

        $class        = "\\Modules\\{$module}\\Module";
        $moduleObject = new $class($module, $this->application, $this->fileManager);

        if (!$moduleObject instanceof Module) {
            throw new BadModuleException("Class {$class} does not extend Module class.");
        }

        $this->modules[$module] = $moduleObject;
        foreach ($moduleObject->getDependencies() as $name) {
            $this->module($name);
        }
    }
46268
public function setInstance(Instance $instance)
    {
        $this->instance = $instance;
        $this->exception = null;
        $this->connect();
        
        return $this;
    }
750535
public function addModule($module)
    {
        if ($module instanceof ModuleInterface) {
            if (in_array($module, $this->modules)) {
                throw new \InvalidArgumentException(
                    'This occurrence of '.get_class($module).' is already registered.'
                );
            }
            if ($module->getContainer() !== $this) {
                throw new \InvalidArgumentException(
                    'This occurrence of '.get_class($module).' is already registered in another module container.'
                );
            }

            $module->attachEvents();
            $this->modules[] = $module;

            return $this;
        }

        /** @var string $className */
        $className = $module;

        if (!is_subclass_of($className, $this->getModuleBaseClassName(), true)
            || !is_subclass_of($className, ModuleInterface::class)) {
            throw new \InvalidArgumentException(
                'Passed module class name needs to be a class extending '.$this->getModuleBaseClassName()
                .' and/or '.ModuleInterface::class
            );
        }

        if (isset($this->modules[$className])) {
            throw new \InvalidArgumentException(
                'Module '.$className.' is already registered.'
            );
        }

        if (!($this instanceof ModuleContainerInterface)) {
            throw new \RuntimeException(
                'Current module container uses the ModuleContainerTrait, but doesn\'t implement '
                .ModuleContainerInterface::class.', please implement it.'
            );
        }

        /** @var ModuleInterface $module */
        $module = new $className($this);
        $module->attachEvents();
        $this->modules[$className] = $module;

        return $this;
    }
607624
public static function get($module) {
		if(self::$modules === array())
			self::initialize();

		if(!isset(self::$modules[$module]))
			return false;

		return self::$modules[$module];
	}
371048
public static function loadModule($moduleName) {
        $moduleConf = self::getModuleConf($moduleName);

        try {

            if (isset($moduleConf['requires_php_extension']) && is_array($moduleConf['requires_php_extension'])) {
                self::checkPhpExtensions($moduleConf['requires_php_extension']);
            }

            $tmpModule = new \ReflectionClass('\\pff\\modules\\' . $moduleConf['class']);
            if ($tmpModule->isSubclassOf('\\pff\\Abs\\AModule')) {
                $moduleName = strtolower($moduleConf['name']);

                if (isset(self::$_modules[$moduleName])) { //Module has already been loaded
                    return self::$_modules[$moduleName];
                }

                self::$_modules[$moduleName] = $tmpModule->newInstance();
                self::$_modules[$moduleName]->setModuleName($moduleConf['name']);
                self::$_modules[$moduleName]->setModuleVersion($moduleConf['version']);
                self::$_modules[$moduleName]->setModuleDescription($moduleConf['desc']);
                self::$_modules[$moduleName]->setConfig(ServiceContainer::get('config'));
                self::$_modules[$moduleName]->setApp(ServiceContainer::get('app'));
                (isset($moduleConf['runBefore']))? $moduleLoadBefore = $moduleConf['runBefore']: $moduleLoadBefore = null ;

                if (isset ($moduleConf['requires']) && is_array($moduleConf['requires'])) {
                    self::$_modules[$moduleName]->setModuleRequirements($moduleConf['requires']);
                    foreach ($moduleConf['requires'] as $requiredModuleName) {
                        self::loadModule($requiredModuleName);
                        self::$_modules[$moduleName]->registerRequiredModule(self::$_modules[$requiredModuleName]);
                    }
                }

                if ($tmpModule->isSubclassOf('\pff\Iface\IHookProvider')) {
                    ServiceContainer::get('hookmanager')->registerHook(self::$_modules[$moduleName], $moduleName, $moduleLoadBefore);
                }

                return self::$_modules[$moduleName];

            } else {
                throw new ModuleException("Invalid module: " . $moduleConf['name']);
            }
        }
        catch (\ReflectionException $e) {
            throw new ModuleException("Unable to create module instance: " . $e->getMessage());
        }

    }
354660
private function loadModuleFromClass(string $name, $moduleClass) : IModule
    {
        if (is_callable($moduleClass)) {
            $moduleFactory = function () use ($moduleClass, $name) {
                return $moduleClass($this, $name);
            };
        } else {
            $moduleFactory = function () use ($moduleClass) {
                return $this->container->get($moduleClass);
            };
        }

        if (is_string($moduleClass) && !is_subclass_of($moduleClass, IModule::class, true)) {
            throw InvalidArgumentException::format(
                'Invalid module class defined within package \'%s\': expecting subclass of %s, %s given',
                $this->name, IModule::class, $moduleClass
            );
        }

        $this->loadingModules[$name] = true;

        /** @var IAuthSystem $authSystem */
        $authSystem = $this->container->get(IAuthSystem::class);

        /** @var IModule $module */
        $module = $this->container->bindForCallback(
            IAuthSystem::class,
            $authSystem->inPackageContext($this->container, $this->name),
            $moduleFactory
        );

        unset($this->loadingModules[$name]);

        if ($module->getName() !== $name) {
            throw InvalidArgumentException::format(
                'Invalid module class defined within package \'%s\': defined module name \'%s\' does not match module name \'%s\' from instance of %s',
                $this->name, $name, $module->getName(), get_class($module)
            );
        }

        return $module;
    }
1256626
public function getValueColumn($instance, string $name)
    {
        if (!$instance) {
            return null;
        }
        $parts = explode('.', $name);
        $part = array_shift($parts);

        if ($instance instanceof \Illuminate\Database\Eloquent\Collection) {
            $instance = $instance->pluck($part)->first();
        } else {
            $instance = $instance->{$part};
        }
        if (!empty($parts) && !is_null($instance)) {
            return $this->getValueColumn($instance, implode('.', $parts));
        }

        if ($instance instanceof \DateTimeInterface) {
            $instance = $this->getDateActive() ? $instance->format($this->getDateFormat()) : $instance;
        }

        return $instance;
    }
760076
private function load()
	{
		if(strlen($this->_method) == 0 || strlen($this->_key) == 0)
			return null;
		$m = mcrypt_module_open($this->_type, '', $this->_mode, '');
		if($m === false)
			return null;
		// This check is performed by mcrypt_generic_init, but it's better
		// to do it now, because mcrypt_generic_init does not return a
		// negative or false value if it fails.
		$ivsize = mcrypt_enc_get_iv_size($m);
		if(strlen($this->_iv) != $ivsize)
		{
			// In ECB (and some other modes), the IV is not used but still
			// required to have the this size by mcrypt_generic_init, so
			// let's make a fake one.
			if(strtolower($this->_mode) == "ecb")
				$ivsize = str_repeat("\0", $ivsize);
			else
				return null;
		}
		$r = @mcrypt_generic_init($m, $this->_key, $this->_iv);
		if($r < 0 || $r === false)
			return null;
		return $m;
	}
579768
protected static function getFieldValue($instance, $methodName)
    {
        try {
            $value = static::formatValue($instance->$methodName());
        } catch (\Exception $e) {
            $value = '<error>format error</error>';
        }

        return $value;
    }
843501
public function getClassByModule($module): ?string
    {
        if ($module instanceof Module || $module instanceof ModuleModel) {
            return Module::findClass($module->type);
        }

        if (\is_string($module)) {
            return Module::findClass($module);
        }

        return null;
    }
349480
public function loadModule($module)
    {
        $trgtModule = $module;
        if (is_array($module) && !empty($module)) {
            $trgtModule = $module[count($module)-1];
        }

        if (!in_array($trgtModule, $this->getActiveModules())) {
            $moduleLoadFile = $_SERVER['DOCUMENT_ROOT'] . '/../config/melis.module.load.php';
            if (file_exists($moduleLoadFile)) {
                $modules = include $_SERVER['DOCUMENT_ROOT'] . '/../config/melis.module.load.php';

                $moduleCount = count($modules);
                $insertAtIdx = $moduleCount - 1;
                array_splice($modules, $insertAtIdx, 0, $module);

                // create the module.load file
                $this->createModuleLoader('config/', $modules, [], []);
            }
        }

        return $this->isModuleLoaded($trgtModule);
    }
544003
public function checkRequirements($module_id)
    {
        $this->checkModuleId($module_id);
        $module = $this->module->getInfo($module_id);
        $this->checkCore($module);
        $this->checkPhpVersion($module);

        if ($this->module->isInstaller($module_id)) {
            $error = $this->translation->text('Modules with "installer" type cannot be installed/enabled when system is already set up');
            throw new UnexpectedValueException($error);
        }

        $this->checkDependenciesExtensions($module);
        $this->checkDependenciesModule($module_id);
        return true;
    }