Kim Stacks
Kim Stacks

Reputation: 10822

how to execute task in phing given double and single quotes

This is my command I wish to execute.

php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"

This is my attempt in phing

<exec command='php -r "apc_clear_cache(); apc_clear_cache(\'user\'); apc_clear_cache(\'opcode\');"' outputProperty="result" />

This is what I get

BUILD FAILED
Error reading project file [wrapped: /var/virtual/abc.com/build.xml:171:26: > required]
Total time: 0.2093 seconds

Please advise.

Update:

I solved my problem by writing a bash script called RunApcClearCache.sh that runs

php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"

and then calling the bash script using ./RunApcClearCache.sh

If there is a better way, I would like to hear it.

I am also disinclined to write a task just for something so straightforward. Surely there must be a way to escape double quotes properly inside a exectask.

Upvotes: 4

Views: 2323

Answers (4)

Antonis Karagiannis
Antonis Karagiannis

Reputation: 71

Phing requires valid XML. In valid XML you cannot use " directly inside an attribute.

You need to escape it / use its equivalent entity. There are five predefined entities:

&lt; represents "<"
&gt; represents ">"
&amp; represents "&"
&apos; represents '
&quot; represents "

Try using the entities and not the characters. So use &quot; instead of " inside the command attribute...

https://en.wikipedia.org/wiki/XML#Escaping

Upvotes: 6

Jens Peters
Jens Peters

Reputation: 2161

I had the same problem. To avoid shell scripts I defined a adhoc task, which maps the corresponding framework functions, e.g.

<adhoc-task name="fileHelper"><![CDATA[
  class FileHelper extends Task {

      private $lib = null;
      private $task = null;
      private $stub = null;

      function setTask($task) {
          $this->task = $task;
      }

      function setStub($stub) {
          $this->stub = $stub;
      }

      function setLib($lib) {
          $this->lib = $lib;
      } 

      function main() {
        $task = $this->task;
    $this->$task();
      }

      function generatePharStub() {
        include_once $this->lib . '/FileHelper.php'; 
        HTMLBuilder\FileHelper::generatePharStub($this->stub);
      }

      function generateIncludes() {
        include_once $this->lib . '/FileHelper.php'; 
        HTMLBuilder\FileHelper::generateIncludelist();
      }
  }
]]></adhoc-task>

Can be called the following way:

  <fileHelper lib="${lib}" stub="${stub}" task="generateIncludes" />

In my case ${lib} points to the library directory. ${stub} is the generated phar stub file.

Upvotes: 0

vralfy
vralfy

Reputation: 1

i would create a new task and excute the php code there. You can also write a new php file and execute this one with php.

Upvotes: 0

botenvouwer
botenvouwer

Reputation: 4432

Try this:

<exec command="php -r 'apc_clear_cache(); apc_clear_cache(\'user\'); apc_clear_cache(\'opcode\');'" outputProperty="result" />

not sure it works

Upvotes: 0

Related Questions