Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Kernel to extract related code into Foundation classes #297

Merged
merged 58 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
c57967c
Create Filesystem.php
caendesilva Jul 30, 2022
d061e5a
Bind Filesystem class in Kernel constructor
caendesilva Jul 30, 2022
086b60c
Move filesystem logic from Kernel to Filesystem class
caendesilva Jul 30, 2022
cd04298
Group together filesystem helpers
caendesilva Jul 30, 2022
f00f2bf
Remove unused imports
caendesilva Jul 30, 2022
e8d743e
Apply fixes from StyleCI
StyleCIBot Jul 30, 2022
7def015
Base path should only accept strings
caendesilva Jul 30, 2022
23bb44b
Pass the Kernel to Filesystem constructor instead of the path
caendesilva Jul 30, 2022
abf7f52
Get the base path from the Kernel
caendesilva Jul 30, 2022
3992047
Merge branch 'kernel-refactor-experiment' of github.com:hydephp/devel…
caendesilva Jul 30, 2022
9c45062
Create Hyperlinks.php
caendesilva Jul 30, 2022
9bdd960
Bind Hyperlinks class in Kernel constructor
caendesilva Jul 30, 2022
d0f9528
Move web links logic from Kernel to Hyperlinks class
caendesilva Jul 30, 2022
adb27c3
Reorder methods
caendesilva Jul 30, 2022
c4785b4
Remove unnecessary comments
caendesilva Jul 30, 2022
48ec6b7
Apply fixes from StyleCI
StyleCIBot Jul 30, 2022
9fe6c0a
Change contract type to concrete
caendesilva Jul 30, 2022
956bd62
Merge branch 'kernel-refactor-experiment' of github.com:hydephp/devel…
caendesilva Jul 30, 2022
1cc5b9f
Apply fixes from StyleCI
StyleCIBot Jul 30, 2022
f15753b
Merge branch 'master' into kernel-refactor-experiment
caendesilva Jul 30, 2022
84bb345
Add class annotations
caendesilva Jul 30, 2022
b172a33
Create FilesystemTest.php
caendesilva Jul 30, 2022
9f96325
Link to the test
caendesilva Jul 30, 2022
49d7e9b
Rearrange methods to match actual class
caendesilva Jul 30, 2022
84beb33
Test base path method
caendesilva Jul 30, 2022
00d4e4a
Set up Filesystem instance in setUp method
caendesilva Jul 30, 2022
cc5110b
Move path method tests to new logic location
caendesilva Jul 30, 2022
7f86af0
Add few more path tests
caendesilva Jul 30, 2022
51f485a
Fix mismatched expected/actual values
caendesilva Jul 30, 2022
44066fd
Clean up tests
caendesilva Jul 30, 2022
160dffa
Move vendorPath method tests to new logic location
caendesilva Jul 30, 2022
bc480d4
Fix mismatched expected/actual values
caendesilva Jul 30, 2022
ebf5ab4
Clean up test code and use mocks
caendesilva Jul 30, 2022
0a9a7fb
Add tests for Filesystem::copy method
caendesilva Jul 30, 2022
ab83cf3
Test moved fluent path helpers
caendesilva Jul 30, 2022
24c1f74
Link to unit tests at top of file
caendesilva Jul 30, 2022
6cf0d65
Test covers both Filesystem and Kernel
caendesilva Jul 30, 2022
6c84fd2
Apply fixes from StyleCI
StyleCIBot Jul 30, 2022
1abad2c
Remove test templates handled in unit test
caendesilva Jul 30, 2022
91a72cb
Add todo
caendesilva Jul 30, 2022
6d6d167
Create HyperlinksTest.php
caendesilva Jul 30, 2022
d57bf92
Move unit test to foundation namespace
caendesilva Jul 30, 2022
e50033d
Move unit test to foundation namespace
caendesilva Jul 30, 2022
8816cb3
Set up Hyperlinks instance in setUp method
caendesilva Jul 30, 2022
0f5fa90
Merge FileHelpersImageTest into new feature test
caendesilva Jul 30, 2022
eb0c9f8
Remove deprecated Hyde::uriPath() helper
caendesilva Jul 30, 2022
9168517
Move unit test to foundation namespace
caendesilva Jul 30, 2022
0f0161d
Apply fixes from StyleCI
StyleCIBot Jul 30, 2022
8037971
Replace mock with actual instance
caendesilva Jul 30, 2022
cb22a15
Remove deprecated Hyde::uriPath() helper
caendesilva Jul 30, 2022
e91a443
Clarify link annotation to facade class
caendesilva Jul 30, 2022
d128d55
Add test description
caendesilva Jul 30, 2022
c2ec349
Add base method tests
caendesilva Jul 31, 2022
66060fc
Add Hyperlinks tests
caendesilva Jul 31, 2022
7f3e674
Add Filesystem tests
caendesilva Jul 31, 2022
cea6cac
Apply fixes from StyleCI
StyleCIBot Jul 31, 2022
086df1e
Update RELEASE_NOTES.md
caendesilva Jul 31, 2022
32ae9e0
Update method annotations
caendesilva Jul 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ This serves two purposes:
- for new features.

### Changed
- for changes in existing functionality.
- internal: The HydeKernel has been refactored to move related logic to service classes. This does not change the end usage as the Hyde facade still works the same

### Deprecated
- for soon-to-be removed features.

### Removed
- for now removed features.
- Remove deprecated Hyde::uriPath() helper

### Fixed
- for any bug fixes.
Expand Down
154 changes: 154 additions & 0 deletions packages/framework/src/Foundation/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Hyde\Framework\Foundation;

use Hyde\Framework\Contracts\HydeKernelContract;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Framework\StaticPageBuilder;

/**
* File helper methods, bound to the HydeKernel instance, and is an integral part of the framework.
*
* If a method uses the name `path` it refers to an internal file path.
* if a method uses the name `link` it refers to a web link used in Blade templates.
*
* @see \Hyde\Framework\Testing\Feature\Foundation\FilesystemTest
*/
class Filesystem
{
protected HydeKernelContract $kernel;

public function __construct(HydeKernelContract $kernel)
{
$this->kernel = $kernel;
}

public function getBasePath(): string
{
return $this->kernel->getBasePath();
}

/**
* Get an absolute file path from a supplied relative path.
*
* The function returns the fully qualified path to your site's root directory.
*
* You may also use the function to generate a fully qualified path to a given file
* relative to the project root directory when supplying the path argument.
*
* @param string $path
* @return string
*/
public function path(string $path = ''): string
{
if (empty($path)) {
return $this->getBasePath();
}

$path = unslash($path);

return $this->getBasePath().DIRECTORY_SEPARATOR.$path;
}

/**
* Works similarly to the path() function, but returns a file in the Framework package.
*
* @param string $path
* @return string
*/
public function vendorPath(string $path = ''): string
{
return $this->path('vendor/hyde/framework/'.unslash($path));
}

/**
* Wrapper for the copy function, but allows choosing if files may be overwritten.
*
* @param string $from The source file path.
* @param string $to The destination file path.
* @param bool $force If true, existing files will be overwritten.
* @return bool|int Returns true|false on copy() success|failure, or an error code on failure
*/
public function copy(string $from, string $to, bool $force = false): bool|int
{
if (! file_exists($from)) {
return 404;
}

if (file_exists($to) && ! $force) {
return 409;
}

return copy($from, $to);
}

/**
* Fluent file helper methods.
*
* Provides a more fluent way of getting either the absolute path
* to a model's source directory, or an absolute path to a file within it.
*
* These are intended to be used as a dynamic alternative to legacy code
* Hyde::path('_pages/foo') becomes Hyde::getBladePagePath('foo')
*/
public function getModelSourcePath(string $model, string $path = ''): string
{
if (empty($path)) {
return $this->path(DiscoveryService::getFilePathForModelClassFiles($model));
}

$path = unslash($path);

return $this->path(DiscoveryService::getFilePathForModelClassFiles($model).DIRECTORY_SEPARATOR.$path);
}

public function getBladePagePath(string $path = ''): string
{
return $this->getModelSourcePath(BladePage::class, $path);
}

public function getMarkdownPagePath(string $path = ''): string
{
return $this->getModelSourcePath(MarkdownPage::class, $path);
}

public function getMarkdownPostPath(string $path = ''): string
{
return $this->getModelSourcePath(MarkdownPost::class, $path);
}

public function getDocumentationPagePath(string $path = ''): string
{
return $this->getModelSourcePath(DocumentationPage::class, $path);
}

/**
* Get the absolute path to the compiled site directory, or a file within it.
*/
public function getSiteOutputPath(string $path = ''): string
{
if (empty($path)) {
return StaticPageBuilder::$outputPath;
}

$path = unslash($path);

return StaticPageBuilder::$outputPath.DIRECTORY_SEPARATOR.$path;
}

/**
* Decode an absolute path created with a Hyde::path() helper into its relative counterpart.
*/
public function pathToRelative(string $path): string
{
return str_starts_with($path, $this->path()) ? unslash(str_replace(
$this->path(),
'',
$path
)) : $path;
}
}
115 changes: 115 additions & 0 deletions packages/framework/src/Foundation/Hyperlinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Hyde\Framework\Foundation;

use Hyde\Framework\Exceptions\BaseUrlNotSetException;
use Hyde\Framework\HydeKernel;
use Hyde\Framework\Models\Pages\DocumentationPage;

/**
* Contains helpers and logic for resolving web paths for compiled files.
*
* It's bound to the HydeKernel instance, and is an integral part of the framework.
*
* @see \Hyde\Framework\Testing\Feature\Foundation\HyperlinksTest
*/
class Hyperlinks
{
protected HydeKernel $kernel;

public function __construct(HydeKernel $kernel)
{
$this->kernel = $kernel;
}

/**
* Format a link to an HTML file, allowing for pretty URLs, if enabled.
*
* @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFormatHtmlPathTest
*/
public function formatHtmlPath(string $destination): string
{
if (config('site.pretty_urls', false) === true) {
if (str_ends_with($destination, '.html')) {
if ($destination === 'index.html') {
return '/';
}
if ($destination === DocumentationPage::getOutputDirectory().'/index.html') {
return DocumentationPage::getOutputDirectory().'/';
}

return substr($destination, 0, -5);
}
}

return $destination;
}

/**
* Inject the proper number of `../` before the links in Blade templates.
*
* @param string $destination relative to output directory on compiled site
* @return string
*
* @see \Hyde\Framework\Testing\Unit\Foundation\HyperlinkFileHelperRelativeLinkTest
*/
public function relativeLink(string $destination): string
{
if (str_starts_with($destination, '../')) {
return $destination;
}

$nestCount = substr_count($this->kernel->currentPage(), '/');
$route = '';
if ($nestCount > 0) {
$route .= str_repeat('../', $nestCount);
}
$route .= $this->formatHtmlPath($destination);

return str_replace('//', '/', $route);
}

/**
* Gets a relative web link to the given image stored in the _site/media folder.
*/
public function image(string $name): string
{
if (str_starts_with($name, 'http')) {
return $name;
}

return $this->relativeLink('media/'.basename($name));
}

/**
* Check if a site base URL has been set in config (or .env).
*/
public function hasSiteUrl(): bool
{
return ! blank(config('site.url'));
}

/**
* Return a qualified URI path to the supplied path if a base URL is set.
*
* @param string $path optional relative path suffix. Omit to return base url.
* @param string|null $default optional default value to return if no site url is set.
* @return string
*
* @throws BaseUrlNotSetException If no site URL is set and no default is provided
*/
public function url(string $path = '', ?string $default = null): string
{
$path = $this->formatHtmlPath(trim($path, '/'));

if ($this->hasSiteUrl()) {
return rtrim(rtrim(config('site.url'), '/').'/'.($path ?? ''), '/');
}

if ($default !== null) {
return $default.'/'.($path ?? '');
}

throw new BaseUrlNotSetException();
}
}
18 changes: 10 additions & 8 deletions packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework;

use Hyde\Framework\Contracts\HydeKernelContract;
use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Helpers\Features;
use Illuminate\Support\Facades\Facade;
Expand All @@ -20,29 +21,30 @@
* @method static string vendorPath(string $path = '')
* @method static string getMarkdownPagePath(string $path = '')
* @method static string getSiteOutputPath(string $path = '')
* @method static string formatHtmlPath(string $destination)
* @method static string getBladePagePath(string $path = '')
* @method static string pathToRelative(string $path)
* @method static string path(string $path = '')
* @method static Features features()
* @method static bool hasFeature(string $feature)
* @method static string path(string $path = '')
* @method static string relativeLink(string $destination)
* @method static string getMarkdownPostPath(string $path = '')
* @method static bool|int copy(string $from, string $to, bool $force = false)
* @method static string getModelSourcePath(string $model, string $path = '')
* @method static string image(string $name)
* @method static void macro(string $name, callable|object $macro)
* @method static RouteContract|null currentRoute()
* @method static string currentPage()
* @method static false|string uriPath(null|string $path = '')
* @method static bool hasSiteUrl()
* @method static string url(string $path = '', ?string $default = null)
* @method static void setBasePath($basePath)
* @method static string url(string $path = '', null|string $default = null)
* @method static void setBasePath(string $basePath)
* @method static void mixin(object $mixin, bool $replace = true)
* @method static string formatHtmlPath(string $destination)
* @method static string makeTitle(string $slug)
* @method static bool hasSiteUrl()
* @method static string getBasePath()
* @method static HydeKernel getInstance()
* @method static HydeKernelContract getInstance()
* @method static string getDocumentationPagePath(string $path = '')
* @method static bool hasMacro(string $name)
* @method static void macro(string $name, callable|object $macro)
* @method static void flushMacros()
*/
class Hyde extends Facade
{
Expand Down
Loading