Skip to content

Commit 5e0e31a

Browse files
committed
WIP: Expanding the structure
1 parent c72a1f8 commit 5e0e31a

File tree

14 files changed

+690
-67
lines changed

14 files changed

+690
-67
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
[![Quality Score](https://img.shields.io/scrutinizer/g/chrisjk123/laravel-blogger.svg?style=flat-square)](https://scrutinizer-ci.com/g/chrisjk123/laravel-blogger)
66
[![Total Downloads](https://img.shields.io/packagist/dt/chrisjk123/laravel-blogger.svg?style=flat-square)](https://packagist.org/packages/chrisjk123/laravel-blogger)
77

8+
## Table of Contents
9+
10+
[Introduction](#introduction)
11+
[Requirements](#requirements)
12+
[Installation](#installation)
13+
[Local testing](#local-testing)
14+
[Usage](#usage)
15+
[Changelog](#changelog)
16+
[Security](#security)
17+
[Credits](#credits)
18+
[License](#license)
19+
20+
## Introduction
821

922
This package is a blogging database with maxed out models, migrations and seeders to help get you setup. After the package is installed the only thing you have to do is add the `HasPosts` trait to an Eloquent model to associate the users.
1023

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"extra": {
4848
"laravel": {
4949
"providers": [
50-
"Chriscreates\\Blog\\BloggerServiceProvider"
50+
"Chriscreates\\Blog\\BlogServiceProvider"
5151
]
5252
}
5353
}

config/blog.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| User relations
8+
|--------------------------------------------------------------------------
9+
|
10+
| This is the default path to the User model in Laravel and primary key.
11+
| You are free to change this path to anything you like.
12+
|
13+
*/
14+
15+
'user' => [
16+
'user_class' => \App\User::class,
17+
'user_key_name' => 'id',
18+
],
19+
20+
/*
21+
|--------------------------------------------------------------------------
22+
| Route Middleware
23+
|--------------------------------------------------------------------------
24+
|
25+
| These middleware will be attached to every route. You can add your own
26+
| middleware to this list or change any of the existing middleware.
27+
|
28+
*/
29+
30+
'middleware' => [
31+
'web',
32+
'auth',
33+
],
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Post commenting options
38+
|--------------------------------------------------------------------------
39+
|
40+
| The default for commenting on posts is enabled, as well as guest
41+
| commenting. Feel free to change these conditions to false.
42+
|
43+
*/
44+
45+
'posts' => [
46+
'allow_comments' => true,
47+
'allow_guest_comments' => true,
48+
],
49+
];

config/blogs.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

routes/web.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// TODO

src/BlogServiceProvider.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog;
4+
5+
use Illuminate\Contracts\Container\BindingResolutionException;
6+
use Illuminate\Events\Dispatcher;
7+
use Illuminate\Support\Facades\Route;
8+
use Illuminate\Support\ServiceProvider;
9+
10+
class BlogServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* All of the event / listener mappings.
14+
*
15+
* @var array
16+
*/
17+
protected $events = [
18+
\Chriscreates\Blog\Events\PostViewed::class => [
19+
\Chriscreates\Blog\Listeners\StoreViewData::class,
20+
],
21+
];
22+
23+
/**
24+
* Bootstrap the application services.
25+
*/
26+
public function boot()
27+
{
28+
$this->handleEvents();
29+
$this->handleRoutes();
30+
$this->handleMigrations();
31+
$this->handlePublishing();
32+
}
33+
34+
/**
35+
* Register bindings in the container.
36+
*
37+
* @return void
38+
*/
39+
public function register()
40+
{
41+
$this->handleConfig();
42+
$this->handleCommands();
43+
}
44+
45+
/**
46+
* Register the events and listeners.
47+
*
48+
* @return void
49+
* @throws BindingResolutionException
50+
*/
51+
private function handleEvents()
52+
{
53+
$events = $this->app->make(Dispatcher::class);
54+
55+
foreach ($this->events as $event => $listeners) {
56+
foreach ($listeners as $listener) {
57+
$events->listen($event, $listener);
58+
}
59+
}
60+
}
61+
62+
/**
63+
* Register the package routes.
64+
*
65+
* @return void
66+
*/
67+
private function handleRoutes()
68+
{
69+
Route::group($this->routeConfiguration(), function () {
70+
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
71+
});
72+
}
73+
74+
/**
75+
* Get the Blog route group configuration array.
76+
*
77+
* @return array
78+
*/
79+
private function routeConfiguration()
80+
{
81+
// TODO
82+
return [
83+
'namespace' => 'Blog\Http\Controllers',
84+
'prefix' => config('blog.path'),
85+
'middleware' => config('blog.middleware'),
86+
];
87+
}
88+
89+
/**
90+
* Register the package's migrations.
91+
*
92+
* @return void
93+
*/
94+
private function handleMigrations()
95+
{
96+
if ($this->app->runningInConsole()) {
97+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations/');
98+
}
99+
}
100+
101+
/**
102+
* Register the package's publishable resources.
103+
*
104+
* @return void
105+
*/
106+
private function handlePublishing()
107+
{
108+
if ($this->app->runningInConsole()) {
109+
$this->publishes([
110+
__DIR__.'/../database/factories/' => database_path('factories'),
111+
], 'blog-factories');
112+
113+
$this->publishes([
114+
__DIR__.'/../database/seeds/' => database_path('seeds'),
115+
], 'blog-seeders');
116+
117+
$this->publishes([
118+
__DIR__.'/../config/blogs.php' => config_path('blogs.php'),
119+
], 'blog-config');
120+
}
121+
}
122+
123+
/**
124+
* @return void
125+
*/
126+
private function handleConfig()
127+
{
128+
$this->mergeConfigFrom(
129+
__DIR__.'/../config/blog.php',
130+
'config'
131+
);
132+
}
133+
134+
/**
135+
* @return void
136+
*/
137+
private function handleCommands()
138+
{
139+
$this->commands([
140+
\Chriscreates\Blog\Console\Commands\InstallCommand::class,
141+
\Chriscreates\Blog\Console\Commands\PublishCommand::class,
142+
\Chriscreates\Blog\Console\Commands\SetupCommand::class,
143+
]);
144+
}
145+
}

src/BloggerServiceProvider.php

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Console\DetectsApplicationNamespace;
7+
use Illuminate\Support\Str;
8+
9+
class InstallCommand extends Command
10+
{
11+
use DetectsApplicationNamespace;
12+
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'blog:install';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Install all of the necessary blog resources';
26+
27+
/**
28+
* Execute the console command.
29+
*
30+
* @return void
31+
*/
32+
public function handle()
33+
{
34+
$this->callSilent('vendor:publish', ['--tag' => 'blog-factories']);
35+
$this->callSilent('vendor:publish', ['--tag' => 'blog-seeders']);
36+
$this->callSilent('vendor:publish', ['--tag' => 'blog-config']);
37+
// $this->callSilent('migrate');
38+
//
39+
// $this->registerCanvasServiceProvider();
40+
41+
$this->info('Installation complete.');
42+
}
43+
44+
/**
45+
* Register the Canvas service provider in the application configuration file.
46+
*
47+
* @return void
48+
*/
49+
private function registerCanvasServiceProvider()
50+
{
51+
$namespace = Str::replaceLast('\\', '', $this->getAppNamespace());
52+
$appConfig = file_get_contents(config_path('app.php'));
53+
54+
if (Str::contains($appConfig, $namespace.'\\Providers\\CanvasServiceProvider::class')) {
55+
return;
56+
}
57+
58+
$lineEndingCount = [
59+
"\r\n" => substr_count($appConfig, "\r\n"),
60+
"\r" => substr_count($appConfig, "\r"),
61+
"\n" => substr_count($appConfig, "\n"),
62+
];
63+
64+
$eol = array_keys($lineEndingCount, max($lineEndingCount))[0];
65+
66+
file_put_contents(config_path('app.php'), str_replace(
67+
"{$namespace}\\Providers\EventServiceProvider::class,".$eol,
68+
"{$namespace}\\Providers\EventServiceProvider::class,".$eol." {$namespace}\Providers\CanvasServiceProvider::class,".$eol,
69+
$appConfig
70+
));
71+
72+
file_put_contents(app_path('Providers/CanvasServiceProvider.php'), str_replace(
73+
"namespace App\Providers;",
74+
"namespace {$namespace}\Providers;",
75+
file_get_contents(app_path('Providers/CanvasServiceProvider.php'))
76+
));
77+
}
78+
}

0 commit comments

Comments
 (0)