Alexandre Debrilhttps://debril.ovh2017-08-22T00:00:00+02:00feed-io 4.0-beta1 is out<p>With PHP 5 reaching its end of life at the end of 2018, it's time for modern libraries to move on and take advantage of PHP 7 features such as return types and scalar type declarations. So I did it to feed-io in the next release to come, tagged 4.0 and in beta phase until mid-September followed by a two weeks RC phase and then a final release in early October.</p> <p>What will change in feed-io 4.0 :</p> <ul> <li>PHP 5 will no longer be supported in this version</li> <li>Return and argument types are declared in function declarations</li> <li>Some bugs due to past versions' design are fixed</li> <li>Memory usage optimization</li> </ul> <p>Users of feed-io 3 will have to adapt all classes implementing interfaces like FeedInterface to add the type hints in functions' declarations. By the way : feed-io 3 will be supported until the end of 2018 so don't panic if you didn't plan to upgrade soon.</p> <p><strong>I am looking for testers and feedback will be highly appreciated</strong>. You can submit your issues in the <a href="">project's Github repository</a>, I will give you an answer as soon as possible.</p>feed-io 3 : JSON Feed support<p>In <a href="">my previous post</a> I introduced JSON Feed, a new syndication standard published on May 17, 2017.</p> <p>And now feed-io supports this new format since the last release. It was tagged 3.0 as it breaks compatibility with the previous version, which was a necessary evil because FeedIo::format() returned a DomDocument, no longer relevant. Now this method returns a string you can display without calling saveXML() anymore. This is the main change from 2.x to 3.0 and if you need to know everything on how to upgrade your code, please refer to the <a href="">upgrade guide</a>.</p> <p>For those how don't know feed-io yet, it is a PHP library built to read and write feeds initially written with RSS and Atom formats in mind. I sometimes had the feeling that one day a JSON standard will come this way, but I always ended thinking &quot;nay, not that year&quot;. Good news : I'm finally wrong ! And I guess nobody cares about what I am thinking, so now I'll get to the point : the code.</p>A new syndication standard : JSON Feed<p>With Atom and RSS being the major syndication formats since more than a decade, there's not much room for another one. In fact, trying to introduce a new one in 2017 is a real challenge as those XML-based formats are used by billions of websites and known by every web developers.</p> <p>But maybe that's the problem : two XML-based formats. Working with both of them is tricky, as there both share the same goal using similar schemas on top of a complex markup language. Look at <a href="">feed-io's \Rule namespace</a>, you may see by yourself that sometimes I had some headaches writing this library.</p> <p>So, in order to offer an easier way to serve feeds, two web veterans (Brent Simmons and Manton Reece) proposed a <a href="">new standard based on JSON</a> with the following motivations :</p> <blockquote> <p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p> <p>So we developed JSON Feed, a format similar to RSS and Atom but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p> </blockquote> <p>Here's the example provided in JSON Feed's specifications :</p> <pre class="prettyprint"><code class="language-json">{ "version": "", "title": "My Example Feed", "home_page_url": "", "feed_url": "", "items": [ { "id": "2", "content_text": "This is a second item.", "url": "" }, { "id": "1", "content_html": "&lt;p&gt;Hello, world!&lt;/p&gt;", "url": "" } ] }</code></pre> <p>Really easy to read. So easy I decided to implement this standard in feed-io.</p>feed-io v2.4 : Json serialisation<p>In version 2.4, feed-io introduces the ability to convert feeds in valid JSON string :</p> <pre class="prettyprint"><code class="language-php"> $feedIo = \FeedIo\Factory::create()-&gt;getFeedIo(); $feed = $feedIo-&gt;read('')-&gt;getFeed(); echo json_encode($feed); </code></pre> <p>The whole feed is exported in the JSON string, including medias, categories and additional elements.</p>FeedIoBundle was the new rss-atom-bundle<p>Two years ago, I announced the future replacement of rss-atom-bundle in favor of FeedIoBundle. The goal was to get a cleaner code based on feed-io and new features like an user interface to handle feeds, without causing any trouble for rss-atom-bundle's users. The result was not what I expected : forms made FeedIoBundle much harder to maintain than rss-atom-bundle and almost nobody adopted FeedIoBundle. Besides, rss-atom-bundle's community is still growing, with some of these people opening new issues and even sometimes submitting pull requests. As a consequence, I drop FeedIoBundle in order to focus on rss-atom-bundle and feed-io.</p> <p>This isn't a problem for me to take this kind of decision. I tried something that didn't work and now it's just time to put an end to it. No matter how much time I spent on programming FeedIoBundle, it was worth the effort as I grabbed some knowledge thanks to this project.</p> <p><del>So now the package will be tagged as &quot;abandoned&quot; on Packagist and all users are encouraged to use feed-io or rss-atom-bundle.</del></p> <p><em>Edit : I managed to remove FeedIoBundle from Packagist, you can't require it in your project anymore.</em></p>PHP and design patterns : a smart way ?<p>Thanks to Jon Lemaitre's excellent article <a href="">&quot;A Response to PHP the wrong way&quot;</a>, I recently discovered <a href=""><a href=""></a></a> self-exposed as the counterbalance of <a href=""><a href=""></a></a>. Then came the need to write an answer on the subject, my views on framework, OOP, standards and the rest but I realized I would end up with a very long article full of obvious arguments or at least statements many people already know. Boring. Seriously who wants to read another pros and cons on frameworks and OOP ? Is it really worth discussing it nowadays, knowing how many classes are offered by PHP itself and how its own syntax is object oriented ? I just think that if you don't want to write OOP code with PHP, you're free to use another language or to fork PHP 3.</p> <p>The only subject I will cover in this post is the usage of design patterns. It could have been the wisest part of &quot;PHP the wrong way&quot; but the author failed to provide relevant arguments by focusing on a philosophical point of view. My goal here is to bring a more objective point of view with technical facts.</p> <blockquote> <p>The wrong way: Looking for a pattern to solve a problem</p> </blockquote> <p>True, this is a dogmatic approach we must stay away from. Patterns were designed to implement common needs like separation of responsibilities, open/close principle and to save some precious time finding design solutions. However, it has some drawbacks and the use of design patterns requires skills and common sense.</p>Feed-io v2.3 : the CLI is here<p>From now on, feed-i is a little bit more than a library. Since version 2.3, it comes with a basic client you can use through your terminal :</p> <p>[code language=&quot;bash&quot;] ./bin/feedio read <a href=""></a> -c 1 [/code]</p> <p>The instruction above will fetch's feed and display its latest item. I used the &quot;count&quot; option to get only one, but this is not mandatory; without it, feed-io displays all the items included in the feed.</p> <p>The command line interface will probably be the main subject of the next few releases. I'm thinking about features like JSON output or items ordering and if you have any suggestion, feel free to <a href="">submit a new issue</a> on Github.</p>How to fix Composer's slowness<p>You may have notice that Composer can get really, reaally slow on some setups. This is the case at my office, so I tried to find a solution. Let's run Composer in very verbose mode :</p> <pre class="prettyprint"><code class="language-bash">$ composer -vvv require phpunit/phpunit Reading ./composer.json Loading config file ./composer.json Checking CA file /etc/ssl/certs/ca-certificates.crt Downloading Writing /root/.composer/cache/repo/ into cache Downloading</code></pre> <p>And it seems to get stuck here. In fact, it's not. If you wait for a little while (let's say, 2 or 3 minutes), Composer stores the file in cache and downloads the next provider-*.json file. Which takes 3 more minutes and Composer won't finish its downloads soon because, it will download more than 10 files after that.</p> <p>But Google is great and it gave me the solution :</p> <pre class="prettyprint"><code class="language-bash">$ composer config --global repo.packagist composer</code></pre> <p>And now it runs fast. Just because sometimes, Packagist is slow through HTTP without SSL.</p> <p>Please note : PHP must support SSL for this to work.</p>A successful upgrade from Symfony 2 to 3<p>As Feed-io-bundle is now Symfony 3 compliant, the next logical move was to upgrade its demo. The application is just composed of the bundle itself and Bootstrap so the upgrade process should be easy to follow. Let's see what's in the <a href="">documentation</a> :</p> <p>There are a couple of steps to upgrading a major version:</p> <blockquote> <pre class="prettyprint"><code> Make your code deprecation free Update to the new major version via Composer Update your code to work with the new version</code></pre> </blockquote> <p>Three steps and every thing seems cristal clear, here we go.</p> <h2>Replace deprecated code and configuration</h2> <p>According to the cookbook, you'll have to replace everything that became deprecated during Symfony 2's evolution. To go through the first step, Symfony provides a package called symfony/phpunit-bridge which detects deprecated parts of your code and shows you the way to correct it. Another way to go is to pay attention to deprecations Symfony notices to your application's log, which means to manually test the whole application. Good luck with that. feedio-demo has unit tests (well in fact, one), I'll use the bridge :</p> <p><img src="" alt="phpunit" /></p> <p>OK, that's easy. Only one <a href="">configuration to fix</a> and feedio-demo is ready for the dependencies upgrade.</p>A factory in feed-io v2.2<p>In feed-io 2.2, I introduced a Factory class built to get a FeedIo instance in one single line of code :</p> <pre class="prettyprint"><code class="language-php">$feedIo = \FeedIo\Factory::create()-&gt;getFeedIo();</code></pre> <p>Obviously this works assuming that you installed feed-io using Composer and you have included vendor/autoload.php before.</p> <h2>Factory's main method : Factory::create()</h2> <p>The factory comes with the ability to configure feed-io before getting its main class. For that, you will provide one or two configuration arrays depending on which dependency you want to configure.</p>