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

Bump to latest Underpin Version #3

Open
JUVOJustin opened this issue Apr 12, 2023 · 1 comment
Open

Bump to latest Underpin Version #3

JUVOJustin opened this issue Apr 12, 2023 · 1 comment

Comments

@JUVOJustin
Copy link

JUVOJustin commented Apr 12, 2023

I just became aware of your project and tried installing this loader. However, i was only able to use it with version 2 of the core. Could the version be bumped to be compatible with the latest core version, or would this involve major refactoring?

@alexstandiford
Copy link
Collaborator

alexstandiford commented Apr 16, 2023

Could the version be bumped to me compatible with the latest core version, or would this involve major refactoring?

It certainly could be integrated with the new version of Underpin, and I don't even think it would take a lot of refactoring to make it work. Looking at the features this integration uses, it's mostly using things that still exist in the latest Underpin, they're just named differently. It's something I'd like to see done, as I'm still using BerlinDB regularly, and would like to be able to use this integration again.

To make this refactor work, we would need to do the following:

  1. Remove this file entirely. It will do nothing in Underpin 3 and up.
  2. Update the readme to reflect how the system could be set up in newer versions of Underpin.
  3. Update the Database loader to extend Object_Registry instead of Loader_Registry. This is probably a 1:1 transition and shouldn't be a big deal.
  4. Update the composer.json file to depend on the latest version of Underpin.

⚠️ One potentially major caveat - newer versions of Underpin require PHP 8.1 to work. If you can't run PHP 8.1, I would recommend sticking with the older version for now or avoiding this altogether until you're in a place where you can use PHP 8.1.

The biggest difference is how you would implement this in the newer version of Underpin, because it doesn't have an enforced pattern like the older version had which autoloaded features such as this one. In other words, you would have to create some way to access the BerlinDB system yourself. This could be done with a singleton instance, or you could add it to a service container, or something like that.

I envision that you would end up making this work kind of like this:

Create some kind of accessor for BerlinDB. The simplest take on this would be a singleton instance, like this, but I usually use Dependency Injection or some kind of service container to handle this. It really depends on the context of what you're building. The example below is using a singleton. This would not be a part of the Underpin BerlinDB library, but rather something you would add in your own plugin to interface with it.

<?php

use Underpin\Interfaces\Singleton;
use Underpin\Traits\With_Instance;
use Underpin\BerlinDB\Loaders\Database;

class Berlin_DB implements Singleton{
    use With_Instance;

    /**
     * @var Database The database loader object for BerlinDB.
     */
    public readonly $database;

    public function __construct(){
        $this->database = new Database();
    }
}

Once that's set up, you would be able to access the system statically using ::instance:

Berlin_DB::instance()->database

Then, you would be able to create new table much like how it's documented now. The only difference is that you're using the accessor you made instead of what the older version of Underpin enforced:

Berlin_DB::instance()->database->add( 'example', [
	'table'             => 'Namespace\To\Berlin_DB\Table',
	'schema'            => 'Namespace\To\Berlin_DB\Schema',
	'query'             => 'Namespace\To\Berlin_DB\Query',
	'name'              => 'Human Readable Table Name',
	'description'       => 'Description of the purpose of this table',
	'sanitize_callback' => function( $key, $value ){
		// Function to sanitize fields before saving.
	}
] );

Basically, anywhere that says underpin()->berlin_db() in the docs would, in this case, be replaced with Berlin_DB::instance()->database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants