Moved models to folder, renamed managers to tools

Tools seems to fit better since the classes were a bit of a mixed bunch
and did not always manage.
Also simplified the structure of the SlugGenerator class.
Also focused EntityContext on shelves and simplified to use session
helper.
This commit is contained in:
Dan Brown 2020-11-21 23:20:54 +00:00
parent 66917520cb
commit c7a2d568bf
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
45 changed files with 52 additions and 95 deletions

View File

@ -2,10 +2,6 @@
use BookStack\Model;
/**
* Class Attribute
* @package BookStack
*/
class Tag extends Model
{
protected $fillable = ['name', 'value', 'order'];

View File

@ -14,7 +14,6 @@ use Illuminate\Support\Carbon;
* @property string $name
* @property Carbon $expires_at
* @property User $user
* @package BookStack\Api
*/
class ApiToken extends Model implements Loggable
{

View File

@ -15,8 +15,6 @@ use Illuminate\Contracts\Session\Session;
* guard with 'remember' functionality removed. Basic auth and event emission
* has also been removed to keep this simple. Designed to be extended by external
* Auth Guards.
*
* @package Illuminate\Auth
*/
class ExternalBaseSessionGuard implements StatefulGuard
{

View File

@ -9,8 +9,6 @@ namespace BookStack\Auth\Access\Guards;
* into the default laravel 'Guard' auth flow. Instead most of the logic is done
* via the Saml2 controller & Saml2Service. This class provides a safer, thin
* version of SessionGuard.
*
* @package BookStack\Auth\Access\Guards
*/
class Saml2SessionGuard extends ExternalBaseSessionGuard
{

View File

@ -4,7 +4,6 @@
* Class Ldap
* An object-orientated thin abstraction wrapper for common PHP LDAP functions.
* Allows the standard LDAP functions to be mocked for testing.
* @package BookStack\Services
*/
class Ldap
{

View File

@ -7,7 +7,6 @@ use BookStack\Model;
* Class SocialAccount
* @property string $driver
* @property User $user
* @package BookStack\Auth
*/
class SocialAccount extends Model implements Loggable
{

View File

@ -18,7 +18,6 @@ use Illuminate\Notifications\Notifiable;
/**
* Class User
* @package BookStack\Auth
* @property string $id
* @property string $name
* @property string $email

View File

@ -1,6 +1,6 @@
<?php namespace BookStack\Entities;
use BookStack\Entities\Managers\EntityContext;
use BookStack\Entities\Tools\ShelfContext;
use Illuminate\View\View;
class BreadcrumbsViewComposer
@ -10,9 +10,9 @@ class BreadcrumbsViewComposer
/**
* BreadcrumbsViewComposer constructor.
* @param EntityContext $entityContextManager
* @param ShelfContext $entityContextManager
*/
public function __construct(EntityContext $entityContextManager)
public function __construct(ShelfContext $entityContextManager)
{
$this->entityContextManager = $entityContextManager;
}

View File

@ -6,8 +6,6 @@
* Provides access to the core entity models.
* Wrapped up in this provider since they are often used together
* so this is a neater alternative to injecting all in individually.
*
* @package BookStack\Entities
*/
class EntityProvider
{

View File

@ -1,7 +1,7 @@
<?php namespace BookStack\Entities;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\PageContent;
use BookStack\Uploads\ImageService;
use DomPDF;
use Exception;

View File

@ -12,7 +12,6 @@ use Illuminate\Support\Collection;
* @property string $description
* @property int $image_id
* @property Image|null $cover
* @package BookStack\Entities
*/
class Book extends Entity implements HasCoverImage
{

View File

@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property Book $book
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
*/
class BookChild extends Entity
abstract class BookChild extends Entity
{
/**

View File

@ -5,7 +5,6 @@ use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
* @package BookStack\Entities
*/
class Chapter extends BookChild
{

View File

@ -32,8 +32,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static Entity|Builder hasPermission(string $permission)
* @method static Builder withLastView()
* @method static Builder withViewCount()
*
* @package BookStack\Entities
*/
class Entity extends Ownable
{
@ -326,8 +324,7 @@ class Entity extends Ownable
*/
public function refreshSlug(): string
{
$generator = new SlugGenerator($this);
$this->slug = $generator->generate();
$this->slug = (new SlugGenerator)->generate($this);
return $this->slug;
}
}

View File

@ -3,7 +3,7 @@
use BookStack\Actions\ActivityType;
use BookStack\Actions\TagRepo;
use BookStack\Entities\Book;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity;

View File

@ -3,7 +3,7 @@
use BookStack\Actions\ActivityType;
use BookStack\Entities\Book;
use BookStack\Entities\Bookshelf;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity;

View File

@ -3,8 +3,8 @@
use BookStack\Actions\ActivityType;
use BookStack\Entities\Book;
use BookStack\Entities\Chapter;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity;

View File

@ -4,9 +4,9 @@ use BookStack\Actions\ActivityType;
use BookStack\Entities\Book;
use BookStack\Entities\Chapter;
use BookStack\Entities\Entity;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Page;
use BookStack\Entities\PageRevision;
use BookStack\Exceptions\MoveOperationException;

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities\Managers;
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Book;
use BookStack\Entities\BookChild;
@ -18,7 +18,6 @@ class BookContents
/**
* BookContents constructor.
* @param $book
*/
public function __construct(Book $book)
{

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities\Managers;
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Page;
use DOMDocument;

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities\Managers;
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Page;
use BookStack\Entities\PageRevision;

View File

@ -1,29 +1,18 @@
<?php namespace BookStack\Entities\Managers;
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Book;
use BookStack\Entities\Bookshelf;
use Illuminate\Session\Store;
class EntityContext
class ShelfContext
{
protected $session;
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
/**
* EntityContextManager constructor.
*/
public function __construct(Store $session)
{
$this->session = $session;
}
/**
* Get the current bookshelf context for the given book.
*/
public function getContextualShelfForBook(Book $book): ?Bookshelf
{
$contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
$contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
if (!is_int($contextBookshelfId)) {
return null;
@ -37,11 +26,10 @@ class EntityContext
/**
* Store the current contextual shelf ID.
* @param int $shelfId
*/
public function setShelfContext(int $shelfId)
{
$this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
}
/**
@ -49,6 +37,6 @@ class EntityContext
*/
public function clearShelfContext()
{
$this->session->forget($this->KEY_SHELF_CONTEXT_ID);
session()->forget($this->KEY_SHELF_CONTEXT_ID);
}
}

View File

@ -5,25 +5,14 @@ use Illuminate\Support\Str;
class SlugGenerator
{
protected $entity;
/**
* SlugGenerator constructor.
* @param $entity
*/
public function __construct(Entity $entity)
{
$this->entity = $entity;
}
/**
* Generate a fresh slug for the given entity.
* The slug will generated so it does not conflict within the same parent item.
*/
public function generate(): string
public function generate(Entity $entity): string
{
$slug = $this->formatNameAsSlug($this->entity->name);
while ($this->slugInUse($slug)) {
$slug = $this->formatNameAsSlug($entity->name);
while ($this->slugInUse($slug, $entity)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
return $slug;
@ -45,16 +34,16 @@ class SlugGenerator
* Check if a slug is already in-use for this
* type of model within the same parent.
*/
protected function slugInUse(string $slug): bool
protected function slugInUse(string $slug, Entity $entity): bool
{
$query = $this->entity->newQuery()->where('slug', '=', $slug);
$query = $entity->newQuery()->where('slug', '=', $slug);
if ($this->entity instanceof BookChild) {
$query->where('book_id', '=', $this->entity->book_id);
if ($entity instanceof BookChild) {
$query->where('book_id', '=', $entity->book_id);
}
if ($this->entity->id) {
$query->where('id', '!=', $this->entity->id);
if ($entity->id) {
$query->where('id', '!=', $entity->id);
}
return $query->count() > 0;

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities\Managers;
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Book;
use BookStack\Entities\Bookshelf;

View File

@ -2,9 +2,9 @@
use Activity;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Bookshelf;
use BookStack\Entities\Managers\EntityContext;
use BookStack\Entities\Tools\ShelfContext;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Exceptions\ImageUploadException;
use Illuminate\Http\Request;
@ -18,7 +18,7 @@ class BookController extends Controller
protected $bookRepo;
protected $entityContextManager;
public function __construct(EntityContext $entityContextManager, BookRepo $bookRepo)
public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
{
$this->bookRepo = $bookRepo;
$this->entityContextManager = $entityContextManager;

View File

@ -4,7 +4,7 @@ namespace BookStack\Http\Controllers;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Book;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Exceptions\SortOperationException;
use BookStack\Facades\Activity;

View File

@ -2,7 +2,7 @@
use Activity;
use BookStack\Entities\Book;
use BookStack\Entities\Managers\EntityContext;
use BookStack\Entities\Tools\ShelfContext;
use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException;
@ -22,7 +22,7 @@ class BookshelfController extends Controller
/**
* BookController constructor.
*/
public function __construct(BookshelfRepo $bookshelfRepo, EntityContext $entityContextManager, ImageRepo $imageRepo)
public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
{
$this->bookshelfRepo = $bookshelfRepo;
$this->entityContextManager = $entityContextManager;

View File

@ -1,7 +1,7 @@
<?php namespace BookStack\Http\Controllers;
use BookStack\Entities\Book;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;

View File

@ -2,7 +2,7 @@
use Activity;
use BookStack\Entities\Book;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Page;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo;

View File

@ -3,7 +3,7 @@
namespace BookStack\Http\Controllers;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Notifications\TestEmail;
use BookStack\Uploads\ImageService;
use Illuminate\Http\Request;

View File

@ -1,8 +1,8 @@
<?php namespace BookStack\Http\Controllers;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Managers\PageEditActivity;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\PageEditActivity;
use BookStack\Entities\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException;

View File

@ -3,7 +3,7 @@
namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException;
use Throwable;

View File

@ -1,6 +1,6 @@
<?php namespace BookStack\Http\Controllers;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException;
use GatherContent\Htmldiff\Htmldiff;

View File

@ -2,7 +2,7 @@
use BookStack\Actions\ActivityType;
use BookStack\Entities\Deletion;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
class RecycleBinController extends Controller
{

View File

@ -4,7 +4,7 @@ use BookStack\Actions\ViewService;
use BookStack\Entities\Book;
use BookStack\Entities\Bookshelf;
use BookStack\Entities\Entity;
use BookStack\Entities\Managers\EntityContext;
use BookStack\Entities\Tools\ShelfContext;
use BookStack\Entities\SearchService;
use BookStack\Entities\SearchOptions;
use Illuminate\Http\Request;
@ -21,7 +21,7 @@ class SearchController extends Controller
public function __construct(
ViewService $viewService,
SearchService $searchService,
EntityContext $entityContextManager
ShelfContext $entityContextManager
) {
$this->viewService = $viewService;
$this->searchService = $searchService;

View File

@ -4,7 +4,7 @@ use BookStack\Actions\Activity;
use BookStack\Actions\ActivityService;
use BookStack\Actions\ActivityType;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Page;
use BookStack\Entities\Repos\PageRepo;
use Carbon\Carbon;

View File

@ -1,6 +1,6 @@
<?php namespace Tests\Entity;
use BookStack\Entities\Managers\PageContent;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Page;
use Tests\TestCase;

View File

@ -1,6 +1,6 @@
<?php namespace Tests\Uploads;
use BookStack\Entities\Managers\TrashCan;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Uploads\Attachment;
use BookStack\Entities\Page;