/** * Hidden method to factory mvc components like models, views or controllers * * @param string $name * Components name * @param string $type * Components type * @param mixed $arguments * Optional arguments to be passed into the object to create * * @return Model|View|Controller */ private function MVCFactory(string $name, string $type, $arguments = null) { // Here we make sure that CSS and JS will correctly and only once be initiated! if (!in_array($this->name, self::$init_done)) { // Init css and js only on non ajax requests if (!$this->core->router->isAjax()) { $this->initCss(); $this->initJs(); } // Store our apps name to be initiated self::$init_done[] = $this->name; } $string = new CamelCase($name); $name = $string->camelize($name); // Create classname of component to create $class = $this->getNamespace() . '\\' . $type . '\\' . $name . $type; // By default each MVC component constructor needs at least a name and this app object as argument $args = [$name, $this]; // Add additional arguments if (isset($arguments)) { if (!is_array($arguments)) { $arguments = (array) $arguments; } foreach ($arguments as $arg) { $args[] = $arg; } } $classfile = new Classfile($class); if (!$classfile->exists()) { return false; } $object = $this->core->di->instance($class, $args); switch ($type) { case 'Controller': $object->model = $this->getModel($name); break; } return $object; }
/** * Get a singleton app object * * @param string $name * Name of app instance to get * * @return \Core\Framework\Amvc\App\AbstractApp */ public function &getAppInstance(string $name) { if (empty($name)) { throw new AppHandlerException('AppHandler::getAppInstance() method needs a camelized appname.'); } $string = new CamelCase($name); $name = $string->camelize(); // App instances are singletons! if (!array_key_exists($name, $this->instances)) { // Create class name $class = '\\AppHandler\\' . $name . '\\' . $name; // $filename = BASEDIR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; if (!file_exists($filename)) { throw new AppHandlerException(sprintf('AppHandler could not find an app classfile "%s" for app "%s"', $name, $filename)); } // Default arguments for each app instance $args = [$name, $this, $this->config->getStorage($name), 'core.page', 'core.di']; $instance = $this->di->instance($class, $args); if (!$instance instanceof AbstractApp) { throw new AppHandlerException('AppHandler must be an instance of AbstractApp class!'); } $instance->setName($name); $this->instances[$name] = $instance; } return $this->instances[$name]; }
/** * Creates a formcontrol and adds it by it's name to the controls list. * * @param string $type * The type of control to create * @param string $name * Name of the control. Ths name is used to bind the control to a model field. * * @return \Core\Html\Form\AbstractForm */ public function &addControl($control, $name = '', $label = '', $value = '', $description = '', $unbound = false) { $string = new CamelCase($control); $control = $this->factory->create('FormDesigner\\Controls\\' . $string->camelize() . 'Control'); // Set name $control->setName($name); // set contols name if ($unbound && method_exists($control, 'setUnbound')) { $control->setUnbound(); $control->removename(); } // Label set? if (!empty($label) && method_exists($control, 'setLabel')) { $control->setLabel($label); } if (!empty($value) && method_exists($control, 'setValue')) { $control->setValue($value); } if (!empty($description) && method_exists($control, 'setDescription')) { $control->setDescription($description); } if ($control instanceof Button) { $control->noLabel(); } // Create element $this->elementFactory('control', $control); return $control; }
/** * Sets controller action * * @param string $action */ public function setAction(string $action) { $string = new CamelCase($action); $this->action = $string->camelize(); }
/** * Get a singleton app object * * @param string $name * Name of app instance to get * * @return \Core\Framework\Amvc\App\AbstractApp */ public function &getAppInstance(string $name) { if (empty($name)) { throw new FrameworkException('Core::getAppInstance() needs an app name'); } $string = new CamelCase($name); $name = $string->camelize(); // App instances are singletons! if (!array_key_exists($name, $this->apps)) { // Create class name $class = '\\Apps\\' . $name . '\\' . $name; // $filename = $this->basedir . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; if (!file_exists($filename)) { throw new FrameworkException(sprintf('Apps could not find an app classfile "%s" for app "%s"', $name, $filename)); } // Default arguments for each app instance $args = [$name, $this->config->createStorage($name), $this]; $instance = $this->di->instance($class, $args); if (!$instance instanceof AbstractApp) { throw new FrameworkException('Apps must be an instance of AbstractApp class!'); } $instance->setName($name); $instance->language->setCode($this->config->get('Core', 'site.language.default')); $this->apps[$name] = $instance; } return $this->apps[$name]; }
/** * Creates and returns a singleton rule object * * @param string $rule_name * Name of the rule * * @return RuleInterface */ public function &createRule($rule_name) : RuleInterface { // Make sure that the rules name is camelcased $string = new CamelCase($rule_name); $rule_name = $string->camelize(); // Rules have to be singletons if (empty(self::$rules[$rule_name])) { // Without a leading \ in the rulename it is assumened that we use a Core FW builtin rule // otherwise the $rule_name points to a class somewhere outsite of the frameworks default rules. $rule_class = strpos($rule_name, '\\') == 0 ? __NAMESPACE__ . '\\Rules\\' . $rule_name . 'Rule' : $rule_name; // Create the rule obejct instance $rule_object = new $rule_class($this); // The rule object must be a child of RuleAbstract! if (!$rule_object instanceof RuleInterface) { throw new ValidatorException('Validator rules MUST be a either implement the \\Core\\Validator\\Rules\\RuleInterface or be an instance of \\Core\\Validator\\Rules\\AbstractRule which implements this interface.'); } // Add rule to the rules stack self::$rules[$rule_name] = $rule_object; } else { // Reset existing rules self::$rules[$rule_name]->reset(); } return self::$rules[$rule_name]; }