Commit 25be45d9 authored by dj3c1t's avatar dj3c1t

base installer

parent 9d0fa763
<?php
namespace Dj3c1t\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class InstallController extends Controller {
public function indexAction(Request $request, $step = 0) {
/*
$im = $this->container->get('dj3c1t_app.install_manager');
$parameters = $im->readParameters();
$parameters["database_user"] = 'install_user';
$im->writeParameters($parameters);
*/
return new Response("install step ".$step);
}
}
install:
path: /install/{step}
defaults: { _controller: Dj3c1tAppBundle:Install:index, step: 0 }
login:
path: /login
defaults: { _controller: Dj3c1tAppBundle:Security:login }
......
services:
# dj3c1t_app.example:
# class: Dj3c1t\AppBundle\Example
# arguments: [@service_id, "plain_value", %parameter%]
dj3c1t_app.request_listener:
class: Dj3c1t\AppBundle\Service\Listener\RequestListener
arguments: ['@service_container']
tags:
- { name: kernel.event_listener, event: kernel.request }
dj3c1t_app.install_manager:
class: Dj3c1t\AppBundle\Service\InstallManager
arguments: ['@service_container']
dj3c1t_app.menus:
class: Dj3c1t\AppBundle\Service\Menus
......
<?php
namespace Dj3c1t\AppBundle\Service;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
class InstallManager {
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
public function isInstalled() {
if(
!$this->container->hasParameter("database_host")
|| !$this->container->hasParameter("database_name")
) {
return false;
}
$database_host = $this->container->getParameter("database_host");
$database_name = $this->container->getParameter("database_name");
if(!isset($database_host) || !isset($database_name)) {
return false;
}
try {
$em = $this->container->get('doctrine')->getManager();
$userRepo = $em->getRepository("Dj3c1tAppBundle:User");
$users = $userRepo->findAll();
}
catch(\Exception $e) {
return false;
}
return true;
}
public function readParameters() {
$parameters_file = $this->container->get('kernel')->getRootDir().'/config/parameters.yml';
if(!@file_exists($parameters_file)) {
throw new \Exception("missing parameters.yml");
}
if(!($content = @file_get_contents($parameters_file))) {
throw new \Exception("can't read paramerters.yml");
}
try {
$yaml = Yaml::parse($content);
}
catch(\Exception $e) {
throw new \Exception("invalid paramerters.yml");
}
return isset($yaml['parameters']) ? $yaml['parameters'] : array();
}
public function writeParameters($parameters) {
$parameters_file = $this->container->get('kernel')->getRootDir().'/config/parameters.yml';
$dumper = new Dumper();
$yaml = $dumper->dump(
array(
'parameters' => $parameters
),
4
);
if(!@file_put_contents($parameters_file, $yaml)) {
throw new \Exception("can't write paramerters.yml");
}
}
}
<?php
namespace Dj3c1t\AppBundle\Service\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class RequestListener {
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event) {
$im = $this->container->get('dj3c1t_app.install_manager');
if(!$im->isInstalled()) {
$request = $event->getRequest();
if($request->get('_route') != 'install') {
$url = $this->container->get('router')->generate('install');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment