/** * Cria classe se ela não existe * * @param string $namespace * @param string $class * @param string $parent * @param string $implements * @param string $license * */ public static function create($namespace, $class, $parent = NUL, $implements = NULL, $license = NULL) { $NSTemp = $namespace; $namespace = self::NAMESPACE_SEPARATOR == substr($namespace, -1) ? substr($namespace, 0, -1) : $namespace; $namespace = $namespace . self::NAMESPACE_SEPARATOR . $class; $fullpath = Location::realpathFromNamespace($namespace) . '.php'; if (TRUE == is_file($fullpath)) { return; } $dirname = dirname($fullpath); if (!is_dir($dirname)) { mkdir($dirname, 0700, TRUE); } $content = sprintf('<?php%5$s%1$s%5$s%2$s%5$s%5$s%3$s%5$s%4$s', self::classLicense(), self::classLocation($NSTemp), self::classDoc($NSTemp, $class), self::classDef($class, $parent, $implements), PHP_EOL); IOException::throwsExceptionIfParamIsNull(file_put_contents($fullpath, $content, LOCK_EX | FILE_TEXT), self::T_CREATECLASSIFNOTEXISTS_ERROR_ON_CREATE_CLASS); }
/** * retorna se cominho completo da classe (filha de SIALAbstract) informado * * @param SIALAbstract|string $target * @return string * @throws SIALException * */ public static function realpathFromNamespace($target) { return Location::realpathFromNamespace($target); }
/** * Inicializa o bootstrap * * @return SIALApplication * */ public function initBootstrap() { $namespace = $this->_config->get('app.mainnamespace') . self::NAMESPACE_SEPARATOR . 'library' . self::NAMESPACE_SEPARATOR . 'Bootstrap'; require_once Location::realpathFromNamespace($namespace) . '.php'; $this->_bootstrap = new $namespace($this->_config); return $this; }
/** * retorna o controller que sera executado * * @return ControllerAbstract * @throws ControllerException * */ public function controller() { $config = $this->config(); $request = $this->request(); $module = $request->getModule(); $funcionality = $request->getFuncionality(); $action = $request->getAction(); $namespace = $config->get('app.namespace') . self::NAMESPACE_SEPARATOR; if (TRUE === Request::$useModule) { $namespace .= $module; } $namespace .= sprintf('%1$s%2$s%1$smvcb%1$scontroller%1$s%3$sController', self::NAMESPACE_SEPARATOR, $funcionality, ucfirst($funcionality)); $fullpath = Location::realpathFromNamespace($namespace) . '.php'; ControllerException::throwsExceptionIfParamIsNull(is_file($fullpath), "Controller::{$funcionality} não implementada"); $ctrl = new $namespace(); ControllerException::throwsExceptionIfParamIsNull($ctrl->hasAction($action), "Action '{$namespace}::{$action}' não existe."); return $ctrl; }
/** * Construtor. * * @param ViewAbstract $view * */ public function __construct(ViewAbstract $view) { # configura a pasta de templates da app $this->_applicationPath = $view->config()->get('app.namespace') . self::NAMESPACE_SEPARATOR; $this->_applicationPath = Location::realpathFromNamespace($this->_applicationPath); $this->_scriptPath = $view->getScriptPaths(); $this->_type = $view::T_TYPE; $this->_extension = '.' == substr($view::T_EXTENSION, 0, 1) ? $view::T_EXTENSION : '.' . $view::T_EXTENSION; $this->_view = $view; }
/** * @param stdClass $param * @param string $type * @throws IllegalArgumentException * */ public static function factory(\stdClass $param, $type) { $namespace = __NAMESPACE__ . self::NAMESPACE_SEPARATOR . $type . self::NAMESPACE_SEPARATOR . 'ScreenForm'; IllegalArgumentException::throwsExceptionIfParamIsNull(Location::hasClassInNamespace($namespace), self::T_SCREENFORMABSTRACT_COMPONENT_UNAVAILABLE); return $namespace::factory($param); }
/** * Cria aplicação. * * @example ApplicationAbstract::factory * @code * <?php * $decStyle = 'ICMBioGreen'; * $docType = 'html'; * * # decorador do documento * $decorator = DecoratorAbstract::factory($docType, $decStyle); * * # criar aplicacao simples sem decorator * $app = ApplicationAbstract::factory($docType, $decorator); * ?> * @endcode * * @param string $type * @param DecoratorAbstract $decorator * @return Application * @throws IllegalArgumentException * */ public static function factory($type, $decorator = NULL) { $NSApplication = __NAMESPACE__ . self::NAMESPACE_SEPARATOR . 'application' . self::NAMESPACE_SEPARATOR . 'Application' . strtoupper($type); IllegalArgumentException::throwsExceptionIfParamIsNull(Location::hasClassInNamespace($NSApplication), self::T_APPLICATION_INVLAID_APP); $application = new $NSApplication(); $application->_decorator = $decorator; return $application; }
/** * Cria instância da View levando em consideração a especialização ou não da classe View pelo projeto. * * @return ViewAbstract * @throws ControllerException * @example ControllerAbstract::getView * @code * <?php * // Busca em \mvcb\view\scripts\html o arquivo Foo.html * $this->setViewType('html'); * $this->getView('foo'); * * // Busca em \mvcb\view\scripts\json o arquivo Bar.html * $this->setViewType('json'); * $this->getView('bar'); * ?> * @encode * */ public function getView() { # nome da view que sera instanciada $type = ucfirst($this->_viewType); if (!isset($this->_view[$type])) { # certifica-se de que o boostrap tenha sido inicializado ControllerException::throwsExceptionIfParamIsNull($this->_bootstrap, self::T_CONTROLLER_METHOD_AFTER_BOOTSTRAP); # complemento do namespace para o pacote view $view = '\\mvcb\\view\\'; # namespace da library da aplicacao $namespace = $this->_bootstrap->config()->get('app.namespace'); $namespace = $this->erReplace(array('application' => ''), $namespace) . 'library'; # verifica se existe especializacao da view na aplicacao # caso nao exista aponta para a view da camada do SIAL/core if (!Location::hasClassInNamespace("{$namespace}{$view}{$type}")) { $namespace = '\\br\\gov\\sial\\core'; } $namespace = "{$namespace}{$view}{$type}"; $this->_view[$type] = new $namespace(); $this->_view[$type]->registerViewScriptBasedFromController($this); $this->_view[$type]->set('constant', $this->_bootstrap->constantHandler()); } return $this->_view[$type]; }
/** * Registra o caminho dos scripts de view baseando-se no controller informado. * * @param br\gov\sial\core\mvcb\controller\ControllerAbstract $ctrl * @return br\gov\sial\core\mvcb\view\ViewAbstract * @example ViewAbstract::registerViewScriptBasedFromController * @code * <?php * ... * class FooController extends ControllerAbstract () * { * } * ... * $fooController = new FooController(); * $this->registerViewScriptBasedFromController($fooController); * ?> * @encode * */ public function registerViewScriptBasedFromController(ControllerAbstract $ctrl) { $scriptPath = current(explode('mvcb', $ctrl->getClassName())) . 'mvcb' . self::NAMESPACE_SEPARATOR . 'view' . self::NAMESPACE_SEPARATOR . 'scripts' . self::NAMESPACE_SEPARATOR . $this::T_TYPE; $this->addScriptPath(Location::realpathFromNamespace($scriptPath)); return $this; }
/** * @param string $type = 'html' * @throws IllegalArgumentException * */ public static function factory($type = 'html') { $namespace = __NAMESPACE__ . self::NAMESPACE_SEPARATOR . $type . self::NAMESPACE_SEPARATOR . 'Document'; IllegalArgumentException::throwsExceptionIfParamIsNull(Location::hasClassInNamespace($namespace), self::T_INVALID_TYPE); return new $namespace($type); }
/** * Cria decorator. * * @example DecoratorAbstract::factory * @code * <?php * $docType = 'html'; * $decStyle = 'ICMBioGreen'; * * # decorador do documento * $decorator = DecoratorAbstract::factory($docType, $decStyle); * ?> * @endcode * * @param string $type * @param string $decStyle * @return Decorator * @throws IllegalArgumentException * */ public static function factory($type, $decStyle = 'default') { $NSApplication = __NAMESPACE__ . self::NAMESPACE_SEPARATOR . 'decorator' . self::NAMESPACE_SEPARATOR . 'Decorator' . strtoupper($type); IllegalArgumentException::throwsExceptionIfParamIsNull(Location::hasClassInNamespace($NSApplication), self::T_DECORATOR_INVLAID_APP); return new $NSApplication(); }
/** * @param string|ModelAbstract|null $namespace * @param stdClass $data * @return ValueObjectAbstract * */ public static function factory($namespace = NULL, $data = NULL) { $data = $data ?: new \stdClass(); if (NULL === $namespace) { $namespace = get_called_class(); } if ($namespace instanceof parent) { $namespace = $namespace->getClassName(); } if (FALSE === stripos($namespace, sprintf('%1$svalueObject%1$s', self::NAMESPACE_SEPARATOR)) && $namespace != 'br\\gov\\sial\\core\\lang\\TFile') { $sep = sprintf('%1$smvcb%1$s', self::NAMESPACE_SEPARATOR); ValueObjectException::throwsExceptionIfParamIsNull(!(FALSE === stripos($namespace, $sep)), self::T_VALUEOBJECT_IMPOSSIBLE_DEFINE_NAMESPACE); $namespace = current(explode($sep, $namespace)); $namespace = sprintf('%1$s%2$svalueObject%2$s%3$s', $namespace, self::NAMESPACE_SEPARATOR, ucfirst(end(explode(self::NAMESPACE_SEPARATOR, $namespace))) . 'ValueObject'); ValueObjectException::throwsExceptionIfParamIsNull(Location::hasClassInNamespace($namespace), sprintf(self::T_VALUEOBJECT_UNAVALIABLE, $namespace)); } $valueObject = new $namespace(); $valueObject->loadData((array) $data); return $valueObject; }