Skip to content

Commit

Permalink
Add Twill Capsules
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro authored and ifox committed May 10, 2021
1 parent 4c8cc69 commit 52aa2a0
Show file tree
Hide file tree
Showing 14 changed files with 741 additions and 139 deletions.
8 changes: 6 additions & 2 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
### General
trailingComma: all
printWidth: 80
tabWidth: 4
useTabs: false
singleQuote: true
trailingCommaPHP: php5
braceStyle: psr-2
requirePragma: false
insertPragma: false
semi: false

### PHP
trailingCommaPHP: true
braceStyle: psr-2
39 changes: 39 additions & 0 deletions config/capsules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

return [
'path' => app_path('Twill/Capsules'),

'list' => [
// ['name' => 'Posts', 'enabled' => true],
],
];

/// To fully override this config:
///
//[
// 'modules' => [
// 'path' => app_path('Twill/Modules'),
//
// 'loaded' = true,
//
// 'list' => [
// [
// "name" => "Post",
// "enabled" => true,
// "psr4_path" => "/app-path/app/Twill/Modules/Post/app",
// "namespace" => "App\Twill\Modules\Post",
// "root_path" => "/app-path/app/Twill/Modules/Post",
// "migrations_dir" => "/app-path/app/Twill/Modules/Post/database/migrations",
// "views_dir" => "/app-path/app/Twill/Modules/Post/resources/views",
// "view_prefix" => "Post.resources.views.admin",
// "routes_file" => "/app-path/app/Twill/Modules/Post/routes/admin.php",
// "model" => "App\Twill\Modules\Post\Data\Models\Post",
// "translation" => "App\Twill\Modules\Post\Data\Models\PostTranslation",
// "slug" => "App\Twill\Modules\Post\Data\Models\PostSlug",
// "revision" => "App\Twill\Modules\Post\Data\Models\PostRevision",
// "repository" => "App\Twill\Modules\Post\Data\Repositories\PostRepository",
// "controller" => "App\Twill\Modules\Post\Http\Controllers\PostController",
// "formRequest" => "App\Twill\Modules\Post\Http\Requests\PostRequest",
// ],
// ],
// ];
89 changes: 89 additions & 0 deletions src/CapsulesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace A17\Twill;

use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use A17\Twill\Services\Capsules\Manager;
use A17\Twill\Services\Routing\HasRoutes;
use A17\Twill\Services\Capsules\HasCapsules;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;

class CapsulesServiceProvider extends RouteServiceProvider
{
use HasRoutes, HasCapsules;

protected function mergeTwillConfig()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/capsules.php',
'twill.capsules'
);

$this->app
->make('config')
->set('twill.capsules.list', $this->getCapsuleList());

$this->app->make('config')->set('twill.capsules.loaded', true);
}

public function register()
{
$this->registerConfig();
}

protected function registerConfig()
{
$this->mergeTwillConfig();

$this->registerCapsules();

$this->registerViewPaths();

$this->registerManager();
}

public function registerCapsules()
{
$this->getCapsuleList()->map(function ($capsule) {
$this->registerCapsule($capsule);
});
}

protected function registerCapsule($capsule)
{
$this->loadMigrationsFrom($capsule['migrations_dir']);
}

public function map(Router $router)
{
$this->getCapsuleList()->each(function ($capsule) use ($router) {
$this->registerCapsuleRoutes($router, $capsule);
});
}

public function registerCapsuleRoutes($router, $capsule)
{
$this->registerRoutes(
$router,
$this->getRouteGroupOptions(),
$this->getRouteMiddleware(),
$this->supportSubdomainRouting(),
"{$capsule['namespace']}\Http\Controllers",
$capsule['routes_file']
);
}

public function registerViewPaths()
{
$this->callAfterResolving('view', function ($view) {
$view->addLocation(config('twill.capsules.path'));
});
}

public function registerManager()
{
$this->app->instance('twill.capsules.manager', new Manager());
}
}
15 changes: 15 additions & 0 deletions src/Helpers/frontend_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ function icon($name, $opts = [])
return "<svg class=\"icon--$name $css_class\" $title $role><use xlink:href=\"" . $svg_link . "\"></use></svg>";
}
}

if (!function_exists('twillViewName')) {
function twillViewName($module, $suffix)
{
$view = "'admin.'.$module.'.{$suffix}'";

if (view()->exists($view)) {
return $view;
}

$prefix = app('twill.capsules.manager')->getCapsuleViewPrefix($module);

return "{$prefix}.{$suffix}";
}
}
36 changes: 33 additions & 3 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Helpers\FlashLevel;
use A17\Twill\Services\Capsules\HasCapsules;
use A17\Twill\Services\Blocks\BlockCollection;
use A17\Twill\Models\Behaviors\HasSlug;
use Illuminate\Contracts\Foundation\Application;
Expand All @@ -23,6 +24,8 @@

abstract class ModuleController extends Controller
{
use HasCapsules;

/**
* @var Application
*/
Expand Down Expand Up @@ -1556,7 +1559,18 @@ protected function validateFormRequest()
$this->request->offsetUnset($field);
});

return App::make("$this->namespace\Http\Requests\Admin\\" . $this->modelName . "Request");
return App::make($this->getFormRequestClass());
}

public function getFormRequestClass()
{
$request = "$this->namespace\Http\Requests\Admin\\" . $this->modelName . "Request";

if (@class_exists($request)) {
return $request;
}

return $this->getCapsuleFormRequestClass($this->modelName);
}

/**
Expand Down Expand Up @@ -1617,15 +1631,31 @@ protected function getModelName()
*/
protected function getRepository()
{
return App::make("$this->namespace\Repositories\\" . $this->modelName . "Repository");
return App::make($this->getRepositoryClass($this->modelName));
}

public function getRepositoryClass($model)
{
if (@class_exists($class = "$this->namespace\Repositories\\" . $model . "Repository"))
{
return $class;
}

return $this->getCapsuleRepositoryClass($model);
}

/**
* @return string
*/
protected function getViewPrefix()
{
return "admin.$this->moduleName";
$prefix = "admin.$this->moduleName";

if (view()->exists("$prefix.form")) {
return $prefix;
}

return $this->getCapsuleViewPrefix($this->moduleName);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Models/Behaviors/HasRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait HasRevisions
{
public function revisions()
{
return $this->hasMany(config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision")->orderBy('created_at', 'desc');
return $this->hasMany($this->getRevisionModel())->orderBy('created_at', 'desc');
}

public function scopeMine($query)
Expand All @@ -26,4 +26,16 @@ public function revisionsArray()
];
})->toArray();
}

protected function getRevisionModel()
{
$revision = config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision";

if (@class_exists($revision))
{
return $revision;
}

return $this->getCapsuleRevisionClass(class_basename($this));
}
}
21 changes: 14 additions & 7 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace A17\Twill\Models\Behaviors;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Str;

trait HasSlug
{
Expand All @@ -27,15 +26,23 @@ protected static function bootHasSlug()

public function slugs()
{
return $this->hasMany(
$this->getNamespace() . "\Slugs\\" . $this->getSlugClassName()
);
return $this->hasMany($this->getSlugModelClass());
}

public function getSlugClass()
{
$slugClassName = $this->getNamespace() . "\Slugs\\" . $this->getSlugClassName();
return new $slugClassName;
return new $this->getSlugModelClass();
}

public function getSlugModelClass()
{
$slug = $this->getNamespace() . "\Slugs\\" . $this->getSlugClassName();

if (@class_exists()) {
return $slug;
}

return $this->getCapsuleSlugClass(class_basename($this));
}

protected function getSlugClassName()
Expand Down
11 changes: 9 additions & 2 deletions src/Models/Behaviors/HasTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Query\JoinClause;
use A17\Twill\Services\Capsules\HasCapsules;

trait HasTranslation
{
use Translatable;
use Translatable, HasCapsules;

public function getTranslationModelNameDefault()
{
return config('twill.namespace') . "\Models\Translations\\" . class_basename($this) . 'Translation';
$repository = config('twill.namespace') . "\Models\Translations\\" . class_basename($this) . 'Translation';

if (@class_exists($repository)) {
return $repository;
}

return $this->getCapsuleTranslationClass(class_basename($this));
}

public function scopeWithActiveTranslations($query, $locale = null)
Expand Down
9 changes: 7 additions & 2 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ abstract class Model extends BaseModel implements TaggableInterface

public $timestamps = true;

protected function isTranslationModel()
{
return Str::endsWith(get_class($this), 'Translation');
}

public function scopePublished($query)
{
return $query->wherePublished(true);
Expand Down Expand Up @@ -75,8 +80,8 @@ public function getFillable()
// Use the list of translatable attributes on our base model
if (
blank($fillable) &&
Str::contains($class = get_class($this), 'Models\Translations') &&
property_exists($class, 'baseModuleModel')
$this->isTranslationModel() &&
property_exists($this, 'baseModuleModel')
) {
$fillable = (new $this->baseModuleModel)->getTranslatedAttributes();

Expand Down
Loading

0 comments on commit 52aa2a0

Please sign in to comment.