Skip to content

Commit b877df7

Browse files
committed
Complete: Publish provider - WIP: Publishing routes and views
1 parent 3389410 commit b877df7

File tree

6 files changed

+105
-6
lines changed

6 files changed

+105
-6
lines changed

routes/web.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?php
22

3-
// TODO
3+
Route::group(['middleware' => config('blog.middleware')], function () {
4+
Route::resource('posts', 'PostController');
5+
});
6+
7+
Route::get('posts/{post}', 'PostController@show')->name('posts.show');

src/BlogServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ private function handleMigrations()
105105
private function handlePublishing()
106106
{
107107
if ($this->app->runningInConsole()) {
108-
109-
// TODO: Are these really needed?
110108
$this->publishes([
111109
__DIR__.'/../database/factories/' => database_path('factories'),
112110
], 'blog-factories');

src/Builders/PostBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
class PostBuilder extends Builder
1111
{
12+
/**
13+
* Return results where Posts are related for the current logged in user.
14+
*
15+
* @return \Chriscreates\Blog\Builders\PostBuilder
16+
*/
17+
public function forCurrentUser() : PostBuilder
18+
{
19+
return $this->where('user_id', request()->user()->id ?? null);
20+
}
21+
1222
/**
1323
* Return results where Posts have status.
1424
*

src/Console/Commands/InstallCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public function handle()
3333
$this->callSilent('vendor:publish', ['--tag' => 'blog-config']);
3434
$this->callSilent('migrate');
3535

36-
// TODO: Do we need to publish a provider?
37-
3836
$this->info('Installation complete.');
3937
}
4038
}

src/Console/Commands/SetupCommand.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Chriscreates\Blog\Tag;
88
use Illuminate\Console\Command;
99
use Illuminate\Console\DetectsApplicationNamespace;
10+
use Illuminate\Support\Str;
1011

1112
class SetupCommand extends Command
1213
{
@@ -44,8 +45,11 @@ public function handle()
4445
}
4546
}
4647

48+
if ( ! file_exists(app_path('Providers/BlogServiceProvider.php'))) {
49+
$this->publishProvider();
50+
}
51+
4752
// TODO:
48-
// Controllers
4953
// Routes
5054
// Views?
5155
@@ -88,4 +92,38 @@ private function seed()
8892
$post->comments()->save(factory(Comment::class)->make());
8993
});
9094
}
95+
96+
/**
97+
* Register the provider.
98+
*
99+
* @return void
100+
*/
101+
private function publishProvider()
102+
{
103+
copy(
104+
__DIR__.'/../../Providers/BlogServiceProvider.php',
105+
app_path('Providers/BlogServiceProvider.php')
106+
);
107+
108+
$namespace = Str::replaceLast('\\', '', $this->getAppNamespace());
109+
$appConfig = file_get_contents(config_path('app.php'));
110+
111+
if (Str::contains($appConfig, $namespace.'\\Providers\\BlogServiceProvider::class')) {
112+
return;
113+
}
114+
115+
$lineEndingCount = [
116+
"\r\n" => substr_count($appConfig, "\r\n"),
117+
"\r" => substr_count($appConfig, "\r"),
118+
"\n" => substr_count($appConfig, "\n"),
119+
];
120+
121+
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
122+
123+
file_put_contents(config_path('app.php'), str_replace(
124+
"{$namespace}\\Providers\EventServiceProvider::class,".$eol,
125+
"{$namespace}\\Providers\EventServiceProvider::class,".$eol." {$namespace}\Providers\BlogServiceProvider::class,".$eol,
126+
$appConfig
127+
));
128+
}
91129
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use \Chriscreates\Blog\Post;
6+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
7+
use Illuminate\Support\Facades\Route;
8+
9+
class BlogServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Define your route model bindings, pattern filters, etc.
13+
*
14+
* @return void
15+
*/
16+
public function boot()
17+
{
18+
parent::boot();
19+
20+
Route::bind('post', function ($value) {
21+
return Post::where(function ($query) use ($value) {
22+
$query->where('id', $value)
23+
->orWhere('slug', 'LIKE', "%{$value}%");
24+
})->first();
25+
});
26+
}
27+
28+
/**
29+
* Define the routes for the application.
30+
*
31+
* @return void
32+
*/
33+
public function map()
34+
{
35+
$this->mapBlogRoutes();
36+
}
37+
38+
/**
39+
* Define the "api" routes for the application.
40+
*
41+
* These routes are typically stateless.
42+
*
43+
* @return void
44+
*/
45+
protected function mapBlogRoutes()
46+
{
47+
Route::middleware('api')
48+
->namespace($this->namespace)
49+
->group(base_path('routes/blog.php'));
50+
}
51+
}

0 commit comments

Comments
 (0)