Skip to content

Commit 55ece67

Browse files
committed
initial commit
0 parents  commit 55ece67

4 files changed

Lines changed: 200 additions & 0 deletions

File tree

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
thumbs.db
3+
*.class
4+
*.pyc
5+
*.pyo
6+
*.swp
7+
.project
8+
.settings
9+
.idea
10+
composer.lock
11+
composer.phar
12+
vendor/*

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
PHP Resource Locator
2+
====================
3+
4+
Example
5+
-------
6+
```php
7+
<?php
8+
9+
use frostealth\Locator\Locator;
10+
11+
$locator = new Locator();
12+
13+
// ...
14+
15+
// injecting simple values
16+
$locator->set('foo', 'bar'); // or $locator->foo = 'bar';
17+
18+
// get its value
19+
$value = $locator->get('foo'); // or $value = $locator->foo;
20+
21+
// ...
22+
23+
// resources
24+
$locator->set('object', function ($locator) {
25+
return new MyObject($locator->foo);
26+
});
27+
28+
// get a new instance
29+
$object = $locator->get('object');
30+
31+
// ...
32+
33+
// singleton resources
34+
$locator->singleton('log', function ($locator) {
35+
return new MyLog($locator->object);
36+
});
37+
38+
// get log resource
39+
$log = $locator->get('log');
40+
```
41+
42+
Note
43+
----
44+
[Use Locator as singleton](https://github.com/frostealth/php-singleton-trait)
45+
46+
Requirements
47+
------------
48+
* PHP >= 5.4
49+
* [PHP Data Storage v1.0](https://github.com/frostealth/php-data-storage/releases)

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "frostealth/php-resource-locator",
3+
"description": "PHP Resource Locator",
4+
"version" : "1.0.0",
5+
"authors": [
6+
{
7+
"name": "Kudinov Ivan",
8+
"email": "frostealth@gmail.com"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"frostealth\\Locator\\": "src/"
14+
}
15+
},
16+
"minimum-stability": "dev",
17+
"require": {
18+
"php": ">=5.4.0",
19+
"frostealth/php-data-storage": "1.0.*"
20+
}
21+
}

src/Locator.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace frostealth\Locator;
4+
5+
use frostealth\Storage\Data;
6+
7+
/**
8+
* Class Locator
9+
*
10+
* @package frostealth\Locator
11+
*/
12+
class Locator
13+
{
14+
/** @var Data */
15+
protected $container;
16+
17+
public function __construct()
18+
{
19+
$this->container = new Data();
20+
}
21+
22+
/**
23+
* @param string $name
24+
*
25+
* @return mixed
26+
*/
27+
public function get($name)
28+
{
29+
$value = $this->container->get($name);
30+
if (is_callable($value)) {
31+
$value = $value($this);
32+
}
33+
34+
return $value;
35+
}
36+
37+
/**
38+
* @param string $name
39+
* @param mixed $value
40+
*/
41+
public function set($name, $value)
42+
{
43+
$this->container->set($name, $value);
44+
}
45+
46+
/**
47+
* @param string $name
48+
*
49+
* @return bool
50+
*/
51+
public function has($name)
52+
{
53+
return $this->container->has($name);
54+
}
55+
56+
/**
57+
* @param string $name
58+
*/
59+
public function remove($name)
60+
{
61+
$this->container->remove($name);
62+
}
63+
64+
/**
65+
* @param string $name
66+
* @param callable $value
67+
*/
68+
public function singleton($name, \Closure $value)
69+
{
70+
$this->container->set($name, function () use ($value) {
71+
static $instance = null;
72+
73+
if ($instance === null) {
74+
$instance = $value($this);
75+
}
76+
77+
return $instance;
78+
});
79+
}
80+
81+
/**
82+
* @param string $name
83+
*
84+
* @return mixed
85+
*/
86+
public function __get($name)
87+
{
88+
return $this->get($name);
89+
}
90+
91+
/**
92+
* @param string $name
93+
* @param mixed $value
94+
*/
95+
public function __set($name, $value)
96+
{
97+
$this->set($name, $value);
98+
}
99+
100+
/**
101+
* @param string $name
102+
*
103+
* @return bool
104+
*/
105+
public function __isset($name)
106+
{
107+
return $this->has($name);
108+
}
109+
110+
/**
111+
* @param string $name
112+
*/
113+
public function __unset($name)
114+
{
115+
$this->remove($name);
116+
}
117+
}
118+

0 commit comments

Comments
 (0)