Skip to content

Commit

Permalink
Merge pull request #305 from hydephp/update-discovery-service
Browse files Browse the repository at this point in the history
Clean up DiscoveryService
  • Loading branch information
caendesilva authored Jul 31, 2022
2 parents 6e1056e + f10daa2 commit 612b24d
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 270 deletions.
7 changes: 5 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ This serves two purposes:

### Changed
- 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
- `DiscoveryService::getSourceFileListForModel()` now throws an exception instead of returning false when given an invalid model class
- `DiscoveryService::getFilePathForModelClassFiles` method was renamed to `DiscoveryService::getModelSourceDirectory`
- `DiscoveryService::getFileExtensionForModelFiles` method was renamed to `DiscoveryService::getModelFileExtension`

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

### Removed
- Removed deprecated Hyde::uriPath() helper
- Removed deprecated CollectionService::findModelFromFilePath()
- Removed deprecated `Hyde::uriPath()` helper
- Removed deprecated `CollectionService::findModelFromFilePath()`

### Fixed
- for any bug fixes.
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Foundation/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ public function copy(string $from, string $to, bool $force = false): int|bool
public function getModelSourcePath(string $model, string $path = ''): string
{
if (empty($path)) {
return $this->path(DiscoveryService::getFilePathForModelClassFiles($model));
return $this->path(DiscoveryService::getModelSourceDirectory($model));
}

$path = unslash($path);

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

public function getBladePagePath(string $path = ''): string
Expand Down
131 changes: 56 additions & 75 deletions packages/framework/src/Services/DiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Hyde\Framework\Services;

use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Contracts\PageParserContract;
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
Expand All @@ -29,76 +31,33 @@ public static function getParserClassForModel(string $model): string
/**
* Create and get a constructed instance of a Model's Parser class.
*
* @param string $model Class constant of the Model to get the Parser for.
* @param string<AbstractPage> $model Class constant of the Model to get the Parser for.
* @param string $slug The slug of the source file to parse.
*
* @example getParserForModel(MarkdownPost::class, 'hello-world')
*
* @return object The constructed Parser instance.
* @return PageParserContract The constructed Parser instance.
*/
public static function getParserInstanceForModel(string $model, string $slug): object
public static function getParserInstanceForModel(string $model, string $slug): PageParserContract
{
/** @var AbstractPage $model */
return new $model::$parserClass($slug);
}

/**
* Get the file extension for a models source files.
*/
public static function getFileExtensionForModelFiles(string $model): string
{
/** @var AbstractPage $model */
return $model::getFileExtension();
}

/**
* Get the source directory path of a model.
*/
public static function getFilePathForModelClassFiles(string $model): string
{
/** @var AbstractPage $model */
return $model::getSourceDirectory();
}

/**
* Create a filepath that can be opened in the browser from a terminal.
*
* @param string $filepath
* @return string
*/
public static function createClickableFilepath(string $filepath): string
{
if (realpath($filepath) === false) {
return $filepath;
}

return 'file://'.str_replace(
'\\',
'/',
realpath($filepath)
);
}

/**
* Get all the Markdown files in the _docs directory.
*/
public static function getDocumentationPageFiles(): array|false
{
return self::getSourceFileListForModel(DocumentationPage::class);
}

/**
* Supply a model::class constant and get a list of all the existing source file base names.
*
* @param string $model
* @return array|false array on success, false if the class was not found
* @param string<AbstractPage> $model
* @return array
*
* @throws \Hyde\Framework\Exceptions\UnsupportedPageTypeException
*
* @example DiscoveryService::getSourceFileListForModel(BladePage::class)
*/
public static function getSourceFileListForModel(string $model): array|false
public static function getSourceFileListForModel(string $model): array
{
if (! class_exists($model) || ! is_subclass_of($model, AbstractPage::class)) {
return false;
throw new UnsupportedPageTypeException($model);
}

// Scan the source directory, and directories therein, for files that match the model's file extension.
Expand All @@ -113,29 +72,38 @@ public static function getSourceFileListForModel(string $model): array|false
return $files;
}

public static function formatSlugForModel(string $model, string $filepath): string
public static function getModelFileExtension(string $model): string
{
/** @var AbstractPage $model */
$slug = str_replace(Hyde::path($model::$sourceDirectory), '', $filepath);

if (str_ends_with($slug, $model::$fileExtension)) {
$slug = substr($slug, 0, -strlen($model::$fileExtension));
}
return $model::getFileExtension();
}

$slug = unslash($slug);
public static function getModelSourceDirectory(string $model): string
{
/** @var AbstractPage $model */
return $model::getSourceDirectory();
}

return $slug;
public static function getBladePageFiles(): array
{
return self::getSourceFileListForModel(BladePage::class);
}

/**
* @return array
* Get all the Markdown files in the _pages directory.
*/
public static function getMarkdownPageFiles(): array|false
public static function getMarkdownPageFiles(): array
{
return self::getSourceFileListForModel(MarkdownPage::class);
}

public static function getMarkdownPostFiles(): array
{
return self::getSourceFileListForModel(MarkdownPost::class);
}

public static function getDocumentationPageFiles(): array
{
return self::getSourceFileListForModel(DocumentationPage::class);
}

/**
* Get all the Media asset file paths.
* Returns a full file path, unlike the other get*List methods.
Expand All @@ -150,20 +118,33 @@ public static function getMediaAssetFiles(): array
}

/**
* @return array
* Get all the Blade files in the _pages directory.
* Create a filepath that can be opened in the browser from a terminal.
*
* @param string<AbstractPage> $filepath
* @return string
*/
public static function getBladePageFiles(): array|false
public static function createClickableFilepath(string $filepath): string
{
return self::getSourceFileListForModel(BladePage::class);
if (realpath($filepath) === false) {
return $filepath;
}

return 'file://'.str_replace(
'\\',
'/',
realpath($filepath)
);
}

/**
* @return array
* Get all the Markdown files in the _posts directory.
*/
public static function getMarkdownPostFiles(): array|false
public static function formatSlugForModel(string $model, string $filepath): string
{
return self::getSourceFileListForModel(MarkdownPost::class);
/** @var AbstractPage $model */
$slug = str_replace(Hyde::path($model::$sourceDirectory), '', $filepath);

if (str_ends_with($slug, $model::$fileExtension)) {
$slug = substr($slug, 0, -strlen($model::$fileExtension));
}

return unslash($slug);
}
}
Loading

0 comments on commit 612b24d

Please sign in to comment.