Skip to content

Commit

Permalink
README: Add documenation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Barášek committed Jun 28, 2021
1 parent 2d82452 commit cf0d681
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Wordpress post feed

API service for downloading a list of new blog posts.

The library downloads the feed of posts from Wordpress and provides a simple API for reading them.

Posts from the feed are automatically cached for a set period of time, so they don't have to be downloaded again in each request.

📦 Installation
---------------

Expand All @@ -18,6 +22,86 @@ $ composer require baraja-core/wordpress-post-feed

You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.

Configuration
-------------

If you are installing the package into the Nette Framework, you can simply register the extension:

```yaml
extensions:
wordpressPostFeed: Baraja\WordPressPostFeed\WordpressPostFeedExtension
```
However, the extension is not required, as you can create an instance manually:
```php
$feed = new \Baraja\WordPressPostFeed\Feed;
```

By default, no dependencies are required. Feed will take care of all dependencies by itself.

How to use
----------

Basic usage is simple:

```php
$feed = new \Baraja\WordPressPostFeed\Feed;

$posts = $feed->load('https://blog.mycleverminds.cz/feed/');

foreach ($posts as $post) {
echo $post->getTitle();
echo $post->getDescription();
echo $post->getLink();
echo $post->getDate()->format('Y-m-d');
echo $post->getCreator();
echo json_encode($post->getCategories());
echo $post->getMainImageUrl();
}
```

The feed will be downloaded automatically, then cached and further requests will be handled instantly straight from the cache.

If you only need to retrieve some posts from the feed, you can use the `limit` and `offset` arguments:

```php
$posts = $feed->load(
url: 'https://blog.mycleverminds.cz/feed/',
limit: 3,
offset: 1,
);
```

Posts are automatically cached for the set period (default 2 hours). If the period expires, the cache is automatically invalidated and the next request will have to retrieve the data and rebuild the cache. This retrieval may slow down this request. If you want to handle all requests instantly, the cache for the feed needs to be automatically refreshed by cron.

Cron will call the following method to refresh the cache:

```php
$feed = new \Baraja\WordPressPostFeed\Feed;
$feed->updateCache('https://blog.mycleverminds.cz/feed/');
```

Image manipulation
------------------

When downloading posts from the feed, images are also automatically downloaded and copied to disk. The image storage is provided by the `\Baraja\WordPressPostFeed\ImageStorage` service, which is automatically registered by the library. The default directory for storing images is `wordpress-post-feed` relative to your `index.php`.

The service provides the following methods:

| Method | Description |
|--------|-------------|
| `save(string $url): void` | Downloads the image from the URL and saves it to disk |
| `getInternalPath(string $url): string` | Returns the absolute path for the internal data store. |
| `getAbsoluteInternalUrl(string $url): string` | Returns the absolute URL to retrieve the image |
| `getRelativeInternalUrl(string $url): string` | Returns the relative URL to retrieve the image |

Working with images is also available directly from the `Post` entity via methods:

- `getAbsoluteInternalUrl()`
- `getRelativeInternalUrl()`
- `getMainImageUrl()`

📄 License
-----------

Expand Down

0 comments on commit cf0d681

Please sign in to comment.