Skip to content

Commit ca4eae7

Browse files
committed
import
0 parents  commit ca4eae7

24 files changed

Lines changed: 1563 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Blocking Component
2+
==================
3+
The Locking Component provides methods to manager content based blocking.
4+
5+
```php
6+
<?php
7+
8+
use Brainbits\Blocking\Blocker;
9+
use Brainbits\Blocking\Adapter\FilesystemAdapter;
10+
use Brainbits\Blocking\Owner\SessionOwner;
11+
12+
$adapter = new FilesystemAdapter('/where/to/store/blocks' /* path to directory on filesystem */);
13+
$owner = new SessionOwner($session /* symfony session */);
14+
$validator = new ExpiredValidator(300 /* block will expire after 300 seconds */);
15+
16+
$blocker = new Blocker($adapter, $owner, $validator);
17+
18+
$identifier = new Identifer('myContent', 123);
19+
20+
$block = $blocker->block($identifier);
21+
$result = $blocker->unblock($identifier);
22+
$result = $blocker->isBlocked($identifier);
23+
$block = $blocker->getBlock($identifier);
24+
```
25+
26+
Blocking Adapters
27+
-----------------
28+
Blocking adapters are used to store the block information.
29+
30+
A file based blocking adapter is provided.
31+
It writes block-files to the filesystem, based on the blocking identifier.
32+
33+
Blocking Identifiers
34+
--------------------
35+
Blocking identifiers are used to identify the content that is being blocked.
36+
37+
A general purpose blocking identifier is provided, that uses content type and
38+
content id to create an identifier string.
39+
40+
Blocking Owners
41+
---------------
42+
Blocking owners are used to identify the user that created the block.
43+
44+
A symfony session based owner class is provided.
45+
46+
Blocking Validators
47+
-------------------
48+
Blocking validators test wether or not an existing block is still valid.
49+
50+
A validator that checks a block via last modification time is provided.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "brainbits/blocking",
3+
"description": "Object blocking",
4+
"keywords": [],
5+
"homepage": "http://brainbits.net",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Stephan Wentz",
10+
"email": "swentz@brainbits.net"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0",
15+
"mikey179/vfsStream": "1.1.*",
16+
"symfony/http-foundation": "2.1.*"
17+
},
18+
"autoload": {
19+
"psr-0": { "Brainbits": "src/" }
20+
},
21+
"minimum-stability": "stable"
22+
}

composer.lock

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* This file is part of the brainbits block package.
4+
*
5+
* @copyright 2012 brainbits GmbH (http://www.brainbits.net)
6+
* @license http://www.makeweb.de/LICENCE Dummy Licence
7+
*/
8+
9+
namespace Brainbits\Blocking\Adapter;
10+
11+
use Brainbits\Blocking\Identifier\IdentifierInterface;
12+
use Brainbits\Blocking\BlockInterface;
13+
14+
/**
15+
* Block adapter interface
16+
*
17+
* @author Stephan Wentz <sw@brainbits.net>
18+
*/
19+
interface AdapterInterface
20+
{
21+
/**
22+
* Write block
23+
*
24+
* @param BlockInterface $block
25+
* @return boolean
26+
*/
27+
public function write(BlockInterface $block);
28+
29+
/**
30+
* Touch block
31+
*
32+
* @param BlockInterface $block
33+
* @return boolean
34+
*/
35+
public function touch(BlockInterface $block);
36+
37+
/**
38+
* Remove block
39+
*
40+
* @param BlockInterface $block
41+
* @return boolean
42+
*/
43+
public function remove(BlockInterface $block);
44+
45+
/**
46+
* Is a block set?
47+
*
48+
* @param BlockInterface $block
49+
* @return boolean
50+
*/
51+
public function exists(IdentifierInterface $identifier);
52+
53+
/**
54+
* Return block
55+
*
56+
* @param BlockInterface $block
57+
* @return BlockInterface
58+
*/
59+
public function get(IdentifierInterface $identifier);
60+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* This file is part of the brainbits block package.
4+
*
5+
* @copyright 2012 brainbits GmbH (http://www.brainbits.net)
6+
* @license http://www.makeweb.de/LICENCE Dummy Licence
7+
*/
8+
9+
namespace Brainbits\Blocking\Adapter;
10+
11+
use Brainbits\Blocking\Identifier\IdentifierInterface;
12+
use Brainbits\Blocking\BlockInterface;
13+
use Brainbits\Blocking\Block;
14+
15+
/**
16+
* File block adapter
17+
* Uses files for storing block information
18+
*
19+
* @author Stephan Wentz <sw@brainbits.net>
20+
*/
21+
class FileAdapter implements AdapterInterface
22+
{
23+
/**
24+
* @var string
25+
*/
26+
protected $root;
27+
28+
/**
29+
* @param string $root
30+
*/
31+
public function __construct($root)
32+
{
33+
$this->root = $root;
34+
}
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function write(BlockInterface $block)
40+
{
41+
file_put_contents(
42+
$this->getFilename($block->getIdentifier()),
43+
serialize($block)
44+
);
45+
46+
return true;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function touch(BlockInterface $block)
53+
{
54+
$filename = $this->getFilename($block->getIdentifier());
55+
touch($filename);
56+
$updatedAt = new \DateTime();
57+
$updatedAt->setTimestamp(filemtime($filename));
58+
$block->setUpdatedAt($updatedAt);
59+
60+
return true;
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function remove(BlockInterface $block)
67+
{
68+
if (!$this->exists($block->getIdentifier()))
69+
{
70+
return false;
71+
}
72+
73+
unlink($this->getFilename($block->getIdentifier()));
74+
75+
return true;
76+
}
77+
78+
/**
79+
* @inheritDoc
80+
*/
81+
public function exists(IdentifierInterface $identifier)
82+
{
83+
return file_exists($this->getFilename($identifier));
84+
}
85+
86+
/**
87+
* @inheritDoc
88+
*/
89+
public function get(IdentifierInterface $identifier)
90+
{
91+
if (!$this->exists($identifier))
92+
{
93+
return null;
94+
}
95+
96+
$filename = $this->getFilename($identifier);
97+
$content = file_get_contents($filename);
98+
$updatedAt = new \DateTime();
99+
$updatedAt->setTimestamp(filemtime($filename));
100+
$block = unserialize($content);
101+
$block->setUpdatedAt($updatedAt);
102+
103+
return $block;
104+
}
105+
106+
/**
107+
* Return assembled filename
108+
*
109+
* @param IdentifierInterface $identifier
110+
* @return string
111+
*/
112+
protected function getFilename(IdentifierInterface $identifier)
113+
{
114+
return $this->root . '/' . $identifier;
115+
}
116+
}

0 commit comments

Comments
 (0)