/** * Load table registry map * * @param Event $event * * @throws \Rad\Core\Exception\MissingBundleException */ public function loadTableRegistryMap(Event $event) { foreach (Bundles::getLoaded() as $bundleName) { $mapDir = Bundles::getPath($bundleName) . DS . 'Domain' . DS . 'map'; if (is_file($mapDir . DS . 'table_registry_config.php')) { require_once $mapDir . DS . 'table_registry_config.php'; } } }
/** * Cli method * * @param CLImate $climate * * @throws \Rad\Core\Exception\MissingBundleException */ public function cliMethod(CLImate $climate) { $sqlDir = VAR_DIR . DS . 'cake_orm' . DS . 'sql'; if (!is_dir($sqlDir)) { mkdir($sqlDir, 0777, true); } $sql = []; foreach (Bundles::getLoaded() as $bundle) { $climate->backgroundLightGray()->info(sprintf('Bundle %s ...', $bundle)); $path = Bundles::getPath($bundle); $sqlFilename = Inflection::underscore($bundle) . '.sql'; $schemaPath = $path . DS . 'Resource' . DS . 'config' . DS . 'schema.xml'; if (!is_file($schemaPath)) { $climate->lightRed(sprintf('File "%s" does not exists.', $schemaPath)); $climate->br(2); continue; } $domDocument = new DOMDocument(); $domDocument->load($schemaPath); $xpath = new DOMXPath($domDocument); $tables = $xpath->query('/database/table'); /** @var DOMElement $table */ foreach ($tables as $table) { $schemaTable = new Table($table->getAttribute('name')); $this->prepareTableRegistry($table, $bundle); $this->prepareColumn($table, $xpath, $schemaTable); $this->prepareForeignConstraint($table, $xpath, $schemaTable); $this->prepareUniqueConstraint($table, $xpath, $schemaTable); $this->preparePrimaryConstraint($table, $xpath, $schemaTable); $this->prepareIndex($table, $xpath, $schemaTable); $sql[$sqlDir . DS . $sqlFilename][] = $schemaTable->createSql(ConnectionManager::get('default')); } foreach ($sql as $file => $tablesSql) { $tmpSql = ''; foreach ($tablesSql as $tableSql) { $tmpSql .= implode(";\n", $tableSql); $tmpSql .= "\n\n"; } file_put_contents($file, $tmpSql); $climate->lightGray(sprintf('Create SQL file "%s".', $file)); } $sql = []; $climate->info('Dump table registry config ...'); $this->dumpTableRegistry($climate, $bundle); $climate->info('Dump model classes ...'); $this->dumpModelClasses($climate, $bundle); $climate->br(2); } }
/** * {@inheritdoc} */ public function loadService() { $this->getContainer()->setShared('twig', function () { $loader = new \Twig_Loader_Filesystem([]); $loader->addPath(SRC_DIR . "/App/Resource/template"); foreach (Bundles::getLoaded() as $bundleName) { if (is_dir(SRC_DIR . "/{$bundleName}/Resource/template/")) { $loader->addPath(SRC_DIR . "/{$bundleName}/Resource/template", $bundleName); } } $loader->addPath(ROOT_DIR . '/vendor/symfony/twig-bridge/Resources', 'TwigBridgeTemplates'); if (Config::get('debug', false)) { $options = ['debug' => true, 'cache' => false]; } else { $options = ['debug' => false, 'cache' => CACHE_DIR . '/twig/']; } $twig = new \Twig_Environment($loader, $options); if (Config::get('debug', false)) { $twig->addExtension(new Twig_Extension_Debug()); } // add route generator function $twig->addFunction(new \Twig_SimpleFunction('generateUrl', function ($url = null, $withParams = true, $withLanguage = true, $incDomain = true) { $router = $this->getRouter(); return $router->generateUrl($url, [Router::GEN_OPT_LANGUAGE => $withLanguage, Router::GEN_OPT_WITH_PARAMS => $withParams, Router::GEN_OPT_INC_DOMAIN => $incDomain]); })); $twig->addFunction(new \Twig_SimpleFunction('getService', function ($name, array $args = []) { return $this->getContainer()->get($name, $args); })); /** @var Registry $registry */ $registry = $this->getContainer()->get('registry'); // return css tags $twig->addFunction(new \Twig_SimpleFunction('getCss', function ($css = null, $priority = 0) use($registry) { $result = $registry->get(TwigHelper::GLOBAL_CSS, TwigHelper::TWIG_REGISTRY_SCOPE); if (null !== $css) { $result = [['css' => $css, 'priority' => $priority]]; } if (is_array($result)) { foreach ($result as $row) { $order[] = $row['priority']; } array_multisort($order, $result); array_walk($result, function (&$item) { $item = $item['css']; // if it is direct link to file if (strpos($item, 'file://') === 0) { $item = substr($item, 7); $item = "<link rel=\"stylesheet\" href=\"{$item}\">"; } else { $item = "<style>\n{$item}\n</style>"; } }); $result = implode("\n", $result); } return strval($result); }, ['is_safe' => ['html']])); // return js tags $twig->addFunction(new \Twig_SimpleFunction('getJs', function ($js = null, $priority = 0) use($registry) { $result = $registry->get(TwigHelper::GLOBAL_JS, TwigHelper::TWIG_REGISTRY_SCOPE); if (null !== $js) { $result = [['js' => $js, 'priority' => $priority]]; } if (is_array($result)) { foreach ($result as $row) { $order[] = $row['priority']; } array_multisort($order, $result); array_walk($result, function (&$item) { $item = $item['js']; // if it is direct link to file if (strpos($item, 'file://') === 0) { $item = substr($item, 7); $item = "<script src=\"{$item}\"></script>"; } else { $item = "<script>\n{$item}\n</script>"; } }); $result = implode("\n", $result); } return strval($result); }, ['is_safe' => ['html']])); $twig->addFunction(new \Twig_SimpleFunction('addCss', function ($css, $priority = 0) { TwigHelper::addCss($css, $priority); })); $twig->addFunction(new \Twig_SimpleFunction('addJs', function ($js, $priority = 0) { TwigHelper::addJs($js, $priority); })); // return master twig $twig->addFunction(new \Twig_SimpleFunction('getMasterTwig', function () use($registry) { $result = $registry->get(TwigHelper::MASTER_TWIG, TwigHelper::TWIG_REGISTRY_SCOPE); $item = array_pop($result); $registry->set(TwigHelper::MASTER_TWIG, $result, TwigHelper::TWIG_REGISTRY_SCOPE); return strval($item); }, ['is_safe' => ['html']])); Library\Helper::addMasterTwig('@App/master.twig'); $renderer = new TwigRendererEngine(['@TwigBridgeTemplates/views/Form/form_div_layout.html.twig']); $renderer->setEnvironment($twig); $twig->addExtension(new FormExtension(new TwigRenderer($renderer))); $twig->addExtension(new TranslationExtension(new IdentityTranslator())); return $twig; }); $this->getContainer()->setShared('form_factory', function () { if (class_exists('Symfony\\Component\\Validator\\Mapping\\Factory\\LazyLoadingMetadataFactory')) { $metadataFactory = new LazyLoadingMetadataFactory(new StaticMethodLoader()); } else { $metadataFactory = new ClassMetadataFactory(new StaticMethodLoader()); } $builder = Validation::createValidatorBuilder()->setConstraintValidatorFactory(new ConstraintValidatorFactory())->setTranslationDomain('validators')->setMetadataFactory($metadataFactory); $extensions = [new HttpFoundationExtension(), new ValidatorExtension($builder->getValidator())]; return Forms::createFormFactoryBuilder()->addExtensions($extensions)->setResolvedTypeFactory(new ResolvedFormTypeFactory())->getFormFactory(); }); }
/** * Handles routing information received from the rewrite engine * * @param string|null $uri */ public function handle($uri = null) { $method = $this->prepareMethodName(); if (empty($uri)) { $uri = $this->getRewriteUri(); } $uri = trim($uri, '/'); // remove empty cells $parts = []; foreach (explode('/', $uri) as $p) { if (trim($p) !== '') { $parts[] = $p; } } $this->extractLanguage($parts); // check if it is REST or not, then do what is required $this->prepareRestRequest($parts); // Cleaning route parts & Rebase array keys $parts[] = strtolower(self::DEFAULT_ACTION); $camelizedParts = $parts; $camelizedParts = array_values(array_map('Rad\\Utility\\Inflection::camelize', $camelizedParts)); $bundle = reset($camelizedParts); $bundles = array_intersect([$bundle, 'App'], Bundles::getLoaded()); foreach ($bundles as $bundleName) { // reset manipulation parameters $dummyCamelizedParts = $camelizedParts; $dummyParts = $parts; if ($bundleName === 'App' && $dummyCamelizedParts[0] != 'App') { array_unshift($dummyParts, $bundleName); array_unshift($dummyCamelizedParts, $bundleName); } else { // get bundle namespace instead of its name array_shift($dummyCamelizedParts); array_unshift($dummyCamelizedParts, trim(Bundles::getNamespace($bundleName), '\\')); } // add "Action" to array as second param array_splice($dummyCamelizedParts, 1, 0, 'Action'); $this->routingPhase = self::ROUTING_PHASE_ACTION; /** * routingPhase is sequence of three phases, in the following order * 1- direct call of action * 2- direct call of method * 3- direct call of index action */ // Continue searching till you found any matching // Or you have at least three elements in array (Bundle, "Action", Action) while (count($dummyCamelizedParts) >= 3) { $actionNamespace = implode('\\', $dummyCamelizedParts) . 'Action'; if (class_exists($actionNamespace)) { $this->finalizeRouterArguments($dummyParts, $dummyCamelizedParts, $actionNamespace, $bundleName, $method); break 2; } array_pop($dummyCamelizedParts); // change router for some other default paths switch ($this->routingPhase) { case self::ROUTING_PHASE_INDEX: $this->routingPhase = self::ROUTING_PHASE_ACTION; break; case self::ROUTING_PHASE_METHOD: $dummyCamelizedParts[] = self::DEFAULT_ACTION; $this->routingPhase--; break; case self::ROUTING_PHASE_ACTION: $dummyCamelizedParts[] = $method; $this->routingPhase--; break; } } } }