diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8f0327..c049d141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ dev-master ### Features - [profile:show] Added command to display current profile +- [query:update] Introduced `expr()` function to allow setting poperty values using expression language. ### Bug fixes diff --git a/composer.json b/composer.json index fa1f0c9b..bb77db6e 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "phpcr/phpcr-shell", "description": "Shell for PHPCR", "require": { - "symfony/console": "~2.3", + "symfony/console": "~2.4", "jackalope/jackalope": "~1.1", "phpcr/phpcr": "~2.1", "phpcr/phpcr-utils": "~1.2", @@ -10,13 +10,14 @@ "symfony/serializer": "~2.3", "symfony/yaml": "~2.3", "symfony/dependency-injection": "~2.3", + "symfony/expression-language": "~2.4", "dantleech/glob-finder": "~1.0" }, "minimum-stability": "dev", "require-dev": { "symfony/symfony": "2.6", - "symfony/process": "~2.3", - "symfony/filesystem": "~2.3", + "symfony/process": "~2.4", + "symfony/filesystem": "~2.4", "phpunit/phpunit": "~3.7.28", "behat/behat": "~3.0.0", "phpspec/phpspec": "2.0", diff --git a/features/all/phpcr_query_update.feature b/features/all/phpcr_query_update.feature index c885b056..cac81685 100644 --- a/features/all/phpcr_query_update.feature +++ b/features/all/phpcr_query_update.feature @@ -153,3 +153,30 @@ Feature: Execute a a raw UPDATE query in JCR_SQL2 | UPDATE [nt:unstructured] mixin_foo('bar') | | UPDATE [nt:unstructured] APPLY mixin_foo('bar') | | UPDATE [nt:unstructured] mixin_foo'bar') | + + Scenario Outline: Execute update query with expressions + When I execute the "" command + Then the command should not fail + And I save the session + Then the node at "" should have the property "" with value "" + And I should see the following: + """ + 1 row(s) affected + """ + Examples: + | query | path | property | expectedValue | + | UPDATE [nt:unstructured] AS a SET a.title = expr('row.getNode().getName()') WHERE localname() = 'article1' | /cms/articles/article1 | title | article1 | + | UPDATE [nt:unstructured] AS a SET a.title = expr('row.getPath()') WHERE localname() = 'article1' | /cms/articles/article1 | title | /cms/articles/article1 | + + Scenario: Execute an update with a quoted expression (can't do this in Examples above) + When I execute the following command: + """ + UPDATE [nt:unstructured] AS a SET a.weight = expr('row.getNode().getPropertyValue("weight") * 2') WHERE a.name = 'Product One' + """ + Then the command should not fail + And I save the session + Then the node at "/cms/products/product1" should have the property "weight" with value "20" + And I should see the following: + """ + 1 row(s) affected + """ diff --git a/src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php b/src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php index c2d79f63..a23971d8 100644 --- a/src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php +++ b/src/PHPCR/Shell/Console/Command/Phpcr/QueryUpdateCommand.php @@ -90,7 +90,7 @@ public function execute(InputInterface $input, OutputInterface $output) $result = $query->execute(); $rows = 0; - $updateProcessor = new UpdateProcessor(); + $updateProcessor = $this->get('query.update.processor'); foreach ($result as $row) { $rows++; diff --git a/src/PHPCR/Shell/DependencyInjection/Container.php b/src/PHPCR/Shell/DependencyInjection/Container.php index 8743169f..0c6c23be 100644 --- a/src/PHPCR/Shell/DependencyInjection/Container.php +++ b/src/PHPCR/Shell/DependencyInjection/Container.php @@ -40,6 +40,7 @@ public function __construct($mode = PhpcrShell::MODE_STANDALONE) $this->registerPhpcr(); $this->registerEvent(); $this->registerConsole(); + $this->registerQuery(); } public function registerHelpers() @@ -167,6 +168,13 @@ public function registerConsole() ->addArgument(new Reference('phpcr.session')); } + public function registerQuery() + { + $this->register('query.update.expression_language', 'Symfony\Component\ExpressionLanguage\ExpressionLanguage'); + $this->register('query.update.processor', 'PHPCR\Shell\Query\UpdateProcessor') + ->addArgument(new Reference('query.update.expression_language')); + } + public function getMode() { return $this->mode; diff --git a/src/PHPCR/Shell/Query/UpdateProcessor.php b/src/PHPCR/Shell/Query/UpdateProcessor.php index baaab2a2..5f7bf694 100644 --- a/src/PHPCR/Shell/Query/UpdateProcessor.php +++ b/src/PHPCR/Shell/Query/UpdateProcessor.php @@ -12,6 +12,7 @@ namespace PHPCR\Shell\Query; use PHPCR\Query\RowInterface; +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; /** * Processor for node updates @@ -25,7 +26,7 @@ class UpdateProcessor */ private $functionMap = array(); - public function __construct() + public function __construct(ExpressionLanguage $expressionLanguage) { $this->functionMapApply = array( 'mixin_add' => function ($operand, $row, $mixinName) { @@ -42,6 +43,14 @@ public function __construct() ); $this->functionMapSet = array( + 'expr' => function ($operand, $row, $expression) use ($expressionLanguage) { + return $expressionLanguage->evaluate( + $expression, + array( + 'row' => $row, + ) + ); + }, 'array_replace' => function ($operand, $row, $v, $x, $y) { $operand->validateScalarArray($v); foreach ($v as $key => $value) { diff --git a/src/PHPCR/Shell/Test/ContextBase.php b/src/PHPCR/Shell/Test/ContextBase.php index 87a13c1a..6e631a1c 100644 --- a/src/PHPCR/Shell/Test/ContextBase.php +++ b/src/PHPCR/Shell/Test/ContextBase.php @@ -151,6 +151,14 @@ public function iExecuteTheCommand($args) $this->executeCommand($args); } + /** + * @Given I execute the following command: + */ + public function iExecuteTheFollowingCommand(PyStringNode $command) + { + $this->executeCommand($command); + } + /** * @Given /^I execute the following commands:$/ */