marijn / spil
此包最新版本(dev-master)没有提供许可证信息。
创建锁的有限状态机
dev-master
2012-06-27 12:21 UTC
Requires
- php: >=5.3.2
This package is not auto-updated.
Last update: 2024-09-14 12:27:38 UTC
README
这大部分是一个实验。然而,如果您觉得这对你有所帮助,请告诉我。-- Marijn
这是一个锁的基本有限状态机实现。实现灵感来源于Stackoverflow上Gordon的评论。
我可能过度命名空间了。你可能想查看之前的版本。对于代码或功能有任何想法都欢迎提出。
用法
简单的锁
想法是创建一个锁,并将状态作为参数传递。
<?php $secret = '$3crt3t'; // Create a Lock that is locked $lock = new Spil\Lock(new Spil\LockState\LockedState(), $secret); // Try to unlock it with a key that won't fit try { $lock->unlock(new Spil\Key("invalid")); } catch (DomainException $e) { printf("Error: %s", $e->getMessage()); } // Create a "fitting" key $key = new Spil\Key($secret); if ($lock->unlock($key)) { print("Welcome!"); } // Try to unlock what is already unlocked try { $lock->unlock($key); } catch (DomainException $e) { printf("Error: %s", $e->getMessage()); }
计时锁
由于灵活的架构,我们可以轻松创建基于时间的锁。
<?php $secret = '$3crt3t'; // Create a Lock that is locked $lock = new Spil\Lock(new Spil\LockState\TemporalLockedState(new Spil\DateRange(new DateTime('yesterday morning'), new DateTime('yesterday noon'))), $secret); // Create a "fitting" key $key = new Spil\Key($secret); // Try to unlock (outside of the created timeframe) try { $lock->unlock($key); } catch (DomainException $e) { printf("Error: %s", $e->getMessage()); }
工厂
提供了一个工厂类,以简化锁的创建。
<?php $secret = '$3crt3t'; $factory = new Spil\LockFactory(); // Create a Lock that is locked $lock = $factory->createTemporalLockedLock($secret, new DateTime('yesterday morning'), new DateTime('yesterday noon')))); // Create a "fitting" key $key = new Spil\Key($secret); // Try to unlock (outside of the created timeframe) try { $lock->unlock($key); } catch (DomainException $e) { printf("Error: %s", $e->getMessage()); }