commit 5759aa7a8a13cf2a9da3879295cc5c01c1d40fab
parent 76375f372487b41cc8eac9320b1104d5e014f8df
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 19 Dec 2015 18:55:32 +0100
Externalized config updating, added cli command to update the config on the CLI
Diffstat:
8 files changed, 225 insertions(+), 176 deletions(-)
diff --git a/src/PartKeepr/SetupBundle/Command/UpdateConfigCommand.php b/src/PartKeepr/SetupBundle/Command/UpdateConfigCommand.php
@@ -0,0 +1,26 @@
+<?php
+namespace PartKeepr\SetupBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class UpdateConfigCommand extends ContainerAwareCommand
+{
+ public function configure()
+ {
+ parent::configure();
+ $this->setName('partkeepr:setup:update-config');
+ $this->setDescription("Updates the PartKeepr configuration with all required config parameters");
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output)
+ {
+ $configService = $this->getContainer()->get("partkeepr.setup.config_service");
+ $config = $configService->configParser();
+ $configOutput = $configService->getConfig($config);
+
+ file_put_contents($configService->getConfigPath(false), $configOutput);
+ $output->writeln("Config updated");
+ }
+}
diff --git a/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php b/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php
@@ -10,6 +10,7 @@ class ExistingConfigParserController extends SetupController
/**
* @Route("/setup/parseExistingConfig")
* @param Request $request
+ * @return JsonResponse
*/
public function parseExistingConfigAction(Request $request)
{
@@ -29,7 +30,7 @@ class ExistingConfigParserController extends SetupController
);
try {
- $response["config"] = $this->configParser();
+ $response["config"] = $this->get("partkeepr.setup.config_service")->configParser();
if (count($response["config"]) == 0) {
$response["config"] = $this->getLegacyConfig();
@@ -55,7 +56,7 @@ class ExistingConfigParserController extends SetupController
{
$config = array();
- $legacyConfig = $this->legacyConfigParser();
+ $legacyConfig = $this->get("partkeepr.setup.config_service")->legacyConfigParser();
if (count($legacyConfig) > 0) {
if (array_key_exists("partkeepr.database.driver", $legacyConfig)) {
diff --git a/src/PartKeepr/SetupBundle/Controller/FileMigrationController.php b/src/PartKeepr/SetupBundle/Controller/FileMigrationController.php
@@ -13,6 +13,7 @@ class FileMigrationController extends SetupController
* @Route("/setup/migrateFiles")
*
* @param Request $request
+ * @return Response
*/
public function migrateFilesAction(Request $request)
{
@@ -34,7 +35,7 @@ class FileMigrationController extends SetupController
"message" => "No files to migrate",
);
- $legacyConfig = $this->legacyConfigParser();
+ $legacyConfig = $this->get("partkeepr.setup.config_service")->legacyConfigParser();
$legacyFilePath = $this->get("kernel")->getRootDir()."/../data/";
$legacyImagePath = $this->get("kernel")->getRootDir()."/../data/images/";
diff --git a/src/PartKeepr/SetupBundle/Controller/SetupController.php b/src/PartKeepr/SetupBundle/Controller/SetupController.php
@@ -2,8 +2,7 @@
namespace PartKeepr\SetupBundle\Controller;
use Doctrine\DBAL\Exception\DriverException;
-use PartKeepr\SetupBundle\Visitor\ConfigVisitor;
-use PartKeepr\SetupBundle\Visitor\LegacyConfigVisitor;
+use PartKeepr\SetupBundle\Services\ConfigSetupService;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
@@ -13,11 +12,6 @@ use Symfony\Component\Routing\Annotation\Route;
class SetupController extends Controller
{
/**
- * The authentification key length
- */
- const KEY_LENGTH = 32;
-
- /**
* @Route("/setup/_int_test_connectivity")
*/
public function intTestConnectivityAction()
@@ -43,6 +37,7 @@ class SetupController extends Controller
/**
* @Route("/setup/testConnectivity")
* @param Request $request
+ *
* @return Response
*/
public function testConnectivityAction(Request $request)
@@ -57,6 +52,7 @@ class SetupController extends Controller
/**
* @Route("/setup/saveConfig")
* @param Request $request
+ *
* @return JsonResponse
*/
public function saveConfigAction(Request $request)
@@ -69,6 +65,7 @@ class SetupController extends Controller
"errors" => [],
"message" => "Configuration saved successful",
);
+
return new JsonResponse($response);
}
@@ -77,10 +74,10 @@ class SetupController extends Controller
*/
public function webserverTestAction()
{
- $response = array(
+ $response = array(
"success" => true,
"message" => "Web Server configuration OK",
- "errors" => array()
+ "errors" => array(),
);
return new JsonResponse($response);
@@ -91,14 +88,14 @@ class SetupController extends Controller
*/
public function generateAuthKeyAction()
{
- $response = array(
+ $response = array(
"success" => true,
"message" => "Auth key successfully generated",
- "errors" => array()
+ "errors" => array(),
);
$parameters = array(
- "authkey" => $this->generateSecret()
+ "authkey" => $this->get("partkeepr.setup.config_service")->generateSecret()
);
$contents = $this->container->get('templating')->render('PartKeeprSetupBundle::authkey.php.twig', $parameters);
@@ -133,23 +130,14 @@ class SetupController extends Controller
}
}
- protected function generateSecret () {
- $secret = "";
- for ($i = 0; $i < self::KEY_LENGTH; $i++) {
- $secret .= chr(65 + rand(0, 16));
- }
- return $secret;
-
-
- }
-
- protected function verifyAuthKey ($givenKey) {
+ protected function verifyAuthKey($givenKey)
+ {
$findText = "Your auth key is: ";
$data = file_get_contents($this->getAuthKeyPath());
$position = strpos($data, $findText);
- $key = substr($data, $position + strlen($findText), self::KEY_LENGTH);
+ $key = substr($data, $position + strlen($findText), ConfigSetupService::KEY_LENGTH);
if ($key === $givenKey) {
return true;
@@ -162,136 +150,17 @@ class SetupController extends Controller
{
$data = json_decode($request->getContent(), true);
- // Parameter defaults to ensure they exist
- $parameters = array(
- "database_driver" => null,
- "database_host" => null,
- "database_port" => null,
- "database_name" => null,
- "database_password" => null,
-
- "mailer_transport" => null,
- "mailer_host" => null,
- "mailer_port" => null,
- "mailer_encryption" => null,
- "mailer_user" => null,
- "mailer_password" => null,
- "mailer_auth_mode" => null,
-
- "authentication_provider" => "PartKeepr.Auth.HTTPBasicAuthenticationProvider",
-
- "locale" => "en",
-
- "secret" => $this->generateSecret(),
-
- "fr3d_ldap.driver.host" => "127.0.0.1",
- "fr3d_ldap.driver.port" => null,
- "fr3d_ldap.driver.username" => null,
- "fr3d_ldap.driver.password" => null,
- "fr3d_ldap.driver.bindRequiresDn" => false,
- "fr3d_ldap.driver.baseDn" => "",
- "fr3d_ldap.driver.accountFilterFormat" => null,
- "fr3d_ldap.driver.optReferrals" => null,
- "fr3d_ldap.driver.useSsl" => null,
- "fr3d_ldap.driver.useStartTls" => null,
- "fr3d_ldap.driver.accountCanonicalForm" => null,
- "fr3d_ldap.driver.accountDomainName" => null,
- "fr3d_ldap.driver.accountDomainNameShort" => null,
- "fr3d_ldap.user.enabled" => false,
- "fr3d_ldap.user.baseDn" => "dc=example,dc=com",
- "fr3d_ldap.user.filter" => null,
- "fr3d_ldap.user.attribute.username" => null,
- "fr3d_ldap.user.attribute.name" => null,
- "fr3d_ldap.user.attribute.email" => null,
-
- "partkeepr.filesystem.data_directory" => "%kernel.root_dir%/../data/",
- "partkeepr.cronjob.check" => true,
- "partkeepr.filesystem.quota" => false,
- "partkeepr.auth.max_users" => "unlimited",
- "partkeepr.category.path_separator" => " ➤ ",
- "partkeepr.maintenance" => false,
- "partkeepr.maintenance.title" => "",
- "partkeepr.maintenance.message" => "",
- "cache.dunglas" => false,
- "cache.doctrine" => "array",
- "partkeepr.parts.limit" => false,
- "partkeepr.users.limit" => false
- );
-
- if (function_exists("apc_fetch")) {
- $parameters["cache.dunglas"] = "api.mapping.cache.apc";
- $parameters["cache.doctrine"] = "apc";
- }
-
- $this->applyIf($parameters, $data["values"]);
-
- $parameters = array_merge($parameters, $data["values"]);
- array_walk_recursive($parameters, function (&$item) { $item = var_export($item, true); });
-
- ksort($parameters);
+ $configService = $this->get("partkeepr.setup.config_service");
- $contents = $this->container->get('templating')->render('PartKeeprSetupBundle::parameters.php.twig', array("parameters" => $parameters));
+ $config = $configService->getConfig($data["values"]);
- file_put_contents($this->getConfigPath($test), $contents);
+ file_put_contents($configService->getConfigPath($test), $config);
}
- private function applyIf($target, $source)
+ private function getAuthKeyPath()
{
- foreach ($target as $key => $value) {
- if (array_key_exists($key, $source)) {
- $target[$key] = $source[$key];
- }
- }
-
- return $target;
- }
-
- private function getAuthKeyPath () {
return dirname(__FILE__)."/../../../../app/authkey.php";
}
- protected function getConfigPath ($test) {
- if ($test) {
- $filename = "parameters_setup.php";
- } else {
- $filename = "parameters.php";
- }
- return dirname(__FILE__)."/../../../../app/config/".$filename;
- }
-
- protected function legacyConfigParser()
- {
- if (file_exists($this->getLegacyConfigPath())) {
- $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
- $traverser = new \PHPParser_NodeTraverser();
- $traverser->addVisitor(new LegacyConfigVisitor());
- $statements = $parser->parse(file_get_contents($this->getLegacyConfigPath()));
- $traverser->traverse($statements);
-
- return LegacyConfigVisitor::getConfigValues();
- }
-
- return array();
- }
-
- protected function configParser()
- {
- if (file_exists($this->getConfigPath(false))) {
- $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
- $traverser = new \PHPParser_NodeTraverser();
- $traverser->addVisitor(new ConfigVisitor());
- $statements = $parser->parse(file_get_contents($this->getConfigPath(false)));
- $traverser->traverse($statements);
-
- return ConfigVisitor::getConfigValues();
- }
-
- return array();
- }
-
- protected function getLegacyConfigPath()
- {
- return dirname(__FILE__)."/../../../../config.php";
- }
}
diff --git a/src/PartKeepr/SetupBundle/DependencyInjection/PartKeeprSetupExtension.php b/src/PartKeepr/SetupBundle/DependencyInjection/PartKeeprSetupExtension.php
@@ -19,6 +19,11 @@ class PartKeeprSetupExtension extends Extension
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ if ($container->getParameter("kernel.environment") !== "setup") {
+
+ $loader->load('setup_services.xml');
+ }
+
$loader->load('services.xml');
}
}
diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/services.xml
@@ -5,32 +5,8 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
- <service id="partkeepr.setup.footprint_service" class="PartKeepr\SetupBundle\Services\FootprintSetupService">
- <argument type="service" id="doctrine.orm.default_entity_manager"/>
- <argument type="service" id="partkeepr.footprint.category_service"/>
- <argument type="service" id="partkeepr_uploadedfile_service"/>
- <argument type="service" id="kernel"/>
- </service>
-
- <service id="partkeepr.setup.manufacturer_service"
- class="PartKeepr\SetupBundle\Services\ManufacturerSetupService">
- <argument type="service" id="doctrine.orm.default_entity_manager"/>
- <argument type="service" id="partkeepr_uploadedfile_service"/>
- <argument type="service" id="kernel"/>
- </service>
-
- <service id="partkeepr.setup.part_unit_service" class="PartKeepr\SetupBundle\Services\PartUnitSetupService">
- <argument type="service" id="doctrine.orm.default_entity_manager"/>
- </service>
-
- <service id="partkeepr.setup.si_prefix_service" class="PartKeepr\SetupBundle\Services\SiPrefixSetupService">
- <argument id="doctrine.orm.default_entity_manager" type="service"/>
- <argument id="kernel" type="service"/>
- </service>
-
- <service id="partkeepr.setup.unit_service" class="PartKeepr\SetupBundle\Services\UnitSetupService">
- <argument id="doctrine.orm.default_entity_manager" type="service"/>
- <argument id="kernel" type="service"/>
+ <service id="partkeepr.setup.config_service" class="PartKeepr\SetupBundle\Services\ConfigSetupService">
+ <argument type="service" id="templating.engine.twig"/>
</service>
</services>
diff --git a/src/PartKeepr/SetupBundle/Resources/config/services.xml b/src/PartKeepr/SetupBundle/Resources/config/setup_services.xml
diff --git a/src/PartKeepr/SetupBundle/Services/ConfigSetupService.php b/src/PartKeepr/SetupBundle/Services/ConfigSetupService.php
@@ -0,0 +1,171 @@
+<?php
+namespace PartKeepr\SetupBundle\Services;
+
+
+use PartKeepr\SetupBundle\Visitor\ConfigVisitor;
+use PartKeepr\SetupBundle\Visitor\LegacyConfigVisitor;
+use Symfony\Bundle\TwigBundle\TwigEngine;
+
+class ConfigSetupService
+{
+ /**
+ * The authentification key length
+ */
+ const KEY_LENGTH = 32;
+
+ /**
+ * @var TwigEngine
+ */
+ private $twig;
+
+ /**
+ * ConfigSetupService constructor.
+ *
+ * @param TwigEngine $twig
+ */
+ public function __construct(TwigEngine $twig)
+ {
+ $this->twig = $twig;
+ }
+
+ public function getConfig ($config) {
+ // Parameter defaults to ensure they exist
+ $parameters = array(
+ "database_driver" => null,
+ "database_host" => null,
+ "database_port" => null,
+ "database_name" => null,
+ "database_password" => null,
+
+ "mailer_transport" => null,
+ "mailer_host" => null,
+ "mailer_port" => null,
+ "mailer_encryption" => null,
+ "mailer_user" => null,
+ "mailer_password" => null,
+ "mailer_auth_mode" => null,
+
+ "authentication_provider" => "PartKeepr.Auth.HTTPBasicAuthenticationProvider",
+
+ "locale" => "en",
+
+ "secret" => $this->generateSecret(),
+
+ "fr3d_ldap.driver.host" => "127.0.0.1",
+ "fr3d_ldap.driver.port" => null,
+ "fr3d_ldap.driver.username" => null,
+ "fr3d_ldap.driver.password" => null,
+ "fr3d_ldap.driver.bindRequiresDn" => false,
+ "fr3d_ldap.driver.baseDn" => "",
+ "fr3d_ldap.driver.accountFilterFormat" => null,
+ "fr3d_ldap.driver.optReferrals" => null,
+ "fr3d_ldap.driver.useSsl" => null,
+ "fr3d_ldap.driver.useStartTls" => null,
+ "fr3d_ldap.driver.accountCanonicalForm" => null,
+ "fr3d_ldap.driver.accountDomainName" => null,
+ "fr3d_ldap.driver.accountDomainNameShort" => null,
+ "fr3d_ldap.user.enabled" => false,
+ "fr3d_ldap.user.baseDn" => "dc=example,dc=com",
+ "fr3d_ldap.user.filter" => null,
+ "fr3d_ldap.user.attribute.username" => null,
+ "fr3d_ldap.user.attribute.name" => null,
+ "fr3d_ldap.user.attribute.email" => null,
+
+ "partkeepr.filesystem.data_directory" => "%kernel.root_dir%/../data/",
+ "partkeepr.cronjob.check" => true,
+ "partkeepr.filesystem.quota" => false,
+ "partkeepr.auth.max_users" => "unlimited",
+ "partkeepr.category.path_separator" => " ➤ ",
+ "partkeepr.maintenance" => false,
+ "partkeepr.maintenance.title" => "",
+ "partkeepr.maintenance.message" => "",
+ "cache.dunglas" => false,
+ "cache.doctrine" => "array",
+ "partkeepr.parts.limit" => false,
+ "partkeepr.users.limit" => false
+ );
+
+ if (function_exists("apc_fetch")) {
+ $parameters["cache.dunglas"] = "api.mapping.cache.apc";
+ $parameters["cache.doctrine"] = "apc";
+ }
+
+ $this->applyIf($parameters, $config);
+
+ $parameters = array_merge($parameters, $config);
+ array_walk_recursive($parameters, function (&$item) {
+ $item = var_export($item, true);
+ });
+
+ ksort($parameters);
+
+ return $this->twig->render('PartKeeprSetupBundle::parameters.php.twig',
+ array("parameters" => $parameters));
+ }
+ public function legacyConfigParser()
+ {
+ if (file_exists($this->getLegacyConfigPath())) {
+ $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
+ $traverser = new \PHPParser_NodeTraverser();
+ $traverser->addVisitor(new LegacyConfigVisitor());
+ $statements = $parser->parse(file_get_contents($this->getLegacyConfigPath()));
+ $traverser->traverse($statements);
+
+ return LegacyConfigVisitor::getConfigValues();
+ }
+
+ return array();
+ }
+
+ public function configParser()
+ {
+ if (file_exists($this->getConfigPath(false))) {
+ $parser = new \PHPParser_Parser(new \PHPParser_Lexer());
+ $traverser = new \PHPParser_NodeTraverser();
+ $traverser->addVisitor(new ConfigVisitor());
+ $statements = $parser->parse(file_get_contents($this->getConfigPath(false)));
+ $traverser->traverse($statements);
+
+ return ConfigVisitor::getConfigValues();
+ }
+
+ return array();
+ }
+
+ public function getConfigPath($test)
+ {
+ if ($test) {
+ $filename = "parameters_setup.php";
+ } else {
+ $filename = "parameters.php";
+ }
+
+ return dirname(__FILE__)."/../../../../app/config/".$filename;
+ }
+
+ public function getLegacyConfigPath()
+ {
+ return dirname(__FILE__)."/../../../../config.php";
+ }
+
+ public function applyIf($target, $source)
+ {
+ foreach ($target as $key => $value) {
+ if (array_key_exists($key, $source)) {
+ $target[$key] = $source[$key];
+ }
+ }
+
+ return $target;
+ }
+
+ public function generateSecret()
+ {
+ $secret = "";
+ for ($i = 0; $i < self::KEY_LENGTH; $i++) {
+ $secret .= chr(65 + rand(0, 16));
+ }
+
+ return $secret;
+ }
+}