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 core internals to use the Laravel Service Container #333

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9e6a3ab
Create the Core files
caendesilva May 14, 2022
39406f6
Define the first service contract
caendesilva May 14, 2022
42ce322
Add the bootstrap method
caendesilva May 14, 2022
d9c3af8
Add PHPDoc
caendesilva May 14, 2022
56a77fd
Create the system manager
caendesilva May 14, 2022
c86257a
Implement the system manager
caendesilva May 14, 2022
8fe22c8
Restructure PHPDocs to remove redundancy
caendesilva May 14, 2022
7dea469
Add the sourceLocationManager getter
caendesilva May 14, 2022
62b6b9c
Deprecate version bindings
caendesilva May 14, 2022
b22e4fa
Add notice it's not yet implemented
caendesilva May 14, 2022
8640d81
Apply fixes from StyleCI
StyleCIBot May 14, 2022
541fff9
Merge pull request #331 from hydephp/analysis-J2vOKV
caendesilva May 14, 2022
8be2923
Allow disabling of the creation of default directories
caendesilva May 14, 2022
51bdcd8
Add helper to quickly get the instantiated provider from the service …
caendesilva May 14, 2022
8a3c673
Deprecate static::$sourceDirectory
caendesilva May 14, 2022
a84e678
Add note that it breaks the RC
caendesilva May 14, 2022
4defd9a
Revert to original behaviour
caendesilva May 14, 2022
4daa47a
Apply fixes from StyleCI
StyleCIBot May 14, 2022
ccc50f6
Merge pull request #334 from hydephp/analysis-0gN0N4
caendesilva May 14, 2022
580cd9a
Add autodiscovery information
caendesilva May 14, 2022
fb8200f
Merge branch 'servicecontainer' of github.com:hydephp/framework into …
caendesilva May 14, 2022
029feba
Apply fixes from StyleCI
StyleCIBot May 14, 2022
ec28718
Merge pull request #335 from hydephp/analysis-YjVdVY
caendesilva May 14, 2022
1fe5d4b
Remove source location
caendesilva May 14, 2022
c08e6ea
Disable CreatesDefaultDirectories using config instead
caendesilva May 14, 2022
bdd96a5
Merge branch 'servicecontainer' of github.com:hydephp/framework into …
caendesilva May 14, 2022
9abd49f
Apply fixes from StyleCI
StyleCIBot May 14, 2022
ee92631
Merge pull request #337 from hydephp/analysis-GD94wk
caendesilva May 14, 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
22 changes: 20 additions & 2 deletions src/Concerns/Internal/FileHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Hyde\Framework\Concerns\Internal;

use Hyde\Framework\Core\HydeManagerContract;

/**
* Offloads file helper methods for the Hyde Facade.
*
Expand Down Expand Up @@ -51,12 +53,28 @@ public static function docsIndexPath(): string|false
public static function path(string $path = ''): string
{
if (empty($path)) {
return getcwd();
return static::getProjectRoot();
}

$path = trim($path, '/\\');

return getcwd().DIRECTORY_SEPARATOR.$path;
return static::getProjectRoot().DIRECTORY_SEPARATOR.$path;
}

/**
* Get the path to the Hyde root directory.
* Catches binding resolution errors allowing the function to
* be used in PHPUnit tests before the application is booted.
*
* @return string
*/
public static function getProjectRoot(): string
{
try {
return app(HydeManagerContract::class)->hydeSystemManager()->getProjectRoot();
} catch (\Illuminate\Contracts\Container\BindingResolutionException) {
return getcwd();
}
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/Core/HydeManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Hyde\Framework\Core;

/**
* The HydeManager bootstraps the Hyde Framework Core Services.
*
* To extend implementations, create a class that extends this class,
* and register it as the Singleton implementation of the Contract in a
* ServiceProvider extending the HydeServiceProvider and load it in app.php.
*/
class HydeManager implements HydeManagerContract
{
protected HydeSystemManager $hydeSystemManager;
protected SourceLocationManager $sourceLocationManager;

public function __construct()
{
$this->bootstrap();
}

public function bootstrap(): void
{
$this->hydeSystemManager = new ($this->getHydeSystemManager());
$this->sourceLocationManager = new ($this->getSourceLocationManager());
}

public function getHydeSystemManager(): string
{
return HydeSystemProvider::class;
}

public function getSourceLocationManager(): string
{
return SourceLocationProvider::class;
}

public function hydeSystemManager(): HydeSystemManager
{
return $this->hydeSystemManager;
}

public function sourceLocationManager(): SourceLocationManager
{
return $this->sourceLocationManager;
}

/**
* Get the instantiated provider from the service container for the given contract.
*/
public static function get(string $classImplementingContract)
{
return app()->get(HydeManagerContract::class)->{basename($classImplementingContract)}();
}
}
43 changes: 43 additions & 0 deletions src/Core/HydeManagerContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Hyde\Framework\Core;

/**
* The Hyde Manager is the glue that holds Hyde services together.
* It's intended to act as an easy way to swap out implementations of
* various services to extend or modify the default Hyde behavior.
*
* The interface implementation should be lightweight on its own and
* instead of containing a large amount of logic, it should only instead
* contain methods specifying where a certain service should be loaded from.
* When extending the Hyde core, create a new ServiceProvider extending the
* HydeServiceProvider, and register your HydeManagerContract implementation.
* Then in the app config, swap out the default ServiceProvider with your own.
*/
interface HydeManagerContract
{
/**
* Load all the core service implementations into the HydeManager.
*/
public function bootstrap(): void;

/**
* =========================================================================
* Get the fully qualified class name of the manager implementation to use.
*
* Default return syntax: \Hyde\Framework\Core\ServiceManager::class
* =========================================================================
*/
public function getHydeSystemManager(): string;

public function getSourceLocationManager(): string;

/**
* =========================================================================
* Get the manager implementation instantiated in the bootstrap process.
* =========================================================================.
*/
public function hydeSystemManager(): HydeSystemManager;

public function sourceLocationManager(): SourceLocationManager;
}
12 changes: 12 additions & 0 deletions src/Core/HydeSystemManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Hyde\Framework\Core;

interface HydeSystemManager
{
/**
* Return the full path to the root of the application.
* Used to assemble all file paths in the Hyde::path() method.
*/
public function getProjectRoot(): string;
}
14 changes: 14 additions & 0 deletions src/Core/HydeSystemProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Hyde\Framework\Core;

class HydeSystemProvider implements HydeSystemManager
{
/**
* @inheritDoc
*/
public function getProjectRoot(): string
{
return getcwd();
}
}
26 changes: 25 additions & 1 deletion src/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Composer\InstalledVersions;
use Hyde\Framework\Actions\CreatesDefaultDirectories;
use Hyde\Framework\Core\HydeManager;
use Hyde\Framework\Core\HydeManagerContract;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -18,20 +20,34 @@ class HydeServiceProvider extends ServiceProvider
*/
public function register()
{
/**
* @deprecated
*/
$this->app->bind(
'hyde.version',
function () {
return InstalledVersions::getPrettyVersion('hyde/hyde') ?: 'unreleased';
}
);

/**
* @deprecated
*/
$this->app->bind(
'framework.version',
function () {
return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased';
}
);

/**
* Register the HydeManager implementation class.
* Swap this out if you want to override the services.
*/
$this->app->singleton(HydeManagerContract::class, function ($app) {
return new HydeManager();
});

$this->commands([
Commands\HydePublishHomepageCommand::class,
Commands\HydeUpdateConfigsCommand::class,
Expand All @@ -54,7 +70,15 @@ function () {
*/
public function boot()
{
(new CreatesDefaultDirectories)->__invoke();
/**
* Creates the default directories required for Hyde to be able to process files.
* If you running the Framework in a non-standard environment that does not
* use the Framework to generate files automatically, such as in Buddy,
* you can disable this with the "hidden" config option seen below.
*/
if (config('hyde.create_default_directories', true)) {
(new CreatesDefaultDirectories)->__invoke();
}

$this->loadViewsFrom(__DIR__.'/../resources/views', 'hyde');

Expand Down
2 changes: 2 additions & 0 deletions src/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static function getFileExtensionForModelFiles(string $model): string

/**
* Get the source directory path of a model.
*
* This is what powers the Hyde autodiscovery.
*/
public static function getFilePathForModelClassFiles(string $model): string
{
Expand Down