Skip to content

Commit

Permalink
Merge pull request #25 from alexdebril/feature/factory
Browse files Browse the repository at this point in the history
Factory class
  • Loading branch information
alexdebril committed Jan 22, 2016
2 parents 971f8cf + dabebe4 commit 178156a
Show file tree
Hide file tree
Showing 16 changed files with 816 additions and 114 deletions.
63 changes: 48 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,42 @@ Keep informed about new releases and incoming features : http://debril.org/categ

Use Composer to add feed-io into your project's requirements :

```sh
composer require debril/feed-io

```

# Requirements

feed-io requires :

- php 5.5+
- psr/log 1.0
- guzzlehttp/guzzle 6.0+

it suggests :
- guzzlehttp/guzzle 6.0+
- monolog/monolog 1.10+

Monolog is not the only library suitable to handle feed-io's logs, you can use any PSR/Log compliant library instead.

# Fetching the repository

Do this if you want to contribute (and you're welcome to do so):

git clone https://github.com/alexdebril/feed-gio.git
```sh
git clone https://github.com/alexdebril/feed-io.git

cd feed-io/

composer.phar install
composer install
```

# Unit Testing

You can run the unit test suites using the following command in the library's source directory:

```sh

make test
make test

```

Expand All @@ -67,16 +75,8 @@ feed-io is designed to read feeds across the internet and to publish your own. I

```php

// first dependency : the HTTP client
// here we use Guzzle as a dependency for the client
$guzzle = new GuzzleHttp\Client();
$client = new FeedIo\Adapter\Guzzle\Client($guzzle);

// second dependency : a PSR-3 logger
$logger = new Psr\Log\NullLogger();

// now create FeedIo's instance
$feedIo = new FeedIo\FeedIo($client, $logger);
// create a simple FeedIo instance
$feedIo = \FeedIo\Factory::create()->getFeedIo();

// read a feed
$result = $feedIo->read($url);
Expand Down Expand Up @@ -130,5 +130,38 @@ $item->addMedia($media);
$feed->add($item);

```
## activate logging

feed-io natively supports PSR-3 logging, you can activate it by choosing a 'builder' in the factory :

```php

$feedIo = \FeedIo\Factory::create(['builder' => 'monolog'])->getFeedIo();

```

feed-io only provides a builder to create Monolog\Logger instances. You can write your own, as long as the Builder implements BuilderInterface.

## Building a FeedIo instance without the factory

To create a new FeedIo instance you only need to inject two dependencies :

- an HTTP Client implementing FeedIo\Adapter\ClientInterface. It can be wrapper for an external library like FeedIo\Adapter\Guzzle\Client
- a PSR-3 logger implementing Psr\Log\LoggerInterface

```php

// first dependency : the HTTP client
// here we use Guzzle as a dependency for the client
$guzzle = new GuzzleHttp\Client();
// Guzzle is wrapped in this adapter which is a FeedIo\Adapter\ClientInterface implementation
$client = new FeedIo\Adapter\Guzzle\Client($guzzle);

// second dependency : a PSR-3 logger
$logger = new Psr\Log\NullLogger();

// now create FeedIo's instance
$feedIo = new FeedIo\FeedIo($client, $logger);

```

5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
}
],
"require": {
"php": ">=5.5.0",
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0",
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.1.0",
"guzzlehttp/guzzle": "~6.0",
"monolog/monolog": "1.10.*@dev"
},
"suggest": {
"guzzlehttp/guzzle": "HTTP communication",
"monolog/monolog": "Allows to handle logs"
},
"autoload": {
Expand Down
192 changes: 96 additions & 96 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions examples/factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the feed-io package.
*
* (c) Alexandre Debril <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require __DIR__.DIRECTORY_SEPARATOR.'bootstrap.php';

$feedIo = \FeedIo\Factory::create()->getFeedIo();

$result = $feedIo->read('http://php.net/feed.atom');

echo "feed title : {$result->getFeed()->getTitle()} \n ";

foreach ($result->getFeed() as $item) {
echo "item title : {$item->getTitle()} \n ";

// let's turn php.net into a PodCast
$media = new \FeedIo\Feed\Item\Media();
$media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3');
$media->setType('audio/mpeg');

// add it to the item
$item->addMedia($media);
}

$domDocument = $feedIo->toAtom($result->getFeed());
echo $domDocument->saveXML();
Loading

0 comments on commit 178156a

Please sign in to comment.