https://github.com/oktopost/skeleton
https://github.com/oktopost/skeleton
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/oktopost/skeleton
- Owner: Oktopost
- License: mit
- Created: 2016-02-21T09:10:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-06-04T07:48:20.000Z (about 3 years ago)
- Last Synced: 2025-03-08T04:20:03.127Z (over 1 year ago)
- Language: PHP
- Size: 227 KB
- Stars: 0
- Watchers: 12
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/Oktopost/Skeleton)
# Skeleton
> Latest version 1.2.0
Skeleton is an [Inversion of Control (IoC)](https://en.wikipedia.org/wiki/Inversion_of_control) Library for PHP 7.1 or higher.
[](https://travis-ci.org/Oktopost/Skeleton)
- [Simple example project](https://github.com/Oktopost/Example-Skeleton)
## Installation
```shell
composer require oktopost/skeleton
```
or inside *composer.json*
```json
"require": {
"oktopost/skeleton": "^1.0"
}
```
## Basic Usage Example:
```php
// src/Proj/Base/IUserDAO.php
interface IUserDAO
{
public function load($id);
}
// src/Proj/DAO/UserDAO.php
class UserDAO implements IUserDAO
{
public function load($id)
{
// ...
}
}
// skeleton-config.php
$skeleton = new \Skeleton\Skeleton();
$skeleton->set(Proj\Base\IUserDAO::class, Proj\DAO\UserDAO::class);
// or
$skeleton->set("Using any string as key", Proj\DAO\UserDAO::class);
// Obtaining a new instance using
$service = $skeleton->get(Proj\DAO\IUserDAO::class);
// or
$service = $skeleton->get("Using any string as key");
```
In this case, **$service** will be set to a new instance of the **UserDAO** class that was created by Skeleton.
## Autoloading class
Given the following setup:
```php
// src/Proj/Base/IUserDAO.php
interface IUserDAO {}
// src/Proj/Base/IUserService.php
interface IUserService {}
// src/Proj/DAO/UserDAO.php
class UserDAO implements IUserDAO {}
// skeleton-config.php
$skeleton = new \Skeleton\Skeleton();
$skeleton->set(Proj\Base\IUserDAO::class, Proj\DAO\UserDAO::class);
$skeleton->set(Proj\Base\IUserService::class, Proj\Service\UserService::class);
```
Instance of **UserService** may be obtained *without* autoloading using:
```php
// src/Proj/Service/UserService.php
class UserService implements IUserService
{
public function setUserDAO(IUserDAO $dao)
{
}
}
$instance = $skeleton->get(IUserService::class);
$instance->setUserDAO($skeleton->get(IUserDAO::class));
```
But with autoloading you can omit the call to setUserDAO using one of the following.
### Using setter methods autolaoding
```php
// skeleton-config.php
$skeleton->enableKnot();
// src/Proj/Service/UserService.php
/**
* @autoload
*/
class UserService implements IUserService
{
/**
* @autoload
* Method must start with the word "set", have only one parameter and the @autoload annotation.
* Private and protected methods will be also autoloaded.
*/
public function setUserDAO(IUserDAO $dao)
{
}
}
// example.php
$instance = $skeleton->get(IUserService::class);
```
### Using data member autoloading.
```php
// skeleton-config.php
$skeleton->enableKnot();
// src/Proj/Service/UserService.php
/**
* @autoload
*/
class UserService implements IUserService
{
/**
* @autoload
* @var \Full\Path\To\IUserDAO
* Important: Full path must be defined under the @var annotation.
*/
private $dao;
}
// example.php
$instance = $skeleton->get(IUserService::class);
```
### Using \__construct autoloading.
In this case the *autoload* annotation is not required for the class name nor for the \__construct method.
```php
// skeleton-config.php
$skeleton->enableKnot();
// src/Proj/Service/UserService.php
class UserService implements IUserService
{
public function __construct(IUserDAO $dao)
{
}
}
// example.php
$instance = $skeleton->get(IUserService::class);
```