{"id":19433175,"url":"https://github.com/ssnepenthe/metis","last_synced_at":"2025-04-24T20:31:29.240Z","repository":{"id":57058728,"uuid":"47230303","full_name":"ssnepenthe/metis","owner":"ssnepenthe","description":"Pimple, with some helpful tweaks for working in WordPress.","archived":false,"fork":false,"pushed_at":"2017-10-19T19:22:00.000Z","size":118,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-11T16:28:52.252Z","etag":null,"topics":["dependency-injection","php","pimple","wordpress","wordpress-php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ssnepenthe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-02T02:02:19.000Z","updated_at":"2019-12-17T18:01:05.000Z","dependencies_parsed_at":"2022-08-24T14:53:22.332Z","dependency_job_id":null,"html_url":"https://github.com/ssnepenthe/metis","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssnepenthe%2Fmetis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssnepenthe%2Fmetis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssnepenthe%2Fmetis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ssnepenthe%2Fmetis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ssnepenthe","download_url":"https://codeload.github.com/ssnepenthe/metis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223967503,"owners_count":17233403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","php","pimple","wordpress","wordpress-php-library"],"created_at":"2024-11-10T14:38:41.771Z","updated_at":"2024-11-10T14:38:42.283Z","avatar_url":"https://github.com/ssnepenthe.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# metis\n[Pimple](https://pimple.sensiolabs.org/), with some some helpful tweaks for working in WordPress.\n\n## Requirements\nPHP 5.3 or later and Composer.\n\n**Note:** Metis should continue to work down to 5.3 but is no longer tested below 5.4.\n\n## Installation\n```\n$ composer require ssnepenthe/metis\n```\n\n## Usage\nThis is basically Pimple but you will use `Metis\\Container` instead of `Pimple\\Container`.\n\nThe following features have been added:\n\n**Handle activation and deactivation logic within your service providers**\n\n```php\nclass Some_Provider implements Pimple\\ServiceProviderInterface {\n    public function activate( Pimple\\Container $container ) {\n        // Handle activation here.\n    }\n\n    public function deactivate( Pimple\\Container $container ) {\n        // Handle deactivation here.\n    }\n\n    // ...\n}\n```\n\nAnd then call the corresponsing method on your container instance.\n\n```php\n$container = new Metis\\Container;\n$container-\u003eregister( new Some_Provider );\n\nregister_activation_hook( __FILE__, array( $container, 'activate' ) );\nregister_deactivation_hook( __FILE__, array( $container, 'deactivate' ) );\n```\n\n**Handle boot logic (add_action/add_filter calls) within your service providers**\n\n```php\nclass Another_Provider implements Pimple\\ServiceProviderInterface {\n    public function boot( Pimple\\Container $container ) {\n        add_action( 'init', array( $container['service'], 'init' ) );\n    }\n\n    // ...\n}\n```\n\nAnd then call the corresponding method on your container instance.\n\n```php\n$container = new Metis\\Container;\n$container-\u003eregister( new Another_Provider );\n\nadd_action( 'plugins_loaded', array( $container, 'boot' ) );\n```\n\n**Service Proxies**\n\nOne of the many benefits of a dependency injection container like Pimple is that objects are created on demand as you access their container entries.\n\nThis can be especially useful for functionality that is only needed on a limited number of requests (e.g. admin, cron, etc.).\n\nUnfortunately this doesn't always work the way you might want in WordPress:\n\n```php\nclass Admin_Provider implements Pimple\\ServiceProviderInterface {\n    public function boot( Pimple\\Container $container ) {\n        add_action( 'admin_init', array( $container['admin_page'], 'do_something' ) );\n    }\n\n    // ...\n}\n```\n\nSince the `boot()` method is typically attached to the `plugins_loaded` hook, the admin page object will always be created regardless of whether `admin_init` has been triggered.\n\nA sensible approach would be to verify that the current request is for an admin page before calling `add_action()`:\n\n```php\nclass Admin_Provider implements Pimple\\ServiceProviderInterface {\n    public function boot( Pimple\\Container $container ) {\n        if ( is_admin() ) {\n            add_action( 'admin_init', array( $container['admin_page'], 'do_something' ) );\n        }\n    }\n\n    // ...\n}\n```\n\nBut this results in a boot method littered with conditionals.\n\nAn alternative would be to access the `admin_page` entry within an anonymous function:\n\n```php\nclass Admin_Provider implements Pimple\\ServiceProviderInterface {\n    public function boot( Pimple\\Container $container ) {\n        add_action( 'admin_init', function() use ( $container ) {\n            $container['admin_page']-\u003edo_something();\n        }\n    }\n\n    // ...\n}\n```\n\nBut that gets tedious quickly and can result in a large number of unnecessary `Closure` objects floating around.\n\nInstead, you might choose to extend `Metis\\Base_Provider` and use the `proxy()` method:\n\n```php\nclass Admin_Provider extends Metis\\Base_Provider {\n    public function boot( Pimple\\Container $container ) {\n        add_action( 'admin_init', array( $this-\u003eproxy( $container, 'admin_page' ), 'do_something' ) );\n    }\n\n    // ...\n}\n```\n\nThis will create a `Metis\\Proxy` object to be used in place of the admin page object. This object will correctly proxy all method calls to the underlying service from the container while holding off on creation of that service until it is actually needed.\n\n**Access WordPress globals from the container**\n\nUse the `WordPress_Provider` class to get access to frequently used WordPress globals from the container.\n\n```php\n$container = new Metis\\Container;\n$container-\u003eregister( new Metis\\WordPress_Provider );\n\n$container['wp'] === $GLOBALS['wp']; // true\n```\n\n`$wp`, `$wpdb`, `$wp_query`, `$wp_rewrite`, `$wp_filesystem` and `$wp_object_cache` are all added to the container.\n\nBe careful about timing when using these - each will return null if it has not yet been defined.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssnepenthe%2Fmetis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fssnepenthe%2Fmetis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fssnepenthe%2Fmetis/lists"}