Skip to content

Commit

Permalink
feat: Add make:article command to generate a new article
Browse files Browse the repository at this point in the history
  • Loading branch information
owenconti committed Jan 30, 2022
1 parent ba440b9 commit 53549ea
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/Console/Commands/MakeArticleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace OhSeeSoftware\LaravelMarkdownContent\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;

class MakeArticleCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:article {type} {slug}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Scaffolds out a new article.';

protected function getStub()
{
return realpath(__DIR__ . '/../../stubs/article.stub');
}

public function handle()
{
$slug = trim($this->argument('slug'));
$type = trim($this->argument('type'));

$path = base_path("content/articles/{$slug}.md");

if ($this->files->exists($path)) {
$this->error("{$slug} already exists!");

return false;
}

$stub = $this->files->get($this->getStub());
$stub = Str::of($stub)
->replace("\nslug:", "\nslug: {$slug}")
->replace("\ntype:", "\ntype: {$type}")
->replace('title:', "title: '".Str::of($slug)->title()->replace('-', ' ')."'")
->replace('updated_at:', 'updated_at: '.now()->format('Y-m-d'))
->replace('created_at:', 'created_at: '.now()->format('Y-m-d'));

$this->files->put($path, $stub);
}
}
5 changes: 4 additions & 1 deletion src/LaravelMarkdownContentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OhSeeSoftware\LaravelMarkdownContent;

use Illuminate\Support\ServiceProvider;
use OhSeeSoftware\LaravelMarkdownContent\Console\Commands\MakeArticleCommand;

class LaravelMarkdownContentServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -40,7 +41,9 @@ public function boot()
], 'lang');*/

// Registering package commands.
// $this->commands([]);
$this->commands([
MakeArticleCommand::class
]);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/stubs/article.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
slug:
type:
title:
category_slug:
excerpt:
updated_at:
created_at:
---

0 comments on commit 53549ea

Please sign in to comment.