示例#1
0
 /**
  * Renders a form to set up the database connection.
  *
  * @return Response|RedirectResponse The response object
  */
 private function setUpDatabaseConnection()
 {
     $request = $this->container->get('request_stack')->getCurrentRequest();
     $parameters['parameters'] = ['database_host' => $this->container->getParameter('database_host'), 'database_port' => $this->container->getParameter('database_port'), 'database_user' => $this->container->getParameter('database_user'), 'database_password' => $this->container->getParameter('database_password'), 'database_name' => $this->container->getParameter('database_name')];
     if ('tl_database_login' !== $request->request->get('FORM_SUBMIT')) {
         return $this->render('database.html.twig', $parameters);
     }
     $parameters['parameters'] = ['database_host' => $request->request->get('dbHost'), 'database_port' => $request->request->get('dbPort'), 'database_user' => $request->request->get('dbUser'), 'database_password' => $this->container->getParameter('database_password'), 'database_name' => $request->request->get('dbName')];
     if ('*****' !== $request->request->get('dbPassword')) {
         $parameters['parameters']['database_password'] = $request->request->get('dbPassword');
     }
     $installTool = $this->container->get('contao.install_tool');
     $installTool->setConnection(ConnectionFactory::create($parameters));
     if (!$installTool->canConnectToDatabase($parameters['parameters']['database_name'])) {
         return $this->render('database.html.twig', array_merge($parameters, ['database_error' => $this->trans('database_could_not_connect')]));
     }
     $dumper = new ParameterDumper($this->container->getParameter('kernel.root_dir'));
     $dumper->setParameters($parameters);
     $dumper->dump();
     return $this->getRedirectResponse();
 }
 /**
  * Returns the container object.
  *
  * @param string $rootDir The root directoy
  *
  * @return ContainerBuilder The object instance
  */
 public static function create($rootDir)
 {
     $container = new ContainerBuilder();
     // Set up the kernel parameters
     $container->setParameter('kernel.root_dir', $rootDir);
     $container->setParameter('kernel.cache_dir', $rootDir . '/cache/install');
     $container->setParameter('kernel.debug', false);
     // Load the parameters.yml file
     if (file_exists($rootDir . '/config/parameters.yml')) {
         $parameters = Yaml::parse(file_get_contents($rootDir . '/config/parameters.yml'));
     } else {
         $parameters = Yaml::parse(file_get_contents($rootDir . '/config/parameters.yml.dist'));
     }
     // Add the parameters to the container
     foreach ($parameters['parameters'] as $name => $value) {
         $container->setParameter($name, $value);
     }
     // Add the Contao upload path
     $container->setParameter('contao.upload_path', 'files');
     // Set up the request stack
     $request = Request::createFromGlobals();
     $requestStack = new RequestStack();
     $requestStack->push($request);
     $container->set('request_stack', $requestStack);
     // Create the session bag
     $bag = new ArrayAttributeBag('_contao_be_attributes');
     $bag->setName('contao_backend');
     // Start the session
     $session = new Session();
     $session->registerBag($bag);
     $session->start();
     $container->set('session', $session);
     // Set up the database connection
     $container->set('database_connection', ConnectionFactory::create($parameters));
     // Resolve the locale
     $translationsDir = __DIR__ . '/../Resources/translations';
     $resolver = new LanguageResolver($requestStack, $translationsDir);
     $locale = $resolver->getLocale();
     // Set up the translator
     $translator = new Translator($locale);
     $translator->setFallbackLocales(['en']);
     $translator->addLoader('xlf', new XliffFileLoader());
     $translator->addResource('xlf', $translationsDir . '/messages.en.xlf', 'en');
     if ('en' !== $locale) {
         $translator->addResource('xlf', $translationsDir . '/messages.' . $locale . '.xlf', 'en');
     }
     $container->setParameter('locale', $locale);
     $container->set('translator.default', $translator);
     // Set up Twig
     $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__ . '/../Resources/views'));
     $twig->addGlobal('path', $request->getBasePath());
     $twig->addGlobal('language', str_replace('_', '-', $locale));
     $twig->addGlobal('ua', Environment::get('agent')->class);
     $twig->addFunction(new \Twig_SimpleFunction('asset', function ($path) use($request) {
         return '/' . ltrim($request->getBasePath() . '/' . $path, '/');
     }));
     $twig->addFilter(new \Twig_SimpleFilter('trans', function ($message, $params = []) use($translator) {
         return $translator->trans($message, $params);
     }));
     $container->set('twig', $twig);
     // Add the kernel bundles
     $kernel = new UnbootableKernel('prod', false);
     $kernel->setRootDir($rootDir);
     $kernel->setBundles($kernel->registerBundles());
     $bundles = [];
     foreach ($kernel->getBundles() as $bundle) {
         $bundles[$bundle->getName()] = get_class($bundle);
     }
     $container->set('kernel', $kernel);
     $container->setParameter('kernel.bundles', $bundles);
     // Add the file system
     $container->set('filesystem', new Filesystem());
     // Add the Contao resources paths
     $pass = new AddResourcesPathsPass();
     $pass->process($container);
     // Add the Contao resource finder
     $container->set('contao.resource_finder', new ResourceFinder($container->getParameter('contao.resources_paths')));
     // Add the Contao resource locator
     $container->set('contao.resource_locator', new FileLocator($container->getParameter('contao.resources_paths')));
     // Add the installer services
     $container->set('contao.installer', new Installer($container->get('database_connection'), $container->get('contao.resource_finder'), $container->get('translator.default')));
     $container->set('contao.install_tool', new InstallTool($container->get('database_connection'), $rootDir));
     $container->set('contao.install_tool_user', new InstallToolUser($container->get('session')));
     return $container;
 }
 /**
  * Returns the container object.
  *
  * @param KernelInterface $kernel
  * @param Request         $request
  *
  * @return ContainerBuilder
  */
 public static function create(KernelInterface $kernel, Request $request)
 {
     $rootDir = $kernel->getRootDir();
     $cacheDir = dirname($rootDir) . '/var/cache/' . $kernel->getEnvironment();
     $logsDir = dirname($rootDir) . '/var/logs';
     $container = new ContainerBuilder();
     $container->setParameter('kernel.root_dir', $rootDir);
     $container->setParameter('kernel.cache_dir', $cacheDir);
     $container->setParameter('kernel.logs_dir', $logsDir);
     $container->setParameter('kernel.debug', false);
     $parameters = [];
     // Load the parameters.yml file
     if (file_exists($rootDir . '/config/parameters.yml')) {
         $parameters = Yaml::parse(file_get_contents($rootDir . '/config/parameters.yml'));
     } elseif (file_exists($rootDir . '/config/parameters.yml.dist')) {
         $parameters = Yaml::parse(file_get_contents($rootDir . '/config/parameters.yml.dist'));
     }
     // Add the parameters to the container
     if (!empty($parameters['parameters'])) {
         foreach ($parameters['parameters'] as $name => $value) {
             $container->setParameter($name, $value);
         }
     }
     // Add the Contao parameters
     $container->setParameter('contao.upload_path', 'files');
     $container->setParameter('contao.image.target_path', 'assets/images');
     // Set up the request stack
     $requestStack = new RequestStack();
     $requestStack->push($request);
     $container->set('request_stack', $requestStack);
     // Create the session bag
     $bag = new ArrayAttributeBag('_contao_be_attributes');
     $bag->setName('contao_backend');
     // Start the session
     $session = new Session();
     $session->registerBag($bag);
     $session->start();
     $container->set('session', $session);
     // Set up the database connection
     $container->set('database_connection', ConnectionFactory::create($parameters));
     // Resolve the locale
     $translationsDir = __DIR__ . '/../Resources/translations';
     $resolver = new LanguageResolver($requestStack, $translationsDir);
     $locale = $resolver->getLocale();
     // Update the request locale
     $request->setLocale($locale);
     // Set up the translator
     $translator = new Translator($locale);
     $translator->setFallbackLocales(['en']);
     $translator->addLoader('xlf', new XliffFileLoader());
     $translator->addResource('xlf', $translationsDir . '/messages.en.xlf', 'en');
     if ('en' !== $locale) {
         $translator->addResource('xlf', $translationsDir . '/messages.' . $locale . '.xlf', 'en');
     }
     $container->set('translator', $translator);
     // Set up Twig
     $twigLoader = new \Twig_Loader_Filesystem();
     $twigLoader->addPath(__DIR__ . '/../Resources/views', 'ContaoInstallation');
     $twig = new \Twig_Environment($twigLoader);
     $twig->addExtension(new AnsiExtension());
     $twig->addFunction(new \Twig_SimpleFunction('asset', function ($path) use($request) {
         return '/' . ltrim($request->getBasePath() . '/' . $path, '/');
     }));
     $twig->addFilter(new \Twig_SimpleFilter('trans', function ($message, $params = []) use($translator) {
         return $translator->trans($message, $params);
     }));
     $container->set('twig', $twig);
     $kernelBundles = [];
     foreach ($kernel->getBundles() as $bundle) {
         $kernelBundles[$bundle->getName()] = get_class($bundle);
     }
     $container->set('kernel', $kernel);
     $container->setParameter('kernel.bundles', $kernelBundles);
     // Add the file system
     $container->set('filesystem', new Filesystem());
     // Add the Contao resources paths
     $pass = new AddResourcesPathsPass();
     $pass->process($container);
     // Add the Contao resource finder
     $container->set('contao.resource_finder', new ResourceFinder($container->getParameter('contao.resources_paths')));
     // Add the Contao resource locator
     $container->set('contao.resource_locator', new FileLocator($container->getParameter('contao.resources_paths')));
     // Add the Contao cache
     $container->set('contao.cache', new FilesystemCache($cacheDir . '/contao/cache', '', 022));
     // Add the installer services
     $container->set('contao.installer', new Installer($container->get('database_connection'), $container->get('contao.resource_finder'), $container->get('translator')));
     $container->set('contao.install_tool', new InstallTool($container->get('database_connection'), $rootDir));
     $container->set('contao.install_tool_user', new InstallToolUser($container->get('session')));
     return $container;
 }