Permalink
Browse files

Merge branch 'master' of github.com:mykehsd/SyliusResourceBundle

Conflicts:
	Controller/Configuration.php
  • Loading branch information...
2 parents 9638ceb + 5bb79cf commit 07181b800d60a21e38febefcb6d6a1595e44783a @mykehsd committed Mar 22, 2013
View
@@ -14,11 +14,10 @@ env: MONGO_VERSION=1.3.2
services: mongodb
before_script:
- - pecl -q install mongo-${MONGO_VERSION} && echo "extension=mongo.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
- - composer install --dev --prefer-source
+ - composer install --dev --prefer-source > /dev/null
script: php bin/phpspec run -n -f pretty
notifications:
- irc: "irc.freenode.org#sylius-dev"
- email: travis-ci@sylius.org
+ email: "travis-ci@sylius.org"
+ irc: "irc.freenode.org#sylius-dev"
@@ -24,20 +24,38 @@ class Configuration
{
protected $bundlePrefix;
protected $resourceName;
- protected $pluralResourceName;
protected $templateNamespace;
+ protected $templatingEngine;
+ protected $parameters;
/**
+ * Current request.
+ *
* @var Request
*/
protected $request;
- public function __construct($bundlePrefix, $resourceName, $templateNamespace = null)
+ public function __construct($bundlePrefix, $resourceName, $templateNamespace, $templatingEngine = 'twig')
{
+
$this->bundlePrefix = $bundlePrefix;
$this->resourceName = $resourceName;
- $this->pluralResourceName = Pluralization::pluralize($resourceName);
$this->templateNamespace = $templateNamespace;
+ $this->templatingEngine = $templatingEngine;
+
+ $this->parameters = array();
+ }
+
+ public function load(Request $request)
+ {
+ $this->request = $request;
+
+ $parameters = $request->attributes->get('_sylius', array());
+ $parser = new ParametersParser();
+
+ $parameters = $parser->parse($parameters, $request);
+
+ $this->parameters = $parameters;
}
public function getBundlePrefix()
@@ -52,31 +70,22 @@ public function getResourceName()
public function getPluralResourceName()
{
- return $this->pluralResourceName;
+ return Pluralization::pluralize($this->resourceName);
}
public function getTemplateNamespace()
{
return $this->templateNamespace;
}
- public function setRequest(Request $request)
- {
- $this->request = $request;
- }
-
- public function isHtmlRequest()
+ public function getTemplatingEngine()
{
- if (null === $this->request) {
- throw new \BadMethodCallException('Request is unknown, cannot check its format');
- }
-
- return 'html' === $this->request->getRequestFormat();
+ return $this->templatingEngine;
}
public function isApiRequest()
{
- return !$this->isHtmlRequest();
+ return 'html' !== $this->request->getRequestFormat();
}
public function getServiceName($service)
@@ -89,45 +98,33 @@ public function getEventName($event)
return sprintf('%s.%s.%s', $this->bundlePrefix, $this->resourceName, $event);
}
- public function getIdentifierName()
+ public function getTemplateName($name)
{
- return $this->get('_identifier', 'id');
+ return sprintf('%s:%s.%s', $this->templateNamespace, $name, $this->templatingEngine);
}
- public function getIdentifierValue()
+ public function getTemplate($name)
{
- return $this->request->get($this->getIdentifierName());
- }
-
- public function getIdentifierCriteria()
- {
- if (null === $this->request) {
- throw new \BadMethodCallException('Request is unknown, cannot get single resource criteria');
- }
-
- return array(
- $this->getIdentifierName() => $this->getIdentifierValue()
- );
- }
-
- public function getTemplate()
- {
- return $this->get('_template');
+ return $this->get('template', $this->getTemplateName($name));
}
public function getFormType()
{
- return $this->get('_form', $this->getDefaultFormType());
+ return $this->get('form', sprintf('%s_%s', $this->bundlePrefix, $this->resourceName));
}
- public function getDefaultFormType()
+ public function getRouteName($name)
{
- return sprintf('%s_%s', $this->bundlePrefix, $this->resourceName);
+ return sprintf('%s_%s_%s', $this->bundlePrefix, $this->resourceName, $name);
}
- public function getRedirectRoute()
+ public function getRedirectRoute($name)
{
- $redirect = $this->get('_redirect');
+ $redirect = $this->get('redirect');
+
+ if (null === $redirect) {
+ return $this->getRouteName($name);
+ }
if (is_array($redirect)) {
return $redirect['route'];
@@ -138,99 +135,74 @@ public function getRedirectRoute()
public function getRedirectParameters()
{
- $redirect = $this->get('_redirect');
+ $redirect = $this->get('redirect');
- if (!is_array($redirect)) {
+ if (null === $redirect || !is_array($redirect)) {
return array();
}
- foreach ($redirect['parameters'] as $key => $parameter) {
- if ('$' === $parameter[0]) {
- $redirect['parameters'][$key] = $this->request->get(str_replace('$', '', $redirect['parameters'][$key]));
- }
- }
-
return $redirect['parameters'];
}
- public function isPaginated()
- {
- return (Boolean) $this->get('_paginate', true);
- }
-
- public function getPaginationMaxPerPage()
+ public function getLimit()
{
- return (int) $this->get('_paginate', 10);
+ return (int) $this->get('limit', 10);
}
- public function getLimit()
+ public function isPaginated()
{
- return (int) $this->get('_limit', 10);
+ return (Boolean) $this->get('paginate', true);
}
public function getDeleted()
{
return $this->request->get('deleted', false);
}
- public function isSortable()
+ public function getPaginationMaxPerPage()
{
- return (Boolean) $this->get('_sortable', false);
+ return (int) $this->get('paginate', 10);
}
public function isFilterable()
{
- return (Boolean) $this->get('_filterable', false);
+ return (Boolean) $this->get('filterable', false);
}
public function getCriteria()
{
- $defaultCriteria = $this->get('_criteria', array());
+ $defaultCriteria = $this->get('criteria', array());
- if ($this->isFilterable() && null !== $this->request) {
- return $this->request->get('criteria', $defaultCriteria);
+ if ($this->isFilterable()) {
+ return array_merge($defaultCriteria, $this->request->get('criteria', array()));
}
return $defaultCriteria;
}
+ public function isSortable()
+ {
+ return (Boolean) $this->get('sortable', false);
+ }
+
public function getSorting()
{
- $defaultSorting = $this->get('_sorting', array());
+ $defaultSorting = $this->get('sorting', array());
- if ($this->isSortable() && null !== $this->request) {
- return $this->request->get('sorting', $defaultSorting);
+ if ($this->isSortable()) {
+ return array_merge($defaultSorting, $this->request->get('sorting', array()));
}
return $defaultSorting;
}
public function getFlashMessage()
{
- return $this->get('_flash');
- }
-
- public function getRoute()
- {
- return sprintf('%s_%s', $this->bundlePrefix, $this->resourceName);
- }
-
- public function getIndexRoute()
- {
- return sprintf('%s_%s', $this->bundlePrefix, $this->pluralResourceName);
- }
-
- public function getEngineParameterName()
- {
- return sprintf('%s.engine', $this->getBundlePrefix());
+ return $this->get('flash');
}
protected function get($parameter, $default = null)
{
- if (null === $this->request) {
- return $default;
- }
-
- return $this->request->attributes->get($parameter, $default);
+ return array_key_exists($parameter, $this->parameters) ? $this->parameters[$parameter] : $default;
}
}
@@ -0,0 +1,37 @@
+<?php
+
+/*
+ * This file is part of the Sylius package.
+ *
+ * (c) Paweł Jędrzejewski
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sylius\Bundle\ResourceBundle\Controller;
+
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Configuration parameters parser.
+ *
+ * @author Paweł Jędrzejewski <pjedrzejewski@sylius.pl>
+ */
+class ParametersParser
+{
+ public function parse(array $parameters, Request $request)
+ {
+ foreach($parameters as $key => $value) {
+ if (is_array($value)) {
+ $parameters[$key] = $this->parse($value, $request);
+ }
+
+ if (is_string($value) && 0 === strpos($value, '$')) {
+ $parameters[$key] = $request->get(substr($value, 1));
+ }
+ }
+
+ return $parameters;
+ }
+}
Oops, something went wrong.

0 comments on commit 07181b8

Please sign in to comment.