Gravity lets other developers define settings and services in your dependency injection container using files in their packages. By pulling everything together, Gravity makes it easy to build and share small, configurable services.
As a package author, you'll define services and settings in your repository using the set()
method:
# /path/to/package/.gravity/foo.php
$g->set('foo.bar.baz', true); // defines a setting
$g->set('Foo\Bar\Baz', new StdClass()); // defines a service
As a package consumer, you'll request services and settings in your project using the get()
method:
# /path/to/project/file.php
$g = (new \Jstewmc\Gravity\Gravity())->pull(); // returns Gravity's manager
$g->get('foo.bar.baz'); // returns true
$g->get('Foo\Bar\Baz'); // returns the StdClass instance
Gravity works entirely within your local filesystem. When you call the pull()
method, Gravity will find the current project's root directory; search it, as well as your vendor
directory, for Gravity files; load each file's service and setting definitions into the manager; and, return the manager to you. That's it!
Gravity's documentation is available online or in the docs
directory.
We strive to maintain great documentation. If you see a mistake or have a suggestion, feel free to fork and fix it!
Gravity requires PHP 7.2+.
Gravity is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX.
Gravity must be installed via Composer. To do so, add the following line to the require
section of your composer.json
file (where x
is the latest major version), and run composer update
:
{
"require": {
"jstewmc/gravity": "^x"
}
}
You can run most examples in the documentation by cloning the repository to your computer, navigating to it on your filesystem, and using the PHP command line.
# navigate to a directory on your computer
$ cd ~/projects
# clone the repository to your computer
~/projects $ git clone https://github.com/jstewmc/gravity.git
# navigate to the repository
~/projects $ cd gravity
# run the first example
~/projects/gravity $ php examples/first.php
Most examples use assert()
statements, and will output nothing when successful, unless stated otherwise.
This library strives to adhere to the following standards:
If you spot an error, please let us know!
This library is licensed under the MIT license.
This library was originally developed by Jack Clayton with input from good friends like Andy O'brien and Harry Wallin.
We hope you enjoy it!