<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Théo Fidry on Medium]]></title>
        <description><![CDATA[Stories by Théo Fidry on Medium]]></description>
        <link>https://medium.com/@tfidry?source=rss-8db48ea4f3f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Zu8FCos45RoGTdgDruh-xw.jpeg</url>
            <title>Stories by Théo Fidry on Medium</title>
            <link>https://medium.com/@tfidry?source=rss-8db48ea4f3f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 12:52:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tfidry/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PHARs roadmap]]></title>
            <link>https://medium.com/@tfidry/phars-roadmap-870671a847c1?source=rss-8db48ea4f3f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/870671a847c1</guid>
            <category><![CDATA[phar]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Théo Fidry]]></dc:creator>
            <pubDate>Tue, 02 Jan 2018 13:20:36 GMT</pubDate>
            <atom:updated>2018-01-02T13:20:36.400Z</atom:updated>
            <content:encoded><![CDATA[<p>In the articles <a href="https://medium.com/@tfidry/managing-your-dependencies-in-php-321d584441ab">Managing your dependencies in PHP</a> and <a href="https://medium.com/@tfidry/create-and-deploy-secure-phars-c5572f10b4dd">Create and deploy secure PHARs</a> I covered some use cases for PHARs as well as introduced a few tools making it easier to work with them.</p><p>However, I think the tooling for PHARs could be improved with:</p><ul><li>Isolating dependencies (see more about the issue <a href="https://medium.com/@tfidry/managing-your-dependencies-in-php-321d584441ab#f2f7">here</a>).</li><li>Installing, updating and keeping track of PHARs in a secure fashion is still not straightforward enough.</li><li>The overall workflow is under documented and far from being optimal.</li></ul><p>In this article, I’ll try to cover those points</p><p>So in this article, I’ll try to cover all the points above and explain what I have in mind. If the project picks your interest, then you should get a clear idea of what the plan is and otherwise you can always ping me :)</p><p>The whole roadmap is accessible at the <a href="https://github.com/humbug/box/projects/1">box’s Humbug fork project</a>.</p><h3><a href="#3278">PHP-Scoper</a></h3><p><a href="https://github.com/humbug/php-scoper">PHP-Scoper</a> is an ambitious project. It aims at tackling the first issue regarding the dependencies isolation. In theory it’s relatively simple: change the namespace of the bundled code to avoid any conflict.</p><p>At the time of writing, PHP-Scoper is successfully being used on <a href="https://github.com/api-platform/schema-generator">schema-generator</a> and itself. It’s still relatively young though so a few issues are expected to arise. The main remaining issues are:</p><ul><li><a href="https://github.com/humbug/php-scoper/issues/49">Add a box integration</a>: this requires to create a plugin for <a href="https://github.com/humbug/box">Box</a> as well as switching the box configuration which is in a JSON format right now into a PHP one.</li><li>Fix the prefixing of classes belonging to the global namespace: <a href="https://github.com/humbug/php-scoper/pull/117">#117</a> and <a href="https://github.com/humbug/php-scoper/issues/135">#135</a>.</li><li><a href="https://github.com/humbug/php-scoper/issues/127">Add PSR-0 support</a>.</li></ul><p>Once this project is done, this would be extremely useful for projects like <a href="https://phpunit.de/">PHPUnit</a>. Indeed, a test framework such as PHPUnit requires a lot of dependencies which <a href="https://medium.com/@tfidry/managing-your-dependencies-in-php-321d584441ab#9360">can be an issue</a>. Instead of importing phpunit/phpunit as a dev dependency which includes:</p><ul><li>Test cases and suites to write the tests</li><li>Assertions</li><li>The engine which is what is used to execute your tests</li></ul><p>A lot of those dependencies are used for the engine which you as a consumer don’t need. With PHP-Scoper, a lightweight library containing only the test cases and assertions could be provided. Then to execute the tests, you could use the PHAR and as a result this would greatly reduce the number of dependencies that you would require to use PHPUnit.</p><h3><a href="#5aea">Box</a></h3><p><a href="https://medium.com/@tfidry/create-and-deploy-secure-phars-c5572f10b4dd#0e03">Box is an application to build and manage PHARs</a>. The <a href="https://github.com/box-project/box2">box project</a> itself is at the same time quite good and quite bad. The good part is that it’s working relatively well and is very stable. The bad parts are: a lack of maintenance/support, heavy use of deprecated libraries, missing some features.</p><p>As I don’t have control on that project, <a href="https://github.com/humbug/box">I forked it under the Humbug umbrella</a> and I hope at some point this could be merged back to the main repository.</p><p>Over the last few months, <a href="https://github.com/humbug/box/graphs/commit-activity">I did some refactoring</a> which tackles the worst part of it:</p><ul><li>Update the dependencies and remove the usage of the deprecated ones</li><li>Merge a few repositories (<a href="https://github.com/box-project/box2">the box project</a> and <a href="https://github.com/box-project/box2-lib">the box library</a> among others) to ease the management</li><li>Rework some tests &amp; tools to make things easier to start and maintain</li></ul><p>What is missing from it now is the juicy parts.</p><h4><a href="#f432">Allow unsigned PHARs for development purposes</a></h4><p>As explained in <a href="https://github.com/humbug/box/issues/14">#14</a>, once you start signing your PHAR files, this is becoming really inconvenient for the contributors as this requires them to create a private key as well. It’s possible to avoid that with a little trick (see the issue for more about it) but I believe this could be simpler.</p><p>Also to encourage people to sign their PHARs, a warning could be displayed when a non-signed PHAR is being used.</p><h4><a href="#e804">Checking constraints before running</a></h4><p>As explained in <a href="https://github.com/humbug/box/issues/13">#13</a>, unlike when installing a dependency with Composer, no constraint check is done when installing/using a PHAR. It can be done in an installer but then it’s easy to miss it if downloading the PHAR directly, keeping the PHAR around and updating it or copying it from somewhere.</p><p><a href="https://github.com/humbug/php-scoper/blob/master/bin/php-scoper#L62">An attempt has been made in PHP-Scoper</a> based on <a href="https://github.com/symfony/requirements-checker">Symfony’s requirement checker</a>. It however has a few drawbacks:</p><ul><li>It is limited to checking extensions and the PHP platform (which can be fine) but don’t take into consideration polyfills. A possible solution could be to look if a polyfill is included as well and ignore the extension requirement if it is.</li><li>As of now it only checks the project rootcomposer.json and doesn&#39;t account for the dependencies composer.json files, which is incomplete. If for example you require ext-xml in your composer.json and one of your dependencies requires ext-pdo, the first one will be properly checked but not the second.</li><li>This checker needs to run in an environment in which the application will not be able to. For example in the case of PHP-Scoper, it needs to be compatible with PHP 5.6 (even though PHP-Scoper requires PHP 7.1) to be able to provide a nice error message when running on PHP 5.6. This is also true for extensions and it results in a lot of boiler template. I didn’t invest much time on it but I think this could be greatly simplified.</li></ul><h3><a href="#7808">Provide an idiomatic way to require the autoloader</a></h3><p>See <a href="https://github.com/humbug/box/issues/12">#12</a> for the GitHub issue.</p><p>At the entry point of the application (bundled in the PHAR or not), the autoloader needs to be required. Depending of if you are inside the PHAR, inside the project or installed it as a global or regular dependency, the location will differ.</p><p>This is certainly not the hardest issue, but I think it would be nice to have one bullet-proof script which anyone could use as a reference instead of taking the risk to forget one case.</p><h3><a href="#0db8">Performance and optimisations</a></h3><p>There is two kind of optimisations that can be provided.</p><p>The first one is about the speed of the PHAR building itself. The way this is done at the time of writing is rather straightforward: retrieve all the files to add to the PHAR and process (compression for example) them one by one. There is a lot of optimisation that could be done by parallelising this work, especially once a PHP-Scoper integration will be added. Indeed if compressing a file can be relatively fast in which case parallelisation won’t offer much, scoping a file is another story and it’s <em>much</em> slower.</p><p>A second kind of optimisation is for the packaged application, i.e. the PHAR itself. Here is the list of the possible optimisations (some are already available):</p><ul><li>Reduce the number of files: get rid of useless files like README.md, doc, tests etc as well as development dependencies. Getting rid of files can easily be done via the exclude pattern in the box configuration and excluding dev dependencies can be done thanks to composer --no-dev option when installing them with composer install.</li><li>Make use of compressors: there is currently already a JSON, PHP and JavaScript compressor available out of the box with Box. Whilst I think the PHP one could be enhanced, this is already a good base and depending of the use case one could easily add a new compressor. On a quick test, I noticed a 30% size decrease of the PHAR with compressors alone.</li><li>Compress the PHAR: there is several compressing formats like GZ and BZ2 available which reduce the PHAR size up to 80%.</li><li>Prefixing: During the prefixing process, PHP-Scoper transforms as much as possible calls into fully qualified calls which makes it faster as either help opcache or avoid an extra autoloading step (as there is no longer any guess work to do to know if a function is from the current namespace or from the global namespace). See <a href="https://github.com/Roave/FunctionFQNReplacer">Roave’s FunctionFQNReplace</a> for more.</li></ul><h3><a href="#0381">Updating PHARs</a></h3><p>As of now <a href="https://github.com/humbug/phar-updater">Humbug’s PharUpdater</a> is the most popular library for providing a PHAR update command. However as mentioned in <a href="https://github.com/humbug/phar-updater/issues/53">#53</a>, I can’t help but to find too tedious to use. There is too much disparity in its usage even for basic cases. I’m sure there is a way to make it easier and provide a more consistent behaviour across PHARs.</p><h3><a href="#5fed">Installing PHARs</a></h3><p>As mentioned on GitHub on <a href="https://github.com/humbug/phar-updater/issues/53">#53</a> and explained <a href="https://medium.com/@tfidry/create-and-deploy-secure-phars-c5572f10b4dd#7877">here</a>, there is two “good” ways to do it:</p><ul><li>Using an installer</li><li>Using PHIVE</li></ul><p>I personally didn’t make my mind yet about PHIVE. Maybe it’s the way to go and it’s only missing a few features and a bit of UX love.</p><p>The alternative which is using an installer, is so far too tedious in my opinion: there is no idiomatic way to do it. It’s often an in-house long script like you can see with <a href="https://github.com/composer/getcomposer.org/blob/master/web/installer">Composer</a>, <a href="https://github.com/box-project/box2/blob/gh-pages/installer.php">Box</a> or <a href="https://github.com/puli/installer/blob/master/src/Installer.php">Puli</a>. Those scripts are verbose, repetitive and error prone. They are in my opinion not viable as they are especially since they are critical from a security point of view.</p><h3><a href="#aa0d">PHARs in the wild</a></h3><p>As the new version of Box is being pushed forward, it is also good to help the existing projects and do a health check on their practice to help them to make it easier to build, optimize and maintain PHARs.</p><p>A list is provided in <a href="https://github.com/humbug/box/issues/19">#19</a> which includes:</p><ul><li><a href="https://github.com/humbug/box">Box itself</a></li><li><a href="https://github.com/composer/composer">Composer</a></li><li><a href="https://github.com/sebastianbergmann/phpunit">PHPUnit</a></li><li><a href="https://github.com/infection/infection">Infection</a></li><li><a href="https://github.com/bobthecow/psysh">Psysh</a></li><li>And much more!</li></ul><h3><a href="#e56b">Conclusion</a></h3><p>The tooling for building and managing PHARs is already here. <a href="https://github.com/humbug/php-scoper">PHP-Scoper</a> among others is a good example of how to use all the tools mentioned above. They work, it’s only missing a few features to make it easier to use and maintain.</p><p>This is a significant work nonetheless so I’m looking for help to speed things up. You can get started by any of the issues above, in the <a href="https://github.com/humbug/box/projects">GitHub board</a> or simply by pinging me to show your interest!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=870671a847c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create and deploy secure PHARs]]></title>
            <link>https://medium.com/@tfidry/create-and-deploy-secure-phars-c5572f10b4dd?source=rss-8db48ea4f3f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5572f10b4dd</guid>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[phar]]></category>
            <dc:creator><![CDATA[Théo Fidry]]></dc:creator>
            <pubDate>Fri, 29 Dec 2017 11:45:42 GMT</pubDate>
            <atom:updated>2018-05-31T12:58:30.871Z</atom:updated>
            <content:encoded><![CDATA[<p>For those who are not familiar with it, PHAR (Php Archive) is analogous to the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jarGuide.html">JAR file</a> concept but for PHP. It allows you to package an application into a single file making it convenient to deploy or distribute. This used to be very convenient for deploying an application over FTP as there is only a single file to replace. Luckily, we don’t have to do that (FTP deployment) anymore (if not, I’m sorry for you).</p><p>So what are PHARs useful for then? Well still the same thing: <strong>packaging applications</strong>. Although not many people may want to use this technique for web applications, it is still extremely useful for console applications.</p><h3><a href="#74b2">State of the art</a></h3><h4><a href="#7877">Installing a PHAR</a></h4><p>There is currently several ways:</p><ul><li>Use an installer, i.e. a script to install the PHAR</li><li>Download the PHAR directly</li><li>Use a PHAR manager like <a href="https://phar.io/">PHIVE</a></li></ul><h4><a href="#75da">Installation with an installer</a></h4><p>How the installer works depends from a tool to another as each installation script can differ. For example the current way to <a href="https://getcomposer.org/download/">download the Composer PHAR</a> is:</p><pre>$ php -r &quot;copy(&#39;https://getcomposer.org/installer&#39;, &#39;composer-setup.php&#39;);&quot;<br>$ php -r &quot;if (hash_file(&#39;SHA384&#39;, &#39;composer-setup.php&#39;) === &#39;544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061&#39;) { echo &#39;Installer verified&#39;; } else { echo &#39;Installer corrupt&#39;; unlink(&#39;composer-setup.php&#39;); } echo PHP_EOL;&quot;<br>$ php composer-setup.php<br>$ php -r &quot;unlink(&#39;composer-setup.php&#39;);&quot;</pre><p>Another example with the <a href="https://github.com/symfony/symfony-installer">Symfony Installer</a>:</p><pre>$ sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony<br>$ sudo chmod a+x /usr/local/bin/symfony</pre><h4><a href="#90cc">Installation by downloading the PHAR directly</a></h4><p>Some projects like <a href="https://phpunit.de/">PHPUnit</a> or <a href="http://psysh.org/">PsySH</a> opt for a simpler (although less secure) way which is downloading the PHAR directly:</p><pre>$ wget https://phar.phpunit.de/phpunit-6.5.phar<br>$ chmod +x phpunit-6.5.phar<br>$ sudo mv phpunit-6.5.phar /usr/local/bin/phpunit<br>$ phpunit --version</pre><h4><a href="#52f4">Installation with PHIVE</a></h4><p><a href="https://phar.io/">PHIVE</a> is a convenient tool to download and verify PHARs, once installed requiring a PHAR is as easy as:</p><pre>$ phive install phpunit</pre><p>Or if you didn’t register your project <a href="https://github.com/phar-io/phar.io/blob/master/data/repositories.xml">in their database</a> in which case you cannot use an alias like above, you can always install it directly via the GitHub URL:</p><pre>$ phive install phpDocumentor/phpDocumentor2</pre><h3><a href="#0e03">Building a PHAR</a></h3><p>PHP provides <a href="https://secure.php.net/phar">tons of functionalities around PHARs</a>. The issue is that the manual is not super detailed still and building a PHAR remains a cumbersome task. You can find examples of how to do it in <a href="https://github.com/composer/composer/blob/dc509b236fad2604d12b251c69884a9f3b3f4173/src/Composer/Compiler.php">Composer</a> and <a href="https://github.com/bobthecow/psysh/blob/master/src/Psy/Compiler.php">PsySH</a>. Luckily there is now the <a href="https://box-project.github.io/box2/">box project</a> which makes it far easier. All you have to do is create a configuration file box.json.dist which looks like this:</p><pre>{<br>    &quot;chmod&quot;: &quot;0755&quot;,<br>    &quot;main&quot;: &quot;bin/command.php&quot;,<br>    &quot;output&quot;: &quot;bin/command.phar&quot;,<br><br>    &quot;directories&quot;: [&quot;src&quot;],<br>    &quot;finder&quot;: [<br>        {<br>            &quot;name&quot;: &quot;*.php&quot;,<br>            &quot;exclude&quot;: [&quot;test&quot;, &quot;tests&quot;],<br>            &quot;in&quot;: &quot;vendor&quot;<br>         }<br>    ],</pre><pre>    &quot;stub&quot;: true<br>}</pre><p>And once you have created this file and installed box, you can build your PHAR with:</p><pre>$ box build</pre><p>You will now have the PHAR file bin/command.phar which you can execute either by calling it with php like any PHP file:</p><pre>$ php bin/command.phar</pre><p>Or make it executable and execute it directly:</p><pre>$ chmod u+x bin/command.phar # Make the PHAR executable<br>$ bin/command.phar           # Execute it</pre><p>Note that the previous way still works even if the file is executable. This is quite useful if you want to pass some options like disabling the garbage collection when you can afford it to speed things up:</p><pre>$ php -d zend.gc_enable=0 bin/command.phar</pre><p>As they are binary artefacts, it is good practice to not commit PHARs so you should add them to your project .gitignore:</p><pre>$ echo &quot;/bin/command.phar&quot; &gt;&gt; .gitignore</pre><p>It is also worth mentioning that there is also the PHAR CLI provided by the PHAR extension:</p><pre>$ man phar</pre><p>As well as other PHAR building tools such as <a href="https://github.com/theseer/Autoload">theseer/Autoload</a>.</p><h3><a href="#cf0a">Updating a PHAR</a></h3><p>There are two simple ways to update a PHAR:</p><ul><li>Using <a href="https://phar.io/">PHIVE</a></li><li>Using a self-update command</li></ul><h4><a href="#3aae">PHIVE</a></h4><p>You can use the phive update command to securely update a PHAR.</p><h4><a href="#908f">Self-update command</a></h4><p>There are <a href="https://packagist.org/?q=phar-updat&amp;p=0&amp;dFR%5Btags%5D%5B0%5D=phar&amp;dFR%5Btags%5D%5B1%5D=update">several projects</a> providing helpers for creating self-update commands. The most popular one is <a href="https://github.com/humbug/phar-updater">Padraìc’s phar-updater</a> project which has been moved under the Humbug umbrella. Here is an example with a <a href="https://symfony.com/doc/current/console.html">Symfony command</a>:</p><pre><strong>&lt;?php declare</strong>(strict_types=1);<br><br><strong>namespace </strong>Acme\PharDemo\Console\Command;<br><br><strong>use </strong>Humbug\SelfUpdate\Updater;<br><strong>use </strong>PHAR;<br><strong>use </strong>Symfony\Component\Console\Command\Command;<br><strong>use </strong>Symfony\Component\Console\Input\InputInterface;<br><strong>use </strong>Symfony\Component\Console\Output\OutputInterface;<br><strong>use </strong>Symfony\Component\Console\Style\SymfonyStyle;<br><br><strong>final class </strong>SelfUpdateCommand <strong>extends </strong>Command<br>{<br>    <strong>private </strong>$updater;<br><br>    <em>/**<br>     * </em><strong><em>@inheritdoc<br>     </em></strong><em>*/<br>    </em><strong>public function </strong>__construct(Updater $updater)<br>    {<br>        <strong>parent</strong>::<em>__construct</em>();<br><br>        // Use dependency injection instead, this is just to show <br>        // you how the updater is configured<br>        $this-&gt;updater = <strong>new </strong>Updater(&#39;bin/command.phar&#39;);<br>        $this-&gt;updater-&gt;setStrategy(Updater::<em>STRATEGY_GITHUB</em>);<br>        $this-&gt;updater-&gt;getStrategy()-&gt;setPackageName(&#39;acme/phar-demo&#39;);<br>        $this-&gt;updater-&gt;getStrategy()-&gt;setPharName(&#39;command.phar&#39;);<br>    }<br><br>    <em>/**<br>     * </em><strong><em>@inheritdoc<br>     </em></strong><em>*/<br>    </em><strong>protected function </strong>configure(): void<br>    {<br>        $this<br>            -&gt;setName(&#39;self-update&#39;)<br>            -&gt;setDescription(sprintf(<br>                &#39;Update %s to most recent stable build.&#39;,<br>                $this-&gt;getLocalPharName()<br>            ))<br>        ;<br>    }<br><br>    <em>/**<br>     * </em><strong><em>@inheritdoc<br>     </em></strong><em>*/<br>    </em><strong>protected function </strong>execute(InputInterface $input, OutputInterface $output): int<br>    {<br>        $io = <strong>new </strong>SymfonyStyle($input, $output);<br><br>        $result = $this-&gt;updater-&gt;update();<br><br>        <strong>if </strong>($result) {<br>            $io-&gt;success(<br>                sprintf(<br>                    &#39;Your PHAR has been updated from &quot;%s&quot; to &quot;%s&quot;.&#39;,<br>                    $this-&gt;updater-&gt;getOldVersion(),<br>                    $this-&gt;updater-&gt;getNewVersion()<br>                )<br>            );<br>        } <strong>else </strong>{<br>            $io-&gt;success(&#39;Your PHAR is already up to date.&#39;);<br>        }<br><br>        <strong>return </strong>0;<br>    }<br> <br>    <strong>private function </strong>getLocalPharName(): string<br>    {<br>        <strong>return </strong>basename(PHAR::<em>running</em>());<br>    }<br>}</pre><h3><a href="#6298">Signing a PHAR</a></h3><p>Signing a PHAR ensures that the PHAR you are using cannot be execute if its content has been changed. While some <a href="https://blog.sucuri.net/2017/07/code-injection-in-phar-signed-php-archives.html">vulnerability remains</a>, this is a solid approach for most usages.</p><p>The first step is to create an OpenSSL private key:</p><pre>$ openssl genrsa -des3 -out acme-phar-private.pem 4096</pre><p>The above will prompt you for a passphrase which is used to encrypt the key. To be able to automate the release process (see the next part), you will need to strip that key from any passphrase:</p><pre># Save the passphrased protected key<br>$ cp acme-phar-private.pem acme-phar-private.pem.passphrase-protected<br>$ openssl rsa -in acme-phar-private.pem -out acme-phar-private-nopassphrase.pem<br>$ cp acme-phar-private-nopassphrase.pem acme-phar-private.pem</pre><p>If you are not planning to automate the deployment process, just keep your private key somewhere and make sure to not commit it. Here, we will prepare it for the automated deployment and create a .travis/ subdirectory in your project with a .gitkeep file. Put the private key in it and then <strong>add that file to your project’s </strong><strong>.gitignore to avoid to push an unencrypted key to the repository</strong>:</p><pre>$ mkdir .travis<br>$ mv acme-phar-private.pem .travis/<br>$ echo &quot;.travis/phar-private.pem&quot; &gt;&gt; .gitignore</pre><p>Box also support PHAR signing, so we can tweak the box.json.dist configuration file to add it:</p><pre>{<br>    &quot;output&quot;: &quot;bin/command.phar&quot;,<br>    ...<br>    &quot;algorithm&quot;: &quot;OPENSSL&quot;,<br>    &quot;key&quot;: &quot;.travis/acme-phar-private.pem&quot;<br>}</pre><p>After building the PHAR with the box build command, you will now find two files:</p><ul><li>bin/command.phar which is the PHAR build as before</li><li>bin/command.phar.pubkey which is the public key derived from the private key</li></ul><p>Now $ bin/command.phar will fail whenever the public key is absent, wrong or when the PHAR has been tempered with.</p><p>Like the PHAR the public key should be ignored for git:</p><pre>$ echo &quot;/bin/command.phar&quot; &gt;&gt; .gitignore<br>$ echo &quot;/bin/command.phar.pubkey&quot; &gt;&gt; .gitignore</pre><p>Note that now you will need a private key even if you want to build the PHAR for development purposes. Do disable it for development, an easy solution for now is to create a box.json file in which we remove the signing part. You can also check. For <a href="https://github.com/humbug/php-scoper">PHP-Scoper</a> for example, we leverage Makefile for that:</p><pre>box.json: box.json.dist<br>   cat box.json.dist | sed -E &#39;s/\&quot;key\&quot;: \&quot;.+\&quot;,//g&#39; | sed -E &#39;s/\&quot;algorithm\&quot;: \&quot;.+\&quot;,//g&#39; &gt; box.json</pre><p>You can also <a href="https://andreas.heigl.org/2017/01/19/encrypt-a-build-result-automaticaly/">encrypt your PHAR with a GPG key</a> instead.</p><h3><a href="#c778">PHAR auto-release</a></h3><p>Automating the release process depends a lot of the Continuous Integration tool you are using. The following example will be for <a href="https://travis-ci.org/">Travis CI</a>. In this part, we’ll assume Travis is already enabled for our repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*hhQG6DX59IjMIhyPCmCT4w.png" /></figure><h4><a href="#777d">Configuring Travis</a></h4><p>You should add a .travis.yml file to your project, if you haven&#39;t already. Here&#39;s a basic template:</p><pre>language: php</pre><pre>cache:<br>  directories:<br>    - $HOME/.composer/cache</pre><pre>matrix:<br>  fast_finish: true<br>  include:<br>  - php: &#39;7.2&#39;<br>    env:<br>    - EXECUTE_DEPLOYMENT=true<br>  - php: master<br>  allow_failures:<br>  - php: master</pre><pre>before_install:<br>  - phpenv config-rm xdebug.ini || true </pre><pre>install:<br>- composer install --no-interaction --no-progress --no-suggest --prefer-dist</pre><pre>script:<br>- vendor/bin/phpunit    # Execute your tests<br>- vendor/bin/box build  # Build your PHAR<br># Execute some end-to-end tests with your PHAR if you have any</pre><pre>notifications:<br>  email: true</pre><p>Commit and push that file, and you should see your first build appear on Travis-CI.</p><h4><a href="#86d1">Adding encrypted files to Travis</a></h4><p>Travis-CI provides a number of facilities for encrypting secrets that you wish to utilize during the build process. In our case, we need to provide <a href="https://docs.travis-ci.com/user/encrypting-files">encrypted files</a>.</p><p>Interestingly, due to some issues with OpenSSL and the way the support is implemented in Travis-CI, you can <a href="https://github.com/travis-ci/travis.rb/issues/239">only encrypt a <em>single</em> file</a>. Thus, if you have multiple files, you need <a href="https://docs.travis-ci.com/user/encrypting-files#Encrypting-multiple-files">create an archive of them and encrypt that</a>.</p><pre>$ cd .travis<br>$ tar cvf secrets.tar *.pem<br>$ cd ..</pre><p>This will create the file .travis/secrets.tar.</p><p>Now, we need to encrypt the file. To do this, you will need to install the travis gem, login and encrypt them:</p><pre>$ gem install travis<br>$ travis login<br>$ travis encrypt-file .travis/secrets.tar \<br>  .travis/secrets.tar.enc --add</pre><p>This will create a new file .travis/secrets.tar.enc and add an entry to your.travis.yml&#39;s before_install section that will decrypt the file; this means that your code and scripts on Travis-CI can then rely on .travis/secrets.tar being available.</p><p>Note that when you use the -add flag and travis, it rewrites your .travis.yml file which might alter the spacings.</p><p>We’ll add the .travis/secrets.tar.enc file to the repository, and omit.travis/secrets.tar:</p><pre>$ git add .travis/secrets.tar.enc<br>$ echo &quot;.travis/secrets.tar&quot; &gt;&gt; .gitignore</pre><p>When a build is triggered on Travis-CI now, it will decrypt this file before any of our build processes are triggered, allowing us access to those secrets!</p><p>Your .travis.yml file should now look similar to this:</p><pre>language: php</pre><pre>cache:<br>  directories:<br>    - $HOME/.composer/cache</pre><pre>matrix:<br>  fast_finish: true<br>  include:<br>  - php: &#39;7.2&#39;<br>    env:<br>    - EXECUTE_DEPLOYMENT=true<br>  - php: master<br>  allow_failures:<br>  - php: master</pre><pre>before_install:<br>  - phpenv config-rm xdebug.ini || true<br>  # The part added by Travis:<br>  - openssl aes-256-cbc -K $encrypted_smth_key -iv $encrypted_smth_iv -in .travis/secrets.tar.enc -out .travis/secrets.tar -d<br>  - tar xvf .travis/secrets.tar -C .travis</pre><pre>install:<br>- composer install --no-interaction --no-progress --no-suggest --prefer-dist</pre><pre>script:<br>- vendor/bin/phpunit    # Execute your tests<br>- vendor/bin/box build  # Build your PHAR<br># Execute some end-to-end tests with your PHAR if you have any</pre><pre>notifications:<br>  email: true</pre><p>A small issue remains here: forks. Indeed when someone will for your repository and open a pull request, Travis will use <em>their</em> fork. As the encrypted data is bound to the main repository, the line openssl aes-256-cbc [...] will fail. To avoid those kind of failures on forks, a solution is to disable this one for pull requests:</p><pre>- |<br>  if [ &#39;false&#39; == &quot;$TRAVIS_PULL_REQUEST&quot; ]; then<br>    openssl aes-256-cbc -K [...]<br>    tar xvf .travis/secrets.tar -C .travis<br>  fi;</pre><p>Note however that the above will <em>also block your own fork</em>. If you want to be more prevent that, you can go a step further by giving your repository slug:</p><pre>- |<br>  if [ &#39;acme/foo&#39; == &quot;$TRAVIS_PULL_REQUEST_SLUG&quot; ]; then<br>    openssl aes-256-cbc -K [...]<br>    tar xvf .travis/secrets.tar -C .travis<br>  fi;</pre><h4><a href="#cb24">Add the deployment config</a></h4><p>Now that we have our secrets securely available on Travis-CI, we can configure the deployment entry of our .travis.yml file to <a href="https://docs.travis-ci.com/user/deployment/releases/">publish our signed PHAR with GitHub releases</a>.</p><p>As <a href="https://docs.travis-ci.com/user/deployment/releases/#Authenticating-with-an-OAuth-token">per the doc</a>, you can use the travis setup releases command. This will create a deploy entry in your .travis.yml that you can then tweak to your needs:</p><pre><strong>deploy:<br>  provider: </strong>releases<br>  <strong>api_key:<br>    secure: </strong>YOUR_API_KEY_ENCRYPTED<br>  <strong>file:<br>    </strong>- bin/command.phar<br>    - bin/command.phar.pubkey<br>  <strong>skip_cleanup: </strong>true<br>  <strong>on:<br>    tags: </strong>true<br>    <strong>repo: </strong>acme/phar-demo<br>    <strong>condition: </strong>&quot;$EXECUTE_DEPLOYMENT&quot;</pre><p>Et voilà!</p><p>You can also find an alternative way in <a href="https://andreas.heigl.org/2017/01/19/encrypt-a-build-result-automaticaly/">Andreas Heigl’s article</a> by leveraging encrypted environment variables instead.</p><p>If you wish to know more about PHARs and the tooling around them I recommend you to check this article <a href="https://medium.com/@tfidry/phars-roadmap-870671a847c1">PHARs Roadmap</a>.</p><h3><a href="#92e7">Credits</a></h3><p>A big thanks to:</p><ul><li><a href="https://mwop.net/">Matthew Weier O’Phinney</a> for his <a href="https://mwop.net/blog/2015-12-14-secure-phar-automation.html">amazing article</a> on which most of the signing and deployment section are inspired</li><li><a href="https://andreas.heigl.org/">Andreas Heigl</a> for his <a href="https://andreas.heigl.org/2017/01/19/encrypt-a-build-result-automaticaly/">article</a> on signing a PHAR with a GPG key</li></ul><p>Edits as per <a href="https://medium.com/@arne.blankerts?source=post_header_lockup">Arne Blankerts</a> comment:</p><ul><li>Corrected some instructions regarding the usage of Phive</li><li>Mentioned other PHAR building tools</li><li>Added link to the PHAR roadmap</li></ul><p>Further edits:</p><ul><li>Added a mention on how to avoid failures on forks due to the encrypted data</li></ul><p>Edit as per <a href="https://github.com/jakzal/phpqa/pull/57">Jakub Zalas</a> comment: add a mention regarding the configuration of the pull request to not prevent the pull request of your own repository.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5572f10b4dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing your dependencies in PHP]]></title>
            <link>https://medium.com/@tfidry/managing-your-dependencies-in-php-321d584441ab?source=rss-8db48ea4f3f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/321d584441ab</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[composer]]></category>
            <category><![CDATA[dependency-management]]></category>
            <dc:creator><![CDATA[Théo Fidry]]></dc:creator>
            <pubDate>Sat, 02 Dec 2017 13:24:34 GMT</pubDate>
            <atom:updated>2018-01-05T08:43:47.409Z</atom:updated>
            <content:encoded><![CDATA[<p>When you are creating a PHP application or library, you usually have 3 kinds of dependencies:</p><ul><li>Hard dependencies: what your application/library needs to be able to run</li><li>Optional dependencies: for example a PHP library can provide a bridge for different frameworks</li><li>Development dependencies: debugging tools, test frameworks…</li></ul><h3><a href="#dc7d">How to manage those dependencies?</a></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*hQ6fh7VWqkfaPmXR8N4CoQ.png" /><figcaption>source: <a href="https://getcomposer.org/">https://getcomposer.org/</a></figcaption></figure><p>Hard dependencies:</p><pre>{<br>    &quot;require&quot;: {<br>        &quot;acme/foo&quot;: &quot;^1.0&quot;<br>    }<br>}</pre><p>Optional dependencies:</p><pre>{<br>    &quot;suggest&quot;: {<br>        &quot;monolog/monolog&quot;: &quot;Advanced logging library&quot;,<br>        &quot;ext-xml&quot;: &quot;Required to support XML&quot;<br>    }<br>}</pre><p>Optional &amp; Development dependencies:</p><pre>{<br>    &quot;require-dev&quot;: {<br>      &quot;monolog/monolog&quot;: &quot;^1.0&quot;,<br>      &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;<br>    }<br>}</pre><p>So far so good. So what can go wrong? Basically require-dev which has some limitations.</p><h3><a href="#6f8e">Issues &amp; limitations</a></h3><h4><a href="#9360">Too many dependencies</a></h4><p>Dependencies with a package manager are <em>great.</em> It’s a fantastic mechanism to re-use existing code and being able to update it easily. You should however be responsible about which and how many dependencies you are including. You are still including code and as such it can be buggy or insecure. You are becoming depend on something someone else has written and on which you may have no control over besides becoming subject to third-party problems. <a href="https://packagist.org/">Packagist</a> and <a href="https://github.com/">GitHub</a> are among others doing a fantastic job at reducing some of those risks, but the risk still exists nonetheless. What happen with the <a href="http://blog.npmjs.org/post/141577284765/kik-left-pad-and-npm">left-pad fiasco</a> in the JavaScript community is a good example that things <em>can</em> go wrong and adding a package is not without consequences.</p><p>A second issue with dependencies is that they need to be compatible. That’s <a href="https://getcomposer.org/">Composer</a> job. But as great as <a href="https://getcomposer.org/">Composer</a> is, there is some dependencies you cannot install together and the more dependencies you add the more likely you are to encounter a conflict.</p><p><strong>TL:DR;</strong> Be responsible about which dependencies you are including and strive for few dependencies.</p><h4><a href="#8c0a">Hard conflict</a></h4><p>Let’s consider the following example:</p><pre>{<br>    &quot;require-dev&quot;: {<br>        &quot;phpstan/phpstan&quot;: &quot;^1.0@dev&quot;,<br>        &quot;phpmetrics/phpmetrics&quot;: &quot;^2.0@dev&quot;<br>    }<br>}</pre><p>Those two packages are<a href="http://istqbexamcertification.com/what-is-static-analysis-tools-in-software-testing/"> static analysis tools</a> and they might not be installable together, i.e. create a conflict because they might depend on different and incompatible versions of <a href="https://github.com/nikic/PHP-Parser">PHP-Parser</a>.</p><p>This is a case of “silly<em>”</em> conflict: a conflict should occur only if you are trying to include a dependency which is incompatible with your application. Those two packages don’t need to be compatible, your application are not using them directly and they are not executing your code application either.</p><p>Another example in the case of a library for which you provide a <a href="https://symfony.com/">Symfony</a> and <a href="https://laravel.com/">Laravel</a> bridge. You may want to include both Symfony and Laravel as dependencies to test those bridges:</p><pre>{<br>    &quot;require-dev&quot;: {<br>        &quot;symfony/framework-bundle&quot;: &quot;^4.0&quot;,<br>        &quot;laravel/framework&quot;: &quot;~5.5.0&quot; # gentle reminder that Laravel<br>                                      # packages are not semver<br>    }<br>}</pre><p>This might work fine in some cases but very likely break most of the time. This is a bit silly here as well as you are <em>very</em> unlikely to have a user requiring both of those packages at the same time and you are even more less likely to want to support this scenario.</p><h4><a href="#66ff">Untestable dependencies</a></h4><p>If you look at the following composer.json:</p><pre>{<br>    &quot;require&quot;: {<br>        &quot;symfony/yaml&quot;: &quot;^2.8 || ^3.0&quot;<br>    },<br>    &quot;require-dev&quot;: {<br>        &quot;symfony/yaml&quot;: &quot;^3.0&quot;<br>    }<br>}</pre><p>There is something happening there… The only installable versions of the Symfony YAML component (the symfony/yaml package will be [3.0.0, 4.0.0[.</p><p>In an application, you most likely do not care. In the case of a library however this might be an issue. Indeed this means you will<em> never </em>be able to test your library for symfony/yaml [2.8.0, 3.0.0[.</p><p>Whether or not this is indeed an issue depends a lot on your case. The thing here is to be aware that this can happen and that there is no trivial way to identify it. The case above is simple, but if that symfony/yaml: ^3.0 is a requirement is hidden deeper in the dependency tree, for example:</p><pre>{<br>    &quot;require&quot;: {<br>        &quot;symfony/yaml&quot;: &quot;^2.8 || ^3.0&quot;<br>    },<br>    &quot;require-dev&quot;: {<br>        &quot;acme/foo&quot;: &quot;^1.0&quot;  # requires symfony/yaml ^3.0<br>    }<br>}</pre><p>You will have no way, for now at least, to know about it.</p><h3><a href="#0d2e">Solutions</a></h3><h4><a href="#57d7">Do not use the package</a></h4><p>KISS. It’s ok, you don’t really need that package after all!</p><h4><a href="#8dea">PHARs</a></h4><p>PHARs (PHP Archives) are a way to package an application into a single file. If you want to know more about it I recommend you <a href="https://secure.php.net/manual/en/intro.phar.php">the official PHP documentation for it</a>.</p><p>Example of usage with <a href="http://www.phpmetrics.org/">PhpMetrics</a>, a static analysis tool:</p><pre>$ wget <a href="https://url/to/download/phpmetrics/phar-file">https://url/to/download/phpmetrics/phar-file</a> -o phpmetrics.phar</pre><pre>$ chmod +x phpmetrics.phar<br>$ mv phpmetrics.phar /usr/local/bin/phpmetrics<br>$ phpmetrics --version<br>PhpMetrics, version 1.9.0</pre><pre># or if you want to keep the PHAR close and do not mind the .phar<br># extension:</pre><pre>$ phpmetrics.phar --version<br>PhpMetrics, version 1.9.0</pre><p><strong>Warning</strong>: bundling code into a PHAR does not isolate it unlike, say, JARs in Java. That said, <a href="https://medium.com/@tfidry/phars-roadmap-870671a847c1#3278">there is PHP-Scoper which is being developed</a> to tackle that issue.</p><p>Let’s illustrate that issue with an example. You built a console application myapp.phar which relies on Symfony YAML 2.8.0 which execute a given PHP script:</p><pre>$ myapp.phar myscript.php</pre><p>Your script myscript.php is leveraging Composer to use Symfony YAML 4.0.0.</p><p>What may happen is the PHAR loads a Symfony YAML class, e.g. Symfony\Yaml\Yaml and then execute your script. Your script also uses Symfony\Yaml\Yaml, but guess what, this class is already loaded! The issue there is that the one loaded is from the symfony/yaml 2.8.0 package, not the 4.0.0 that your script needs. As a result, if the API differs, this is gonna break hard.</p><p><strong>TL:DR;</strong> PHARs are great for static analysis tools like <a href="https://github.com/phpstan/phpstan">PhpStan</a> or <a href="http://www.phpmetrics.org/">PhpMetrics</a> but are unreliable (for now at least) as soon as they execute code because of those dependency collisions (for now least!).</p><p>There is also a few other things to keep in mind when using PHARs:</p><ul><li>They are harder to track as there is no native support for them in Composer. There is however a few solutions like the Composer plugin <a href="https://github.com/tommy-muehle/tooly-composer-script">tooly-composer-script</a> or <a href="https://phar.io/">PhiVe</a>, a PHAR installer.</li><li>How the versions are managed depends a lot on the project. Some projects offers a self-update command à la Composer with different channels of stability, some provide a unique download endpoint with the latest release, some make use of the GitHub release feature and ship a PHAR for each release, etc.</li></ul><h4><a href="#8635">Using multiple repositories</a></h4><p>By far one of the most popular technique. So instead of requiring all the bridges dependencies in a single composer.json, we break down the package in multiple repositories.</p><p>If we take the previous example of the library which we’ll call acme/foo, then we will create another package acme/foo-bundle for Symfony and acme/foo-provider for Laravel.</p><p>Note that everything can actually still be in a single repository and have read-only repositories for the other packages like Symfony is doing.</p><p>The main advantage of that approach is that it remains relatively simple and do not require any extra tooling except ventually a repository splitter like <a href="https://github.com/splitsh/lite">splitsh</a> which is used for example for Symfony, Laravel and PhpBB. The drawback is that you now have multiple packages to maintain instead of one.</p><h4><a href="#8af3">Tweak the configuration</a></h4><p>Another way would be to have a more advanced installation &amp; testing script. For the previous example, we could have something along the lines of:</p><pre>#!/usr/bin/env bash<br># bin/tests.sh</pre><pre># Test the core library<br>vendor/bin/phpunit --exclude-group=laravel,symfony</pre><pre># Test the Symfony bridge<br>composer require symfony/framework-bundle:^4.0<br>vendor/bin/phpunit --group=symfony<br>composer remove symfony/framework-bundle</pre><pre># Test the Laravel bridge<br>composer require laravel/framework:~5.5.0<br>vendor/bin/phpunit --group=symfony<br>composer remove laravel/framework</pre><p>It works, but in my experience at least, this leads to bloated test scripts which are relatively slow, hard to maintain and not being very friendly for new contributors.</p><h4><a href="#e1ac">Use multiple composer.json</a></h4><p>This approach is relatively new (in PHP) mostly because the required tooling was not there, so I’ll expand a bit more on that solution.</p><p>The idea is relatively simple. Instead of having:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;,<br>        &quot;phpstan/phpstan&quot;: &quot;^1.0@dev&quot;,<br>        &quot;phpmetrics/phpmetrics&quot;: &quot;^2.0@dev&quot;<br>    }<br>}</pre><p>We will install phpstan/phpstan and phpmetrics/phpmetrics in different composer.json files. But here comes the first issue: where do we put them? Which structure to adopt?</p><p>This is where <a href="https://github.com/bamarni/composer-bin-plugin">composer-bin-plugin</a> comes in. It’s a very simple Composer plugin that allows you to interact with a composer.json based in a different directory. So lets say we have our root composer.json file:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;<br>    }<br>}</pre><p>We can install the plugin:</p><pre>$ composer require --dev bamarni/composer-bin-plugin</pre><p>Now that the plugin is installed, whenever you do composer bin acme smth, it will execute the command composer smth in the sub-directory vendor-bin/acme. So we can now install PhpStan and PhpMetrics like so:</p><pre>$ composer bin phpstan require phpstan/phpstan:^1.0@dev<br>$ composer bin phpmetrics require phpmetrics/phpmetrics:^2.0@dev</pre><p>This will create following directory structure:</p><pre>... # projects files/directories<br>composer.json<br>composer.lock<br>vendor/<br>vendor-bin/<br>    phpstan/<br>        composer.json<br>        composer.lock<br>        vendor/<br>    phpmetrics/<br>        composer.json<br>        composer.lock<br>        vendor/</pre><p>Where vendor-bin/phpstan/composer.json looks like this:</p><pre>{<br>    &quot;require&quot;: {<br>        &quot;phpstan/phpstan&quot;: &quot;^1.0&quot;<br>    }<br>}</pre><p>And vendor-bin/phpmetrics/composer.json looks like this:</p><pre>{<br>    &quot;require&quot;: {<br>        &quot;phpmetrics/phpmetrics&quot;: &quot;^2.0&quot;<br>    }<br>}</pre><p>So now we can easily use PhpStan and PhpMetrics by calling vendor-bin/phpstan/vendor/bin/phpstan and vendor-bin/phpmetrics/vendor/bin/phpstan.</p><p>Let’s now go further and take the example of a library with bridges for different frameworks:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;,<br>        &quot;symfony/framework-bundle&quot;: &quot;^4.0&quot;,<br>        &quot;laravel/framework&quot;: &quot;~5.5.0&quot;<br>    }<br>}</pre><p>So we apply the same approach and we end up with avendor-bin/symfony/composer.json file for the Symfony bridge:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;,<br>        &quot;symfony/framework-bundle&quot;: &quot;^4.0&quot;<br>    }<br>}</pre><p>And another vendor-bin/laravel/composer.json for the Laravel bridge:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;,<br>        &quot;laravel/framework&quot;: &quot;~5.5.0&quot;<br>    }<br>} </pre><p>Our root composer.json would now look like this:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;bamarni/composer-bin-plugin&quot;: &quot;^1.0&quot;<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;<br>    }<br>}</pre><p>To test the core library and the bridges you would now create 3 different PHPUnit files, one for each with the proper autoload file (e.g. vendor-bin/symfony/vendor/autoload.php for the Symfony bridge).</p><p>If you actually try this, you will notice a major flaw in this approach: the redundant configuration. Indeed you would need to duplicate the config of the root composer.json into the other two vendor-bin/{symfony,laravel/composer.json, adjust the autoload sections as the path to the files would have changed and whenever you require a new dependency, you would need to require it in the other composer.json files as well. This is not manageable, this is where <a href="https://github.com/theofidry/composer-inheritance-plugin">composer-inheritance-plugin</a> comes in.</p><p>This plugin is tiny wrapper around <a href="https://github.com/wikimedia/composer-merge-plugin">composer-merge-plugin</a> to merge the content of vendor-bin/symfony/composer.json with the root composer.json. So instead of having:</p><pre>{<br>    &quot;autoload&quot;: {...},<br>    &quot;autoload-dev&quot;: {...},</pre><pre>    &quot;require&quot;: {...},<br>    &quot;require-dev&quot;: {<br>        &quot;phpunit/phpunit&quot;: &quot;^6.0&quot;,<br>        &quot;symfony/framework-bundle&quot;: &quot;^4.0&quot;<br>    }<br>}</pre><p>You will now have:</p><pre>{<br>    &quot;require-dev&quot;: {<br>        &quot;symfony/framework-bundle&quot;: &quot;^4.0&quot;,<br>        &quot;theofidry/composer-inheritance-plugin&quot;: &quot;^1.0&quot;<br>    }<br>}</pre><p>The rest of the config, autoload and dependencies of the root composer.json will be included. There is nothing to configure, <a href="https://github.com/theofidry/composer-inheritance-plugin">composer-inheritance-plugin</a> is just a thin wrapper around <a href="https://github.com/wikimedia/composer-merge-plugin">composer-merge-plugin</a> to preconfigure everything for this usage with <a href="https://github.com/bamarni/composer-bin-plugin">composer-bin-plugin</a>.</p><p>If you want to, you could inspect the dependencies installed for it with:</p><pre>$ composer bin symfony show</pre><p>I have been using this approach in several projects like <a href="https://github.com/nelmio/alice">alice</a> for different tools like PhpStan or PHP-CS-Fixer and framework bridges. Another example would be <a href="https://github.com/theofidry/AliceDataFixtures">alice-data-fixtures </a>for which there is a lot of different ORM bridges for the persistence layer (Doctrine ORM, Doctrine ODM, Eloquent ORM, etc.) and framework integrations.</p><p>I also use it in multiple private projects for applications for different tools as an alternative to PHARs and it has been working very well.</p><h3><a href="#7e06">Conclusion</a></h3><p>I am sure some will find some approaches weird or not recommend to use them. The goal here is not to judge or recommend one in peculiar, but rather lay out a list of possible ways to manage some dependencies and what are the benefits and drawbacks of each. So depending of the issue you have and your personal preferences, pick the one that works the best for you. As one said, there are no solutions, only tradeoffs.</p><p>Edit: Added a link to PHP-Scoper roadmap when mentioning the lack of isolation of the code bundled in the PHARs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=321d584441ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>