Phing and composer
Asked Answered
A

4

10

we use phing to build and test our project. I want to remove dependencies on PEAR as much as possible so I can run different versions of packages for different projects. I have created a composer.json file which install all the necessary packages

{
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.2.*",
        "doctrine/doctrine-orm-module": "*",
        "phpoption/phpoption": "*"
    },
    "require-dev": {
        "phing/phing": "*",
        "phpunit/phpunit": "*",
        "pdepend/pdepend": "*",
        "phpmd/phpmd": "*",
        "phploc/phploc": "*",
        "phpdocumentor/phpdocumentor": "*",
        "squizlabs/php_codesniffer": "*",
        "mayflower/php-codebrowser": "*",
        "sebastian/phpcpd": "*",
        "zendframework/zftool": "dev-master",
        "zendframework/zend-form": "*",
        "hounddog/doctrine-data-fixture-module": "*",
        "pear/console_commandline": "dev-trunk",
        "pear/log": "dev-master",
        "pear/pear_exception": "dev-master"
    },
    "config": {
        "bin-dir": "vendor/bin/"
    }
}

And I have a phing build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="SolExactConnector" default="build">
    <property name="basedir" value="." override="true"/>
    <property name="source" value="${basedir}/module"/>

    <fileset dir="${source}" id="sourceWithoutTests">
        <include name="**/*.php"/>

        <exclude name="*/test/"/>

        <exclude name="*/Module.php"/>
        <exclude name="*/config/module.config.php"/>
        <exclude name="*/test/Bootstrap.php"/>
    </fileset>

    <fileset dir="${source}" id="sourceWithTests">
        <include name="**/*.php"/>

        <exclude name="*/Module.php"/>
        <exclude name="*/config/module.config.php"/>
        <exclude name="*/test/Bootstrap.php"/>
    </fileset>

    <fileset dir="${source}" id="tests">
        <include name="*/test/**/*Test.php"/>
    </fileset>


    <target name="prepare" description="Clean up and create artifact directories">
        <delete dir="${basedir}/build/api"/>
        <delete dir="${basedir}/build/code-browser"/>
        <delete dir="${basedir}/build/coverage"/>
        <delete dir="${basedir}/build/logs"/>
        <delete dir="${basedir}/build/pdepend"/>
        <delete dir="${basedir}/build/docs"/>

        <mkdir dir="${basedir}/build/api"/>
        <mkdir dir="${basedir}/build/code-browser"/>
        <mkdir dir="${basedir}/build/coverage"/>
        <mkdir dir="${basedir}/build/logs"/>
        <mkdir dir="${basedir}/build/pdepend"/>
        <mkdir dir="${basedir}/build/docs"/>
    </target>

    <target name="phpunit" description="Run unit tests" depends="prepare">
        <coverage-setup database="${basedir}/build/logs/coverage.db">
            <fileset refid="sourceWithoutTests"/>
        </coverage-setup>
        <phpunit haltonfailure="true" haltonerror="true" printsummary="true" bootstrap="test/Bootstrap.php"
                 codecoverage="true">
            <formatter todir="${basedir}/build/logs" type="clover" outfile="clover.xml"/>
            <formatter todir="${basedir}/build/logs" type="xml" outfile="junit.xml"/>
            <batchtest>
                <fileset refid="tests"/>
            </batchtest>
        </phpunit>
    </target>

    <target name="lint" description="Perform syntax check of sourcecode files" depends="prepare">
        <phplint haltonfailure="true" cachefile="${basedir}/build/logs/lint.cache">
            <fileset refid="sourceWithTests"/>
        </phplint>
    </target>


    <target name="pdepend" description="Generate jdepend.xml and software metrics charts using PHP_Depend"
            depends="prepare">
        <phpdepend file="${source}">
            <logger type="jdepend-xml" outfile="${basedir}/build/logs/jdepend.xml"/>
            <logger type="jdepend-chart" outfile="${basedir}/build/pdepend/dependencies.svg"/>
            <logger type="overview-pyramid" outfile="${basedir}/build/pdepend/overview-pyramid.svg"/>
        </phpdepend>
    </target>

    <target name="phpmd" description="Generate pmd.xml using PHPMD" depends="prepare">
        <phpmd file="${source}">
            <formatter type="xml" outfile="${basedir}/build/logs/pmd.xml"/>
        </phpmd>
    </target>

    <target name="phpcpd" description="Generate pmd-cpd.xml using PHPCPD" depends="prepare">
        <phpcpd>
            <formatter type="pmd" outfile="${basedir}/build/logs/pmd-cpd.xml"/>
            <fileset refid="sourceWithTests"/>
        </phpcpd>
    </target>

    <target name="phploc" description="Generate phploc.xml" depends="prepare">
        <phploc reportType="xml" reportName="phploc"
                reportDirectory="${basedir}/build/logs">
            <fileset refid="sourceWithTests"/>
        </phploc>
    </target>

    <target name="phpcs" description="Generate checkstyle.xml using PHP_CodeSniffer" depends="prepare">
        <phpcodesniffer
                standard="PSR2"
                showSniffs="true"
                showWarnings="true">
            <fileset refid="sourceWithTests"/>
            <formatter type="default" usefile="false"/>
            <formatter type="checkstyle" outfile="${basedir}/build/logs/checkstyle-codesniffer.xml"/>
        </phpcodesniffer>
    </target>

    <target name="hphpa" description="HipHop's static analyzer" depends="prepare">
        <exec executable="wget" checkreturn="true">
            <arg line="https://phar.phpunit.de/hphpa.phar"/>
        </exec>
        <exec executable="php hphpa.phar" checkreturn="true">
            <arg line="--checkstyle ${basedir}/build/logs/checkstyle-hphpa.xml"/>
            <arg line="${source}"/>
        </exec>
        <delete file="hphpa.phar"/>
    </target>

    <target name="phpdoc2" description="Generate API documentation using phpDox" depends="prepare">
        <phpdoc2 title="API Documentation"
                 destdir="${basedir}/build/docs"
                 template="responsive-twig">
            <fileset refid="sourceWithTests"/>
        </phpdoc2>
    </target>

    <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser" depends="prepare">
        <exec executable="phpcb">
            <arg line="--log    ${basedir}/build/logs
              --source ${source}
              --output ${basedir}/build/code-browser"/>
        </exec>
    </target>

    <target name="composer" description="Installing dependencies" depends="prepare">
        <delete dir="${basedir}/vendor"/>

        <composer command="install">
            <arg value="--dev"/>
        </composer>
    </target>

    <target name="doctrine" description="Building Database/Doctrine" depends="prepare">
        <copy file="${basedir}/config/autoload/local.php.test" tofile="${basedir}/config/autoload/local.php"
              haltonerror="true"/>
        <delete dir="${basedir}/data/db/"/>
        <mkdir dir="${basedir}/data/db/"/>
        <chmod file="${basedir}/data/db/" mode="777"/>

        <exec executable="${basedir}/vendor/bin/doctrine-module">
            <arg value="orm:schema-tool:create"/>
        </exec>

        <delete dir="${basedir}/data/DoctrineORMModule/Proxy"/>
        <mkdir dir="${basedir}/data/DoctrineORMModule/Proxy"/>

        <exec executable="${basedir}/vendor/bin/doctrine-module">
            <arg value="orm:generate-proxies"/>
        </exec>

        <exec executable="${basedir}/vendor/bin/doctrine-module">
            <arg value="data-fixture:import"/>
        </exec>

    </target>

    <target name="build"
            depends="lint,pdepend,phpcs,phpcpd,phpmd,hphpa,phpdoc2,composer,doctrine,phpunit,phpcb"/>
</project>

Some targets (like phpunit, phpmd and phploc) run fine but others don't? E.g. when I run phpcpd I get this error:

Execution of target "phpcpd" failed for the following reason: /home/munnik/Sites/SolExactConnector/trunk/build.xml:83:16: /home/munnik/Sites/SolExactConnector/trunk/build.xml:83:16: PHPCPDTask depends on PHPCPD being installed and on include_path.

BUILD FAILED /home/munnik/Sites/SolExactConnector/trunk/build.xml:83:16: /home/munnik/Sites/SolExactConnector/trunk/build.xml:83:16: PHPCPDTask depends on PHPCPD being installed and on include_path. Total time: 0.1250 seconds

Do I need to add the composer autoload or something like that?

Americanist answered 7/10, 2013 at 13:58 Comment(1)
It seems Phing is assuming packages are installed using PEAR. Various check in different tasks check for PEAR paths and don't check for composer paths. I think this is a bug in Phing.Americanist
G
22

To use composer autoloader instead of global PEAR packages you can add following line to beginning of your build.xml:

<php expression="include('vendor/autoload.php')"/>

This helped me with PHPUnit (I dont have a global PHPUnit PEAR installation). Think this will help you successfully load all your composer packages.

Gamosepalous answered 19/10, 2013 at 8:47 Comment(2)
This doesn't help with phpDocHenshaw
Its important to note that the <php expression ../> is a tag that goes inside the top-level <project> tag - this may not be obvious to people with no Phing experience.Seriate
D
6

Phing now provides an autoloader task, which you may use to include your own autoloader or Composer's autoloader.

For example:

<autoloader autoloaderpath="vendor/autoload.php"/>
Dewy answered 16/12, 2015 at 23:37 Comment(0)
B
1

To set the composer autoloader you can create a target as:

<target name="require.autoload">
    <adhoc><![CDATA[
    require_once 'lib/composer/autoload.php';
 ]]></adhoc>
</target>

Then all the targets that need autoloader, has this requirement

 <target name="test.coverage.html" depends="require.autoload">

Note: require once the file placed in

"config": {
            "vendor-dir": "lib/composer"
Bankroll answered 10/7, 2014 at 8:27 Comment(0)
J
1

I ran into exactly the same problem, but didn't have much luck modifying autoloaders. I decided to work around by creating <exec> tasks instead for simplicity. There's not a lot of difference apart from the loss of nested <fileset> elements (these needs to be specified as arguments instead).

Justly answered 7/8, 2014 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.