public function testParseString() { $map = array("x=1&y=2" => array('x' => 1, 'y' => 2)); foreach ($map as $string => $array) { $data = array(); Utils::parseString($string, $data); $this->assertEquals($data, $array); } }
/** * Manage special input parameters (arrays and files) and target * * @param MvcEvent $event */ public function postRoute(MvcEvent $event) { $match = $event->getRouteMatch(); if (!$match) { return; } $services = $event->getApplication()->getServiceManager(); // [Normalize the arguments] $config = $services->get('config'); $routeName = $match->getMatchedRouteName(); if (isset($config['console']['router']['routes'][$routeName]['options']['arrays'])) { foreach ($config['console']['router']['routes'][$routeName]['options']['arrays'] as $arrayParam) { if ($value = $match->getParam($arrayParam)) { $data = array(); // if the data ends with <char(s)> then we have special delimiter $delimiter = '&'; // Ex: "x[1]=2&y[b]=z" if (strpos($value, '=') === false) { // when we do not have pairs the delimiter in such situations // is set to "comma" by default $delimiter = ','; // Ex: "x,y,z" } if (preg_match("/<(.*?)>\$/", $value, $m)) { // Ex: "x[1]=2|y[b]=z<|>" - the delimiter is | and the pairs are x[1]=2 and y[b]=z $delimiter = $m[1]; $value = substr($value, 0, strlen($value) - strlen("<{$delimiter}>")); } if (strpos($value, '=') === false) { // we do not have pairs $data = explode($delimiter, $value); foreach ($data as $i => $v) { $data[$i] = trim($v); } } else { // Ex: x=1 y[1]=2 y[2]=3 Utils::parseString($value, $data, $delimiter); } $match->setParam($arrayParam, $data); } } } // [Translate all paths to real absolute paths] if (isset($config['console']['router']['routes'][$routeName]['options']['files'])) { $path = $services->get('path'); foreach ($config['console']['router']['routes'][$routeName]['options']['files'] as $param) { if ($value = $match->getParam($param)) { if (!is_array($value)) { $match->setParam($param, $path->getAbsolute($value)); } else { $newValue = array_map(function ($v) use($path) { return $path->getAbsolute($v); }, $value); $match->setParam($param, $newValue); } } } } // [Figure out the target] if (isset($config['console']['router']['routes'][$routeName]['options']['no-target'])) { return; } // Read the default target $targetConfig = $services->get('targetConfig'); // Add manage named target (defined in zsapi.ini) $target = $match->getParam('target'); if ($target) { try { $reader = new ConfigReader(); $data = $reader->fromFile($config['zsapi']['file']); if (empty($data[$target])) { if (!isset($config['console']['router']['routes'][$routeName]['options']['ingore-target-load'])) { throw new \Zend\Console\Exception\RuntimeException('Invalid target specified.'); } else { $data[$target] = array(); } } foreach ($data[$target] as $k => $v) { $targetConfig[$k] = $v; } } catch (\Zend\Config\Exception\RuntimeException $ex) { if (!isset($config['console']['router']['routes'][$routeName]['options']['ingore-target-load'])) { throw new \Zend\Console\Exception\RuntimeException('Make sure that you have set your target first. \\n This can be done with ' . __FILE__ . ' addTarget --target=<UniqueName> --zsurl=http://localhost:10081/ZendServer --zskey= --zssecret='); } } } if (empty($targetConfig) && !($match->getParam('zskey') || $match->getParam('zssecret') || $match->getParam('zsurl'))) { throw new \Zend\Console\Exception\RuntimeException('Specify either a --target= parameter or --zsurl=http://localhost:10081/ZendServer --zskey= --zssecret='); } // optional: override the target parameters from the command line foreach (array('zsurl', 'zskey', 'zssecret', 'zsversion', 'http') as $key) { if (!$match->getParam($key)) { continue; } $targetConfig[$key] = $match->getParam($key); } $outputFormat = $match->getParam('output-format', 'xml'); if ($outputFormat == 'kv') { $outputFormat = "json"; } $apiManager = $services->get('zend_server_api'); $apiManager->setOutputFormat($outputFormat); }