Creating Core System Classes
Every time Higgs runs there are several base classes that are initialized automatically as part of the core framework. It is possible, however, to swap any of the core system classes with your own version or even just extend the core versions.
Most users will never have any need to do this, but the option to replace or extend them does exist for those who would like to significantly alter the Higgs core.
Important
Messing with a core system class has a lot of implications, so make sure you know what you are doing before attempting it.
System Class List
The following is a list of the core system classes that are invoked every time Higgs runs:
Higgs\Autoloader\Autoloader
Higgs\Autoloader\FileLocator
Higgs\Cache\CacheFactory
Higgs\Cache\Handlers\BaseHandler
Higgs\Cache\Handlers\FileHandler
Higgs\Cache\ResponseCache
Higgs\Higgs
Higgs\Config\BaseService
Higgs\Config\DotEnv
Higgs\Config\Factories
Higgs\Config\Services
Higgs\Controller
Higgs\Cookie\Cookie
Higgs\Cookie\CookieStore
Higgs\Debug\Exceptions
Higgs\Debug\Timer
Higgs\Events\Events
Higgs\Filters\Filters
Higgs\HTTP\CLIRequest
(if launched from command line only)Higgs\HTTP\ContentSecurityPolicy
Higgs\HTTP\Header
Higgs\HTTP\IncomingRequest
(if launched over HTTP)Higgs\HTTP\Message
Higgs\HTTP\OutgoingRequest
Higgs\HTTP\Request
Higgs\HTTP\Response
Higgs\HTTP\SiteURI
Higgs\HTTP\SiteURIFactory
Higgs\HTTP\URI
Higgs\HTTP\UserAgent
(if launched over HTTP)Higgs\Log\Logger
Higgs\Log\Handlers\BaseHandler
Higgs\Log\Handlers\FileHandler
Higgs\Router\RouteCollection
Higgs\Router\Router
Higgs\Superglobals
Higgs\View\View
Replacing Core Classes
To use one of your own system classes instead of a default one, ensure:
the Autoloader can find your class,
your new class implements the appropriate interface,
and modify the appropriate Service to load your class in place of the core class.
Creating Your Class
For example, if you have a new App\Libraries\RouteCollection
class that you would
like to use in place of
the core system class, you would create your class like this:
<?php
namespace App\Libraries;
use Higgs\Router\RouteCollectionInterface;
class RouteCollection implements RouteCollectionInterface
{
// ...
}
Adding the Service
Then you would add the routes
service in
app/Config/Services.php to load your class instead:
<?php
namespace Config;
use Higgs\Config\BaseService;
class Services extends BaseService
{
public static function routes(bool $getShared = true)
{
if ($getShared) {
return static::getSharedInstance('routes');
}
return new \App\Libraries\RouteCollection(static::locator(), config(Modules::class), config(Routing::class));
}
// ...
}
Extending Core Classes
If all you need to is add some functionality to an existing library - perhaps add a method or two - then it’s overkill to recreate the entire library. In this case, it’s better to simply extend the class. Extending the class is nearly identical to Replacing Core Classes with one exception:
The class declaration must extend the parent class.
For example, to extend the native RouteCollection
class, you would declare your class
with:
<?php
namespace App\Libraries;
use Higgs\Router\RouteCollection as BaseRouteCollection;
class RouteCollection extends BaseRouteCollection
{
// ...
}
If you need to use a constructor in your class make sure you extend the parent constructor:
<?php
namespace App\Libraries;
use Higgs\Router\RouteCollection as BaseRouteCollection;
class RouteCollection extends BaseRouteCollection
{
public function __construct()
{
parent::__construct();
// your code here
}
}
Tip: Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the Higgs core.