0
0
Fork 0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-04-18 18:38:44 +00:00

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; use BookStack\Model;
/**
* Class Attribute
* @package BookStack
*/
class Tag extends Model class Tag extends Model
{ {
protected $fillable = ['name', 'value', 'order']; protected $fillable = ['name', 'value', 'order'];

View file

@ -14,7 +14,6 @@ use Illuminate\Support\Carbon;
* @property string $name * @property string $name
* @property Carbon $expires_at * @property Carbon $expires_at
* @property User $user * @property User $user
* @package BookStack\Api
*/ */
class ApiToken extends Model implements Loggable 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 * guard with 'remember' functionality removed. Basic auth and event emission
* has also been removed to keep this simple. Designed to be extended by external * has also been removed to keep this simple. Designed to be extended by external
* Auth Guards. * Auth Guards.
*
* @package Illuminate\Auth
*/ */
class ExternalBaseSessionGuard implements StatefulGuard 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 * 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 * via the Saml2 controller & Saml2Service. This class provides a safer, thin
* version of SessionGuard. * version of SessionGuard.
*
* @package BookStack\Auth\Access\Guards
*/ */
class Saml2SessionGuard extends ExternalBaseSessionGuard class Saml2SessionGuard extends ExternalBaseSessionGuard
{ {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property Book $book * @property Book $book
* @method Builder whereSlugs(string $bookSlug, string $childSlug) * @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 * Class Chapter
* @property Collection<Page> $pages * @property Collection<Page> $pages
* @package BookStack\Entities
*/ */
class Chapter extends BookChild class Chapter extends BookChild
{ {

View file

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

View file

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

View file

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

View file

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

View file

@ -4,9 +4,9 @@ use BookStack\Actions\ActivityType;
use BookStack\Entities\Book; use BookStack\Entities\Book;
use BookStack\Entities\Chapter; use BookStack\Entities\Chapter;
use BookStack\Entities\Entity; use BookStack\Entities\Entity;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Managers\TrashCan; use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Page; use BookStack\Entities\Page;
use BookStack\Entities\PageRevision; use BookStack\Entities\PageRevision;
use BookStack\Exceptions\MoveOperationException; 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\Book;
use BookStack\Entities\BookChild; use BookStack\Entities\BookChild;
@ -18,7 +18,6 @@ class BookContents
/** /**
* BookContents constructor. * BookContents constructor.
* @param $book
*/ */
public function __construct(Book $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 BookStack\Entities\Page;
use DOMDocument; 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\Page;
use BookStack\Entities\PageRevision; 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\Book;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Bookshelf;
use Illuminate\Session\Store;
class EntityContext class ShelfContext
{ {
protected $session;
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id'; 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. * Get the current bookshelf context for the given book.
*/ */
public function getContextualShelfForBook(Book $book): ?Bookshelf 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)) { if (!is_int($contextBookshelfId)) {
return null; return null;
@ -37,11 +26,10 @@ class EntityContext
/** /**
* Store the current contextual shelf ID. * Store the current contextual shelf ID.
* @param int $shelfId
*/ */
public function setShelfContext(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() 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 class SlugGenerator
{ {
protected $entity;
/**
* SlugGenerator constructor.
* @param $entity
*/
public function __construct(Entity $entity)
{
$this->entity = $entity;
}
/** /**
* Generate a fresh slug for the given entity. * Generate a fresh slug for the given entity.
* The slug will generated so it does not conflict within the same parent item. * 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); $slug = $this->formatNameAsSlug($entity->name);
while ($this->slugInUse($slug)) { while ($this->slugInUse($slug, $entity)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }
return $slug; return $slug;
@ -45,16 +34,16 @@ class SlugGenerator
* Check if a slug is already in-use for this * Check if a slug is already in-use for this
* type of model within the same parent. * 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) { if ($entity instanceof BookChild) {
$query->where('book_id', '=', $this->entity->book_id); $query->where('book_id', '=', $entity->book_id);
} }
if ($this->entity->id) { if ($entity->id) {
$query->where('id', '!=', $this->entity->id); $query->where('id', '!=', $entity->id);
} }
return $query->count() > 0; 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\Book;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Bookshelf;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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