Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #365 #438

Open
wants to merge 9 commits into
base: 3.0
Choose a base branch
from
2 changes: 0 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ preset: psr2

risky: false

linting: true

finder:
name:
- "*.php"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,19 @@ To get a menu by its slug, use the syntax below. The menu items will be loaded i

The currently supported menu items are: Pages, Posts, Custom Links and Categories.

Once you'll have instances of `MenuItem` class, if you want to use the original instance (like the original Page or Term, for example), just call the `MenuItem::instance()` method. The `MenuItem` object is just a post with `post_type` equals `nav_menu_item`:
Once you'll have instances of `MenuItem` class, if you want to use the original instance (like the original Page or Term, for example), just access the `MenuItem::object()` relation. The `MenuItem` object is just a post with `post_type` equals `nav_menu_item`:

```php
$menu = Menu::slug('primary')->first();

foreach ($menu->items as $item) {
echo $item->instance()->title; // if it's a Post
echo $item->instance()->name; // if it's a Term
echo $item->instance()->link_text; // if it's a custom link
echo $item->object->title; // if it's a Post
echo $item->object->name; // if it's a Term
echo $item->object->link_text; // if it's a custom link
}
```

The `instance()` method will return the matching object:
`object` will return the matching object:

- `Post` instance for `post` menu item;
- `Page` instance for `page` menu item;
Expand Down
14 changes: 4 additions & 10 deletions src/Corcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ class Corcel
*/
public static function isLaravel()
{
return function_exists('app') &&
app() instanceof Application;
}

/**
* @return bool
*/
public static function isLumen()
{
return preg_match('/Lumen', app()->version()) === 1;
return function_exists('app') && (
app() instanceof Application ||
strpos(app()->version(), 'Lumen') === 0
);
}
}
23 changes: 22 additions & 1 deletion src/Laravel/CorcelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Corcel\Corcel;
use Corcel\Laravel\Auth\AuthUserProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
use Corcel\Model\Post;
use Corcel\Model\Page;
use Corcel\Model\CustomLink;
use Corcel\Model\Taxonomy;

/**
* Class CorcelServiceProvider
Expand All @@ -23,8 +28,9 @@ public function boot()
{
$this->publishConfigFile();
$this->registerAuthProvider();
$this->registerMorphMaps();
}

/**
* @return void
*/
Expand All @@ -47,6 +53,21 @@ private function registerAuthProvider()
}
}

/**
* register morph maps for polymorphic relations
*
* @return void
*/
public function registerMorphMaps()
{
Relation::morphMap([
'post' => Post::class,
'page' => Page::class,
'custom' => CustomLink::class,
'category' => Taxonomy::class,
]);
}

/**
* @return void
*/
Expand Down
24 changes: 21 additions & 3 deletions src/Model/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,29 @@ class MenuItem extends Post
*/
private $instanceRelations = [
'post' => Post::class,
'page' => Page::class,
'page' => PageMenu::class,
'custom' => CustomLink::class,
'category' => Taxonomy::class,
];

/**
* @return Post|Page|CustomLink|Taxonomy
*/
public function object()
{
return $this->morphTo();
}

public function getObjectIdAttribute()
{
return $this->meta->_menu_item_object_id;
}

public function getObjectTypeAttribute()
{
return $this->meta->_menu_item_object;
}

/**
* @return Post|Page|CustomLink|Taxonomy
*/
Expand All @@ -41,13 +59,13 @@ public function parent()
}

/**
* @deprecated deprecated in favor of object()
* @return Post|Page|CustomLink|Taxonomy
*/
public function instance()
{
if ($className = $this->getClassName()) {
return (new $className)->newQuery()
->find($this->meta->_menu_item_object_id);
return $this->object;
}

return null;
Expand Down
13 changes: 13 additions & 0 deletions src/Model/PageMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Corcel\Model;

/**
* Class PageMenu
*
* @package Corcel\Model
* @author Luiz Vid <luizvid@gmail.com>
*/
class PageMenu extends MenuItem
{
}
13 changes: 12 additions & 1 deletion tests/Unit/Model/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Corcel\Model\MenuItem;
use Corcel\Model\Post;
use Corcel\Model\Taxonomy;
use Corcel\Model\Page;

/**
* Class MenuTest
Expand Down Expand Up @@ -93,7 +94,7 @@ public function it_has_parent_relation()
return $item->meta->_menu_item_object === 'post';
});

$parent = $posts->first()->instance();
$parent = $posts->first()->object;
$child = $posts->last();

$this->assertEquals($parent->ID, $child->parent()->ID);
Expand Down Expand Up @@ -122,7 +123,9 @@ public function it_can_have_custom_links_associated_as_meta()

$this->assertEquals('Foobar', $item->post_title);
$this->assertEquals('Foobar', $item->instance()->link_text);
$this->assertEquals('Foobar', $item->object->link_text);
$this->assertEquals('http://example.com', $item->meta->_menu_item_url);
$this->assertEquals('http://example.com', $item->object->url);
$this->assertEquals('http://example.com', $item->instance()->url);
}

Expand All @@ -138,6 +141,8 @@ public function it_can_have_pages()
});

$pages->each(function (MenuItem $item) {
$this->assertEquals('page', $item->object_type);
$this->assertInstanceOf(Page::class, $item->object);
$this->assertEquals("page-title", $item->instance()->post_title);
$this->assertEquals("page-content", $item->instance()->post_content);
});
Expand All @@ -155,6 +160,8 @@ public function it_can_have_posts()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('post', $item->object_type);
$this->assertInstanceOf(Post::class, $item->object);
$this->assertEquals("post-title", $item->instance()->title);
$this->assertEquals("post-content", $item->instance()->content);
});
Expand All @@ -172,6 +179,8 @@ public function it_can_have_custom_links()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('custom', $item->object_type);
$this->assertInstanceOf(CustomLink::class, $item->object);
$this->assertEquals("http://example.com", $item->instance()->url);
$this->assertEquals("custom-link-text", $item->instance()->link_text);
});
Expand All @@ -189,6 +198,8 @@ public function it_can_have_categories()
});

$posts->each(function (MenuItem $item) {
$this->assertEquals('category', $item->object_type);
$this->assertInstanceOf(Taxonomy::class, $item->object);
$this->assertEquals("Bar", $item->instance()->name);
$this->assertEquals("bar", $item->instance()->slug);
});
Expand Down