简体   繁体   中英

How to check if variable is empty in phing build xml?

I referred the documentation and tried this -

<php function="empty" returnProperty="productionSameAsExpectedBranch">
    <param value="${productionDeviationFromExpectedBranch}"/>
</php>

But it gives error -

[php] Calling PHP function: empty() [PHP Error] call_user_func_array() expects parameter 1 to be a valid callback, function 'empty' not found or invalid function name [line 125 of /usr/share/pear/phing/tasks/system/PhpEvalTask.php]

I'm using a construct like this:

<if>
  <or>
    <not><isset property="productionSameAsExpectedBranch" /></not>
    <and>
      <isset property="productionSameAsExpectedBranch" />
      <equals arg1="${productionSameAsExpectedBranch}" arg2="" />
    </and>
  </or>
  <then>
    <!-- it is not set or empty -->
    <!--property name="productionSameAsExpectedBranch" value="something" override="true" /-->
  </then>
<else />
</if>

I would use isfalse condition to check if a string is empty.

<target name="empty-string">

    <property name="productionSameAsExpectedBranch" value=""/>

    <if>
        <isfalse value="${productionSameAsExpectedBranch}"/>
        <then>
            <echo>Property is string empty, do something!</echo>
        </then>
    </if>

</target>

Note I set productionSameAsExpectedBranch to empty string first, I do this to avoid non-existing property. The property will not be overwritten if already exists.

This works because internally isfalse uses PHP's casting.

Another solution would be to use the matches condition with an empty line pattern like:

<target name="empty-string">

    <property name="productionSameAsExpectedBranch" value=""/>

    <if>
        <matches string="${productionSameAsExpectedBranch}" pattern="^$"/>
        <then>
            <echo>Property is string empty, do something!</echo>
        </then>
    </if>

</target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM