partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit c5df62a8f76696226cbe631d2a67faaef2eccf97
parent ef33d5507f8ae121ccd32f898a4b4182882dce7b
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 15 Nov 2015 18:30:40 +0100

Parse existing symfony config, fixes #461

Diffstat:
Msrc/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php | 79+++++++++++++++++++++++++++++++++++--------------------------------------------
Asrc/PartKeepr/SetupBundle/Visitor/ConfigVisitor.php | 40++++++++++++++++++++++++++++++++++++++++
Mweb/setup/js/SetupTests/ExistingConfigurationTest.js | 1-
3 files changed, 75 insertions(+), 45 deletions(-)

diff --git a/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php b/src/PartKeepr/SetupBundle/Controller/ExistingConfigParserController.php @@ -1,12 +1,11 @@ <?php namespace PartKeepr\SetupBundle\Controller; +use PartKeepr\SetupBundle\Visitor\ConfigVisitor; use PartKeepr\SetupBundle\Visitor\LegacyConfigVisitor; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Yaml\Parser; class ExistingConfigParserController extends SetupController { @@ -24,22 +23,20 @@ class ExistingConfigParserController extends SetupController return new JsonResponse($response); } - $response = array( + $response = array( "success" => true, "errors" => [], "message" => "Existing configuration imported successfully", ); try { - $legacyConfig = $this->legacyConfigParser(); + $config = array( + "values" => array(), + ); - if (count($legacyConfig) == 0) { - $response["message"] = "No configuration found"; - } else { - $config = array( - "values" => array() - ); + $legacyConfig = $this->legacyConfigParser(); + if (count($legacyConfig) > 0) { if (array_key_exists("partkeepr.database.driver", $legacyConfig)) { $config["values"]["database_driver"] = $legacyConfig["partkeepr.database.driver"]; } @@ -97,38 +94,15 @@ class ExistingConfigParserController extends SetupController $response["config"] = $config; } - /*if (file_exists($this->getConfigPath(false))) { - $yaml = new Parser(); - $data = $yaml->parse(file_get_contents($this->getConfigPath(false))); - - if (array_key_exists("parameters", $data)) { - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["driver"] = $this->getParameter("database_driver"); - } - - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["host"] = $this->getParameter("database_host"); - } - - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["port"] = $this->getParameter("database_port"); - } - - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["dbname"] = $this->getParameter("database_name"); - } - - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["username"] = $this->getParameter("database_user"); - } - - if (array_key_exists("database_driver", $data["parameters"])) { - $config["database"]["password"] = $this->getParameter("database_password"); - } - } - }*/ + $config = $this->configParser(); + if (count($config) > 0) { + $response["config"]["values"] = $config; + } + if (count($response["config"]) == 0) { + $response["message"] = "No configuration found"; + } } catch (\Exception $e) { $response["success"] = false; @@ -139,12 +113,13 @@ class ExistingConfigParserController extends SetupController return new JsonResponse($response); } - protected function legacyConfigParser () { - if (file_exists($this->getLegacyAuthConfigPath())) { + 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->getLegacyAuthConfigPath())); + $statements = $parser->parse(file_get_contents($this->getLegacyConfigPath())); $traverser->traverse($statements); return LegacyConfigVisitor::getConfigValues(); @@ -153,7 +128,23 @@ class ExistingConfigParserController extends SetupController return array(); } - protected function getLegacyAuthConfigPath () { + 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/Visitor/ConfigVisitor.php b/src/PartKeepr/SetupBundle/Visitor/ConfigVisitor.php @@ -0,0 +1,40 @@ +<?php +namespace PartKeepr\SetupBundle\Visitor; + + +class ConfigVisitor extends \PHPParser_NodeVisitorAbstract +{ + private static $configValues = array(); + + public function enterNode(\PHPParser_Node $node) { + if ($node instanceof \PHPParser_Node_Expr_MethodCall) { + if ($node->var->name == "container" && $node->name == "setParameter") { + if (array_key_exists(0, $node->args) && array_key_exists(1, $node->args)) { + if ($node->args[1]->value instanceof \PHPParser_Node_Scalar_String) { + self::$configValues[$node->args[0]->value->value] = $node->args[1]->value->value; + } elseif ($node->args[1]->value instanceof \PHPParser_Node_Scalar_LNumber) { + self::$configValues[$node->args[0]->value->value] = $node->args[1]->value->value; + } elseif ($node->args[1]->value instanceof \PHPParser_Node_Expr_ConstFetch) { + switch (strtolower($node->args[1]->value->name->parts[0])) { + case "true": + $value = true; + break; + case "false": + $value = false; + break; + case "null": + $value = null; + break; + } + self::$configValues[$node->args[0]->value->value] = $value; + } + } + } + + } + } + + public static function getConfigValues () { + return self::$configValues; + } +} diff --git a/web/setup/js/SetupTests/ExistingConfigurationTest.js b/web/setup/js/SetupTests/ExistingConfigurationTest.js @@ -12,7 +12,6 @@ Ext.define('PartKeeprSetup.ExistingConfigurationTest', { var config = PartKeeprSetup.getApplication().getSetupConfig(); Ext.merge(config, data.config); - console.log(config); config.existingConfig = true; } }