Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hudsonpereira committed May 26, 2015
0 parents commit b831844
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.phar
composer.lock
vendor
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "dishark/metaeloquent",
"description" : "Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags",
"keywords" : ["laravel", "eloquent", "metatags"],
"homepage" : "http://dishark.github.io/metaeloquent",
"license" : "MIT",
"authors": [
{
"name": "Dishark",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Dishark\\Metaeloquent\\": "src/"
}
}
}
137 changes: 137 additions & 0 deletions readme.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Meta Eloquent

Meta Eloquent is an eloquent booster that allows it to easily manage HTTP Meta tags.

# Installation

## 1. Dependecy

Using composer, execute the following command to automatically update your composer.json:

```
composer require dishark/metaeloquent
```
or manually update your composer.json file

```
{
"require": {
"dishark/metaeloquent": "dev-master"
}
}
```

## 2. Provider

You need to update your application configuration in order to register the package, so it can be loaded by Laravel. Just update your config/app.php file adding the following code at the end of your 'providers' section:

```php
// file START omitted
'providers' => [
// other providers omitted
'Dishark\Metaeloquent\MetaEloquentServiceProvider',
],
// file END omitted
```

#Usage

Make sure to use `Dishark\Metaeloquent\Traits\MetaTrait` in your model. Then declare the `$metaAttributes` property specifying the metatag as the key and the column as the value.

Example 1:

````php
<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
use MetaTrait;

protected $metaAttributes = [
'author' => 'author',
'description' => 'title',
'keywords' => 'keywords',
];
}
```

Sometimes the column isn't enough. Let's create some Meta accessor:

Example 2:

```php
<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
use MetaTrait;

protected $metaAttributes = [
'author' => 'author',
'description' => 'title',
'keywords' => 'keywords',
];

public function getMetaAuthor()
{
return $this->author->name;
}
}
```

## View

In your view:

```php
@extends ('layout')

@section ('metadata')
{!! $post->meta() !!}
@endsection
```

The layout:

```php
<head>
@yield('metadata')
</head>
```

So now the author attribute will be called through this method instead.

## OpenGraph

```php
<?php App\Post;

use Dishark\Metaeloquent\Traits\MetaTrait;

class Post {
use MetaTrait;

protected $metaAttributes = [
'author' => 'author',
'description' => 'title',
'keywords' => 'keywords',

'og:title' => 'name',
'og:image' => 'image',
'og:type' => 'article',
'og:url' => 'url'
];

public function getMetaImage()
{
return asset('images_path/' . $this->image);
}

public function getMetaUrl()
{
return route('articles', $this->slug);
}
}
```
13 changes: 13 additions & 0 deletions src/MetaEloquentServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Dishark\Metaeloquent;

use Illuminate\Support\ServiceProvider;
use Dishark\Metaeloquent\Providers\W3cProvider;
use Dishark\Metaeloquent\Providers\OpenGraphProvider;

class MetaEloquentServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('dishark.metaeloquent.w3c', W3cProvider::class);
$this->app->bind('dishark.metaeloquent.og', OpenGraphProvider::class);
}
}
5 changes: 5 additions & 0 deletions src/Providers/MetaProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace Dishark\Metaeloquent\Providers;

interface MetaProviderInterface {
public function provide($attributes);
}
20 changes: 20 additions & 0 deletions src/Providers/OpenGraphProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php namespace Dishark\Metaeloquent\Providers;

class OpenGraphProvider implements MetaProviderInterface {
public function provide($attributes)
{
$output = '';

$ogKeys = array_filter(array_flip($attributes), function($key){
return strpos($key, 'og:') === 0;
});

$ogKeys = array_flip($ogKeys);

foreach ($ogKeys as $key => $value) {
$output .= "<meta property=\"{$key}\" content=\"{$value}\" />";
}

return $output;
}
}
14 changes: 14 additions & 0 deletions src/Providers/W3cProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace Dishark\Metaeloquent\Providers;

class W3cProvider implements MetaProviderInterface {
public function provide($attributes)
{
$output = '';

$output .= (isset($attributes['author'])) ? "<meta name=\"author\" content=\"{$attributes['author']}\">" : '';
$output .= (isset($attributes['description'])) ? "<meta name=\"description\" content=\"{$attributes['description']}\">" : '';
$output .= (isset($attributes['keywords'])) ? "<meta name=\"keywords\" content=\"{$attributes['keywords']}\">" : '';

return $output;
}
}
41 changes: 41 additions & 0 deletions src/Traits/MetaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace Dishark\Metaeloquent\Traits;

trait MetaTrait {
public function parseMetaAttributes()
{
if(! is_array($this->metaAttributes)) return;

foreach ($this->metaAttributes as $key => $attribute) {
if (method_exists($this, 'getMeta' . $attribute)) {
$this->metaAttributes[$key] = $this->{'getMeta' . $attribute}();

continue;
}

$this->metaAttributes[$key] = ($this->$attribute) ? : $attribute;
}

return $this->metaAttributes;
}

public function meta()
{
$this->metaProviders = $this->metaProviders?:['w3c', 'og'];
$metaAttributes = $this->parseMetaAttributes();
$metaTags = '';

foreach ($this->metaProviders as $provider) {
if (app()->bound($provider)) {
$metaTags .= app($provider)->provide($metaAttributes);

continue;
}

if (app()->bound('dishark.metaeloquent.' . $provider)) {
$metaTags .= app('dishark.metaeloquent.' . $provider)->provide($metaAttributes);
}
}

return $metaTags;
}
}

0 comments on commit b831844

Please sign in to comment.