Ejemplo n.º 1
0
 /**
  * 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';
         }
     }
 }
Ejemplo n.º 2
0
 protected function setUp()
 {
     $this->router = new Router();
     if (!defined('SRC_DIR')) {
         define('SRC_DIR', __DIR__ . '/Fixtures/src');
     }
     if (!defined('DS')) {
         define('DS', '/');
     }
     $bundles = [new AppBundle(), new TestBundle()];
     Bundles::loadAll($bundles);
 }
 /**
  * Overrides the original method from phinx in order to return a tailored
  * Config object containing the connection details for the database.
  *
  * @return Phinx\Config\Config
  */
 public function getConfig()
 {
     if ($this->configuration) {
         return $this->configuration;
     }
     $dir = APP_DIR . DS . 'Resource' . DS . 'migrations';
     $migrationTable = 'phinxlog';
     if ($bundleName = $this->input->getOption('bundle')) {
         $dir = Bundles::getPath($bundleName) . DS . 'Resource' . DS . 'migrations';
     }
     return $this->configuration = new Config(['paths' => ['migrations' => $dir], 'environments' => ['default_migration_table' => $migrationTable, 'default_database' => getenv('RAD_ENVIRONMENT'), getenv('RAD_ENVIRONMENT') => \Rad\Configure\Config::get('migrations.environments.' . getenv('RAD_ENVIRONMENT'))]]);
 }
Ejemplo n.º 4
0
 /**
  * Init application
  *
  * @throws BaseException
  * @throws DependencyInjection\Exception
  */
 protected function init()
 {
     $error = new ErrorHandler();
     $error->setHandler(new JsonHandler())->setDebug(true)->register();
     DotEnv::load(ROOT_DIR);
     if (!getenv('RAD_ENVIRONMENT')) {
         putenv('RAD_ENVIRONMENT=production');
     }
     $this->container = Container::getInstance();
     Config::set('environment', getenv('RAD_ENVIRONMENT'));
     Config::set('debug', (bool) getenv('RAD_DEBUG'));
     Bundles::loadAll($this->registerBundles());
     $this->container->setShared('event_manager', new EventManager(), true);
     $this->loadConfig();
     $this->loadService();
     $this->loadServicesFromConfig();
     $this->container->setShared('router', new Router());
     $this->bundleStartup();
 }
Ejemplo n.º 5
0
 /**
  * {@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();
     });
 }
Ejemplo n.º 6
0
 /**
  * Dump model classes
  *
  * @param CLImate $climate
  * @param string  $bundle
  */
 protected function dumpModelClasses(CLImate $climate, $bundle)
 {
     if (is_array($this->tablesModel[$bundle])) {
         foreach ($this->tablesModel[$bundle] as $tableSpec) {
             $alias = $tableSpec['alias'];
             if ($tableSpec['tableClass']) {
                 $tableClassPath = SRC_DIR . DS . str_replace('\\', '/', $tableSpec['tableClass']) . '.php';
                 $tableClassDir = dirname($tableClassPath);
                 $tableClassName = trim(substr($tableSpec['tableClass'], strrpos($tableSpec['tableClass'], '\\')), '\\');
                 $tableClassNamespace = trim(substr($tableSpec['tableClass'], 0, strrpos($tableSpec['tableClass'], '\\')), '\\');
                 if (!is_file($tableClassPath)) {
                     if (!is_dir($tableClassDir)) {
                         mkdir($tableClassDir, 0777, true);
                     }
                     ob_start();
                     echo '<?php';
                     include Bundles::getPath('CakeOrm') . '/Resource/config/table_template.php';
                     $content = ob_get_contents();
                     ob_end_clean();
                     file_put_contents($tableClassPath, $content);
                     $climate->lightGray(sprintf('Create table class "%s".', $tableSpec['tableClass']));
                 } else {
                     $climate->lightMagenta(sprintf('Table class "%s" exists.', $tableSpec['tableClass']));
                 }
             }
             //if ($tableSpec['entityClass']) {
             //    $entityClassPath = SRC_DIR . DS . str_replace('\\', '/', $tableSpec['entityClass']);
             //    $entityClassName = trim(
             //        substr($tableSpec['entityClass'], strrpos($tableSpec['entityClass'], '\\')),
             //        '\\'
             //    );
             //}
         }
     }
 }
Ejemplo n.º 7
0
 /**
  * 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;
             }
         }
     }
 }