From d995344751ef88e112ea10cba509bcc652ff3135 Mon Sep 17 00:00:00 2001 From: iwyg Date: Sun, 21 Feb 2016 18:02:16 +0100 Subject: [PATCH] merge d422d9df97fa --- lucid/cache/.coveralls.yml => .coveralls.yml | 0 .gitignore | 3 - lucid/cache/.travis.yml => .travis.yml | 2 +- LICENSE.md | 6 +- README.md | 150 +- build/publish | 108 -- composer.json | 67 +- composer.lock | 1238 ----------------- lucid/cache/.phpunit | 7 - lucid/cache/LICENSE.md | 7 - lucid/cache/README.md | 43 - lucid/cache/composer.json | 25 - lucid/cache/phpunit.xml.dist | 26 - lucid/cache/src/.travis.yml | 25 - lucid/cache/src/.travis_install.sh | 32 - lucid/cache/src/CacheInterface.php | 132 -- lucid/cache/src/Client/AbstractClient.php | 183 --- lucid/cache/src/Client/AbstractSqlClient.php | 47 - lucid/cache/src/Client/Apcu.php | 96 -- .../cache/src/Client/ConnectionInterface.php | 58 - lucid/cache/src/Client/Filesystem.php | 306 ---- lucid/cache/src/Client/InMemory.php | 158 --- lucid/cache/src/Client/Memcache.php | 79 -- lucid/cache/src/Client/MemcacheConnection.php | 125 -- lucid/cache/src/Client/Memcached.php | 122 -- .../cache/src/Client/MemcachedConnection.php | 112 -- lucid/cache/src/Client/Redis.php | 141 -- lucid/cache/src/Client/Xcache.php | 115 -- lucid/cache/src/ClientInterface.php | 125 -- lucid/cache/src/Driver/AbstractDriver.php | 189 --- lucid/cache/src/Driver/AbstractSqlDriver.php | 54 - lucid/cache/src/Driver/ApcDriver.php | 120 -- lucid/cache/src/Driver/ApcuDriver.php | 95 -- lucid/cache/src/Driver/ArrayDriver.php | 170 --- .../cache/src/Driver/ConnectionInterface.php | 58 - lucid/cache/src/Driver/DriverInterface.php | 125 -- lucid/cache/src/Driver/FilesystemDriver.php | 269 ---- lucid/cache/src/Driver/MemcacheConnection.php | 125 -- lucid/cache/src/Driver/MemcacheDriver.php | 80 -- .../cache/src/Driver/MemcachedConnection.php | 112 -- lucid/cache/src/Driver/MemcachedDriver.php | 131 -- lucid/cache/src/Driver/RedisDriver.php | 141 -- lucid/cache/src/Driver/XcacheDriver.php | 113 -- lucid/cache/src/Section.php | 173 --- lucid/cache/src/Storage.php | 243 ---- lucid/cache/src/Util/Time.php | 36 - .../cache/tests/Client/AbstractClientTest.php | 134 -- lucid/cache/tests/Client/ApcuTest.php | 75 - lucid/cache/tests/Client/FilesystemTest.php | 127 -- lucid/cache/tests/Client/MemcacheTest.php | 142 -- lucid/cache/tests/Client/MemcachedTest.php | 230 --- lucid/cache/tests/Client/RedisTest.php | 133 -- lucid/cache/tests/Client/XcacheTest.php | 68 - lucid/cache/tests/Driver/ApcDriverTest.php | 75 - lucid/cache/tests/Driver/ApcuDriverTest.php | 42 - lucid/cache/tests/Driver/DriverTest.php | 134 -- .../cache/tests/Driver/MemcacheDriverTest.php | 141 -- .../tests/Driver/MemcachedDriverTest.php | 229 --- lucid/cache/tests/Driver/RedisDriverTest.php | 133 -- lucid/cache/tests/Driver/XcacheDriverTest.php | 64 - lucid/cache/tests/Fixures/apchelper.php | 84 -- lucid/cache/tests/Fixures/apcuhelper.php | 85 -- lucid/cache/tests/Fixures/helper.php | 21 - lucid/cache/tests/Fixures/xcachehelper.php | 75 - lucid/cache/tests/SectionTest.php | 129 -- lucid/cache/tests/StorageTest.php | 85 -- lucid/cache/tests/Stubs/Memcached.php | 23 - lucid/common/.coveralls.yml | 2 - lucid/common/.phpunit | 7 - lucid/common/.travis.yml | 38 - lucid/common/LICENSE.md | 7 - lucid/common/README.md | 21 - lucid/common/composer.json | 29 - lucid/common/phpunit.xml.dist | 26 - lucid/common/src/Contract/Arrayable.php | 24 - lucid/common/src/Contract/Cachable.php | 36 - lucid/common/src/Contract/Jsonable.php | 24 - lucid/common/src/Helper/Arr.php | 240 ---- lucid/common/src/Helper/Str.php | 174 --- lucid/common/src/Struct/Items.php | 191 --- lucid/common/src/Struct/ListInterface.php | 94 -- lucid/common/src/Struct/PriorityQueue.php | 52 - .../src/Struct/ReversePriorityQueue.php | 38 - lucid/common/src/Traits/Getter.php | 90 -- lucid/common/tests/Helper/ArrTest.php | 210 --- lucid/common/tests/Helper/StrTest.php | 92 -- lucid/common/tests/Struct/ItemsTest.php | 173 --- .../common/tests/Struct/PriorityQueueTest.php | 42 - .../tests/Struct/ReversePriorityQueueTest.php | 42 - lucid/common/tests/Traits/GetterTest.php | 61 - lucid/hash/src/HashBcrypt.php | 57 - lucid/hash/src/HashInterface.php | 42 - lucid/hash/src/HashKey.php | 111 -- lucid/hash/src/Helper/BcConvertHelper.php | 100 -- lucid/hash/tests/HashBcryptTest.php | 30 - lucid/hash/tests/HashKeyTest.php | 26 - lucid/hash/tests/HashTestCase.php | 34 - lucid/resource/.coveralls.yml | 2 - lucid/resource/.travis.yml | 36 - lucid/resource/LICENSE.md | 7 - lucid/resource/README.md | 77 - lucid/resource/composer.json | 29 - lucid/resource/phpunit.xml.dist | 26 - lucid/resource/src/AbstractResource.php | 82 -- .../src/Cache/ResourceCacheInterface.php | 47 - lucid/resource/src/Collection.php | 143 -- lucid/resource/src/CollectionInterface.php | 67 - .../src/Exception/LoaderException.php | 36 - lucid/resource/src/FileResource.php | 34 - .../src/Loader/AbstractFileLoader.php | 65 - lucid/resource/src/Loader/AbstractLoader.php | 157 --- lucid/resource/src/Loader/ChainedLoader.php | 139 -- .../resource/src/Loader/ListenerInterface.php | 31 - lucid/resource/src/Loader/LoaderInterface.php | 77 - lucid/resource/src/Loader/Resolver.php | 72 - .../resource/src/Loader/ResolverInterface.php | 47 - lucid/resource/src/Locator.php | 151 -- lucid/resource/src/LocatorInterface.php | 69 - lucid/resource/src/ObjectResource.php | 62 - lucid/resource/src/ResourceInterface.php | 54 - lucid/resource/tests/CollectionTest.php | 137 -- lucid/resource/tests/FileResourceTest.php | 90 -- lucid/resource/tests/Fixures/loc_a/file.txt | 0 lucid/resource/tests/Fixures/loc_b/file.txt | 0 .../tests/Loader/AbstractFileLoaderTest.php | 163 --- .../tests/Loader/AbstractLoaderTest.php | 70 - .../tests/Loader/ChainedLoaderTest.php | 164 --- lucid/resource/tests/Loader/ResolverTest.php | 75 - lucid/resource/tests/LocatorTest.php | 128 -- lucid/resource/tests/ObjectResourceTest.php | 98 -- lucid/resource/tests/Stubs/PhpFileLoader.php | 33 - lucid/signal/.coveralls.yml | 2 - lucid/signal/.phpunit | 7 - lucid/signal/.travis.yml | 36 - lucid/signal/README.md | 108 -- lucid/signal/composer.json | 33 - lucid/signal/phpunit.xml.dist | 26 - lucid/signal/src/.coveralls.yml | 3 - lucid/signal/src/.travis.yml | 23 - lucid/signal/src/ChainedEvent.php | 41 - lucid/signal/src/ChainedEventInterface.php | 38 - lucid/signal/src/ContainerAwareDispatcher.php | 72 - lucid/signal/src/Event.php | 34 - lucid/signal/src/EventDispatcher.php | 205 --- lucid/signal/src/EventDispatcherInterface.php | 107 -- lucid/signal/src/EventInterface.php | 59 - lucid/signal/src/EventName.php | 91 -- lucid/signal/src/EventTrait.php | 79 -- lucid/signal/src/HandlerInterface.php | 31 - lucid/signal/src/Priority.php | 149 -- lucid/signal/src/PriorityInterface.php | 74 - lucid/signal/src/SubscriberInterface.php | 29 - lucid/signal/src/Subscription.php | 45 - lucid/signal/src/SubscriptionInterface.php | 29 - lucid/signal/tests/ChainedEventTest.php | 42 - .../tests/ContainerAwareDispatcherTest.php | 82 -- lucid/signal/tests/EventDispatcherTest.php | 260 ---- lucid/signal/tests/EventNameTest.php | 58 - lucid/signal/tests/EventTest.php | 53 - lucid/signal/tests/PriorityTest.php | 122 -- lucid/signal/tests/Stubs/SimpleSubscriber.php | 48 - lucid/signal/tests/SubscriptionTest.php | 28 - lucid/template/.coveralls.yml | 2 - lucid/template/.phpunit | 7 - lucid/template/.travis.yml | 38 - lucid/template/LICENSE.md | 7 - lucid/template/README.md | 126 -- lucid/template/composer.json | 30 - lucid/template/phpunit.xml.dist | 26 - lucid/template/src/.coveralls.yml | 3 - lucid/template/src/.travis.yml | 23 - lucid/template/src/AbstractPhpEngine.php | 233 ---- lucid/template/src/Data/Data.php | 122 -- .../src/Data/TemplateDataInterface.php | 56 - lucid/template/src/DelegatingEngine.php | 138 -- lucid/template/src/DisplayInterface.php | 32 - lucid/template/src/Engine.php | 528 ------- lucid/template/src/EngineInterface.php | 40 - .../src/Exception/LoaderException.php | 41 - .../src/Exception/RenderException.php | 39 - .../src/Exception/TemplateException.php | 23 - .../src/Extension/AbstractExtension.php | 42 - .../src/Extension/ExtensionInterface.php | 49 - .../src/Extension/FunctionInterface.php | 74 - .../src/Extension/PhpEngineExtension.php | 50 - .../src/Extension/RoutingExtension.php | 59 - .../src/Extension/TemplateFunction.php | 121 -- lucid/template/src/Identity.php | 77 - lucid/template/src/IdentityInterface.php | 45 - lucid/template/src/IdentityParser.php | 53 - .../template/src/IdentityParserInterface.php | 31 - .../src/Listener/ListenerInterface.php | 33 - .../template/src/Loader/FilesystemLoader.php | 88 -- lucid/template/src/Loader/LoaderInterface.php | 42 - .../template/src/Loader/LoggerAwareLoader.php | 74 - lucid/template/src/PhpRenderInterface.php | 73 - lucid/template/src/RenderEngineDecorator.php | 77 - lucid/template/src/RenderInterface.php | 32 - .../src/Resource/AbstractResource.php | 34 - lucid/template/src/Resource/FileResource.php | 55 - .../src/Resource/ResourceInterface.php | 33 - .../template/src/Resource/StringResource.php | 50 - lucid/template/src/Section.php | 82 -- lucid/template/src/Traits/ViewAwareTrait.php | 50 - lucid/template/src/View.php | 238 ---- lucid/template/src/ViewAwareInterface.php | 38 - lucid/template/src/ViewManagerInterface.php | 58 - lucid/template/src/composer.json | 21 - lucid/template/tests/Data/DataTest.php | 67 - lucid/template/tests/DelegatingEngineTest.php | 142 -- lucid/template/tests/EngineTest.php | 270 ---- .../Extension/PhpEngineExtensionTest.php | 84 -- lucid/template/tests/Fixures/view/content.php | 5 - lucid/template/tests/Fixures/view/error.php | 1 - lucid/template/tests/Fixures/view/func.0.php | 3 - lucid/template/tests/Fixures/view/func.1.php | 3 - .../template/tests/Fixures/view/include.0.php | 10 - .../template/tests/Fixures/view/include.1.php | 10 - lucid/template/tests/Fixures/view/index.0.php | 11 - lucid/template/tests/Fixures/view/index.1.php | 10 - lucid/template/tests/Fixures/view/index.2.php | 12 - lucid/template/tests/Fixures/view/index.php | 9 - .../tests/Fixures/view/partials/extend.0.php | 5 - .../tests/Fixures/view/partials/extend.1.php | 2 - .../tests/Fixures/view/partials/extend.2.php | 4 - .../tests/Fixures/view/partials/footer.php | 0 .../tests/Fixures/view/partials/header.php | 1 - .../tests/Fixures/view/partials/include.0.php | 1 - .../tests/Fixures/view/partials/include.1.php | 1 - lucid/template/tests/IdentityParserTest.php | 53 - lucid/template/tests/IdentityTest.php | 39 - .../template/tests/Listener/ListenerTest.php | 33 - .../tests/Loader/FilesystemLoaderTest.php | 100 -- .../tests/Loader/LoggerAwareLoaderTest.php | 90 -- .../tests/RenderEngineDecoratorTest.php | 75 - lucid/template/tests/RenderTest.php | 109 -- .../tests/Resource/FileResourceTest.php | 44 - .../tests/Resource/StringResourceTest.php | 44 - lucid/template/tests/SectionTest.php | 98 -- lucid/template/tests/Stubs/Displayable.php | 27 - lucid/template/tests/Stubs/Listener.php | 39 - lucid/template/tests/ViewTest.php | 197 --- lucid/writer/.coveralls.yml | 2 - lucid/writer/.travis.yml | 36 - lucid/writer/LICENSE.md | 7 - lucid/writer/README.md | 419 ------ lucid/writer/composer.json | 30 - lucid/writer/phpunit.xml.dist | 26 - lucid/writer/src/File/JsonGenerator.php | 88 -- lucid/writer/src/File/PhpGenerator.php | 76 - lucid/writer/src/FormatterHelper.php | 105 -- lucid/writer/src/GeneratorInterface.php | 39 - lucid/writer/src/Object/AbstractBlock.php | 108 -- lucid/writer/src/Object/AbstractWriter.php | 584 -------- lucid/writer/src/Object/Annotateable.php | 109 -- lucid/writer/src/Object/Argument.php | 161 --- lucid/writer/src/Object/ClassWriter.php | 176 --- lucid/writer/src/Object/Constant.php | 93 -- lucid/writer/src/Object/DocBlock.php | 358 ----- lucid/writer/src/Object/ImportHelper.php | 53 - lucid/writer/src/Object/ImportInterface.php | 31 - lucid/writer/src/Object/ImportResolver.php | 232 --- lucid/writer/src/Object/InterfaceMethod.php | 74 - lucid/writer/src/Object/InterfaceWriter.php | 246 ---- lucid/writer/src/Object/MemberInterface.php | 34 - lucid/writer/src/Object/Method.php | 305 ---- lucid/writer/src/Object/MethodInterface.php | 59 - lucid/writer/src/Object/Property.php | 161 --- .../src/Object/TraitAwareWriterHelper.php | 285 ---- lucid/writer/src/Object/TraitWriter.php | 97 -- .../src/Object/VisibilityHelperTrait.php | 52 - lucid/writer/src/Stringable.php | 30 - lucid/writer/src/Writer.php | 341 ----- lucid/writer/src/WriterInterface.php | 103 -- lucid/writer/tests/File/JsonGeneratorTest.php | 33 - lucid/writer/tests/File/PhpGeneratorTest.php | 19 - lucid/writer/tests/FormatterHelperTest.php | 80 -- .../tests/Object/AbstractWriterTest.php | 67 - lucid/writer/tests/Object/ArgumentTest.php | 103 -- lucid/writer/tests/Object/ClassWriterTest.php | 196 --- .../writer/tests/Object/CommentBlockTest.php | 33 - lucid/writer/tests/Object/ConstantTest.php | 29 - lucid/writer/tests/Object/DocBlockTest.php | 199 --- lucid/writer/tests/Object/Fixures/class.0.php | 12 - lucid/writer/tests/Object/Fixures/class.1.php | 15 - .../writer/tests/Object/Fixures/class.2.1.php | 20 - lucid/writer/tests/Object/Fixures/class.2.php | 17 - lucid/writer/tests/Object/Fixures/class.3.php | 15 - lucid/writer/tests/Object/Fixures/class.4.php | 18 - lucid/writer/tests/Object/Fixures/class.5.php | 10 - .../tests/Object/Fixures/class.test.php | 12 - .../tests/Object/Fixures/interface.0.php | 11 - .../tests/Object/Fixures/interface.1.php | 16 - .../tests/Object/Fixures/interface.2.php | 14 - .../tests/Object/Fixures/interface.4.1.php | 26 - .../tests/Object/Fixures/interface.4.php | 15 - lucid/writer/tests/Object/Fixures/trait.0.php | 18 - lucid/writer/tests/Object/Fixures/trait.1.php | 20 - .../tests/Object/ImportResolverTest.php | 110 -- .../tests/Object/InterfaceMethodTest.php | 72 - .../tests/Object/InterfaceWriterTest.php | 152 -- lucid/writer/tests/Object/MethodTest.php | 222 --- lucid/writer/tests/Object/PropertyTest.php | 74 - lucid/writer/tests/Object/TraitWriterTest.php | 95 -- .../Object/VisibilityHelperTraitTest.php | 40 - lucid/writer/tests/WriterTest.php | 190 --- lucid/xml/.coveralls.yml | 2 - lucid/xml/.travis.yml | 38 - lucid/xml/LICENSE.md | 7 - lucid/xml/README.md | 389 ------ lucid/xml/composer.json | 30 - lucid/xml/phpunit.xml.dist | 26 - lucid/xml/src/Dom/DOMDocument.php | 136 -- lucid/xml/src/Dom/DOMElement.php | 56 - .../xml/src/Inflector/InflectorInterface.php | 40 - lucid/xml/src/Inflector/SimpleInflector.php | 203 --- lucid/xml/src/Loader/Loader.php | 53 - lucid/xml/src/Loader/LoaderInterface.php | 83 -- lucid/xml/src/Loader/LoaderTrait.php | 127 -- lucid/xml/src/Normalizer/Normalizer.php | 421 ------ .../src/Normalizer/NormalizerInterface.php | 50 - lucid/xml/src/Parser.php | 568 -------- lucid/xml/src/ParserInterface.php | 52 - lucid/xml/src/SimpleXMLElement.php | 156 --- lucid/xml/src/Traits/XmlHelperTrait.php | 47 - lucid/xml/src/Writer.php | 620 --------- lucid/xml/tests/Dom/DomDocumentTest.php | 91 -- lucid/xml/tests/Dom/DomElementTest.php | 61 - lucid/xml/tests/Fixures/test.xml | 4 - .../tests/Inflector/SimpleInflectorTest.php | 78 -- lucid/xml/tests/Loader/LoaderTest.php | 118 -- lucid/xml/tests/Normalizer/NormalizerTest.php | 220 --- .../tests/Normalizer/Stubs/ArrayableStub.php | 29 - .../Normalizer/Stubs/ConvertToArrayStub.php | 84 -- .../Normalizer/Stubs/NestedPropertyStub.php | 26 - .../Normalizer/Stubs/SinglePropertyStub.php | 17 - .../Normalizer/Stubs/TraversableStub.php | 32 - lucid/xml/tests/ParserTest.php | 367 ----- lucid/xml/tests/SimpleXMLElementTest.php | 136 -- lucid/xml/tests/WriterTest.php | 538 ------- phpunit.xml.dist | 46 +- src/.gitignore | 1 + src/Exception/MatchException.php | 27 + src/Exception/MissingValueException.php | 25 + src/Exception/ParserException.php | 29 + .../Exception/ResolverException.php | 12 +- .../ContainerAwareResolverInterface.php | 33 + src/Handler/Dispatcher.php | 53 + src/Handler/DispatcherInterface.php | 33 + .../Handler/ParameterMapperInterface.php | 18 +- src/Handler/ParserInterface.php | 24 + .../Handler/PassParameterMapper.php | 16 +- .../Handler/ReferenceInterface.php | 10 +- src/Handler/Reflector.php | 270 ++++ src/Handler/Resolver.php | 151 ++ .../Handler/ResolverInterface.php | 18 +- src/Handler/StrictParameterMapper.php | 84 ++ src/Handler/TypeMapCollection.php | 87 ++ src/Handler/TypeMapCollectionInterface.php | 67 + src/Handler/TypeMapperInterface.php | 36 + src/Loader/LoaderInterface.php | 24 + src/Loader/PhpLoader.php | 175 +++ src/Matcher/Context.php | 129 ++ src/Matcher/ContextInterface.php | 81 ++ src/Matcher/MatcherTrait.php | 123 ++ src/Matcher/RequestMatcher.php | 76 + src/Matcher/RequestMatcherInterface.php | 49 + .../Parser/Delimiter.php | 12 +- src/Parser/ParserInterface.php | 40 + src/Parser/Standard.php | 296 ++++ .../Client/SqLite.php => src/Parser/Text.php | 12 +- src/Parser/Token.php | 53 + .../Parser/TokenInterface.php | 11 +- src/Parser/Variable.php | 77 + src/Request/Context.php | 141 ++ src/Request/ContextInterface.php | 58 + src/Request/PassResponseMapper.php | 34 + src/Request/ResponseMapperInterface.php | 32 + src/Request/UrlGenerator.php | 332 +++++ src/Request/UrlGeneratorInterface.php | 86 ++ src/Route.php | 251 ++++ src/RouteCollectionBuilder.php | 400 ++++++ src/RouteCollectionInterface.php | 84 ++ src/RouteContext.php | 184 +++ src/RouteContextInterface.php | 77 + src/RouteGroup.php | 122 ++ src/RouteInterface.php | 123 ++ src/Router.php | 253 ++++ src/RouterInterface.php | 99 ++ src/Routes.php | 150 ++ tests/Cache/RoutesTest.php | 59 + tests/Handler/DispatcherTest.php | 64 + tests/Handler/PassParameterMapperTest.php | 38 + tests/Handler/ReflectorTest.php | 117 ++ tests/Handler/ResolverTest.php | 143 ++ tests/Handler/StrictParameterMapperTest.php | 126 ++ tests/Handler/Stubs/AbstractHandler.php | 14 + tests/Handler/Stubs/SimpleHandler.php | 27 + tests/Handler/TypeMapCollectionTest.php | 45 + tests/Loader/Fixures/faulty.php | 3 + tests/Loader/Fixures/route_groups.0.php | 30 + tests/Loader/Fixures/route_groups.1.php | 20 + tests/Loader/Fixures/route_groups.2.php | 21 + tests/Loader/Fixures/routes.0.php | 14 + tests/Loader/PhpLoaderTest.php | 104 ++ tests/Matcher/ContextTest.php | 67 + tests/Matcher/RequestMatcherTest.php | 114 ++ tests/Parser/StandardTest.php | 141 ++ tests/Parser/VariableTest.php | 18 + tests/Request/ContextTest.php | 75 + tests/Request/Fixures/Server.php | 61 + tests/Request/UrlGeneratorTest.php | 117 ++ tests/RouteCollectionBuilderTest.php | 151 ++ tests/RouteContextTest.php | 39 + tests/RouteTest.php | 154 ++ tests/RouterTest.php | 179 +++ tests/RoutesTest.php | 120 ++ 417 files changed, 6765 insertions(+), 30686 deletions(-) rename lucid/cache/.coveralls.yml => .coveralls.yml (100%) delete mode 100644 .gitignore rename lucid/cache/.travis.yml => .travis.yml (94%) delete mode 100755 build/publish delete mode 100644 composer.lock delete mode 100755 lucid/cache/.phpunit delete mode 100644 lucid/cache/LICENSE.md delete mode 100644 lucid/cache/README.md delete mode 100644 lucid/cache/composer.json delete mode 100644 lucid/cache/phpunit.xml.dist delete mode 100644 lucid/cache/src/.travis.yml delete mode 100755 lucid/cache/src/.travis_install.sh delete mode 100644 lucid/cache/src/CacheInterface.php delete mode 100644 lucid/cache/src/Client/AbstractClient.php delete mode 100644 lucid/cache/src/Client/AbstractSqlClient.php delete mode 100644 lucid/cache/src/Client/Apcu.php delete mode 100644 lucid/cache/src/Client/ConnectionInterface.php delete mode 100644 lucid/cache/src/Client/Filesystem.php delete mode 100644 lucid/cache/src/Client/InMemory.php delete mode 100644 lucid/cache/src/Client/Memcache.php delete mode 100644 lucid/cache/src/Client/MemcacheConnection.php delete mode 100644 lucid/cache/src/Client/Memcached.php delete mode 100644 lucid/cache/src/Client/MemcachedConnection.php delete mode 100644 lucid/cache/src/Client/Redis.php delete mode 100644 lucid/cache/src/Client/Xcache.php delete mode 100644 lucid/cache/src/ClientInterface.php delete mode 100644 lucid/cache/src/Driver/AbstractDriver.php delete mode 100644 lucid/cache/src/Driver/AbstractSqlDriver.php delete mode 100644 lucid/cache/src/Driver/ApcDriver.php delete mode 100644 lucid/cache/src/Driver/ApcuDriver.php delete mode 100644 lucid/cache/src/Driver/ArrayDriver.php delete mode 100644 lucid/cache/src/Driver/ConnectionInterface.php delete mode 100644 lucid/cache/src/Driver/DriverInterface.php delete mode 100644 lucid/cache/src/Driver/FilesystemDriver.php delete mode 100644 lucid/cache/src/Driver/MemcacheConnection.php delete mode 100644 lucid/cache/src/Driver/MemcacheDriver.php delete mode 100644 lucid/cache/src/Driver/MemcachedConnection.php delete mode 100644 lucid/cache/src/Driver/MemcachedDriver.php delete mode 100644 lucid/cache/src/Driver/RedisDriver.php delete mode 100644 lucid/cache/src/Driver/XcacheDriver.php delete mode 100644 lucid/cache/src/Section.php delete mode 100644 lucid/cache/src/Storage.php delete mode 100644 lucid/cache/src/Util/Time.php delete mode 100644 lucid/cache/tests/Client/AbstractClientTest.php delete mode 100644 lucid/cache/tests/Client/ApcuTest.php delete mode 100644 lucid/cache/tests/Client/FilesystemTest.php delete mode 100644 lucid/cache/tests/Client/MemcacheTest.php delete mode 100644 lucid/cache/tests/Client/MemcachedTest.php delete mode 100644 lucid/cache/tests/Client/RedisTest.php delete mode 100644 lucid/cache/tests/Client/XcacheTest.php delete mode 100644 lucid/cache/tests/Driver/ApcDriverTest.php delete mode 100644 lucid/cache/tests/Driver/ApcuDriverTest.php delete mode 100644 lucid/cache/tests/Driver/DriverTest.php delete mode 100644 lucid/cache/tests/Driver/MemcacheDriverTest.php delete mode 100644 lucid/cache/tests/Driver/MemcachedDriverTest.php delete mode 100644 lucid/cache/tests/Driver/RedisDriverTest.php delete mode 100644 lucid/cache/tests/Driver/XcacheDriverTest.php delete mode 100644 lucid/cache/tests/Fixures/apchelper.php delete mode 100644 lucid/cache/tests/Fixures/apcuhelper.php delete mode 100644 lucid/cache/tests/Fixures/helper.php delete mode 100644 lucid/cache/tests/Fixures/xcachehelper.php delete mode 100644 lucid/cache/tests/SectionTest.php delete mode 100644 lucid/cache/tests/StorageTest.php delete mode 100644 lucid/cache/tests/Stubs/Memcached.php delete mode 100644 lucid/common/.coveralls.yml delete mode 100755 lucid/common/.phpunit delete mode 100644 lucid/common/.travis.yml delete mode 100644 lucid/common/LICENSE.md delete mode 100644 lucid/common/README.md delete mode 100644 lucid/common/composer.json delete mode 100644 lucid/common/phpunit.xml.dist delete mode 100644 lucid/common/src/Contract/Arrayable.php delete mode 100644 lucid/common/src/Contract/Cachable.php delete mode 100644 lucid/common/src/Contract/Jsonable.php delete mode 100644 lucid/common/src/Helper/Arr.php delete mode 100644 lucid/common/src/Helper/Str.php delete mode 100644 lucid/common/src/Struct/Items.php delete mode 100644 lucid/common/src/Struct/ListInterface.php delete mode 100644 lucid/common/src/Struct/PriorityQueue.php delete mode 100644 lucid/common/src/Struct/ReversePriorityQueue.php delete mode 100644 lucid/common/src/Traits/Getter.php delete mode 100644 lucid/common/tests/Helper/ArrTest.php delete mode 100644 lucid/common/tests/Helper/StrTest.php delete mode 100644 lucid/common/tests/Struct/ItemsTest.php delete mode 100644 lucid/common/tests/Struct/PriorityQueueTest.php delete mode 100644 lucid/common/tests/Struct/ReversePriorityQueueTest.php delete mode 100644 lucid/common/tests/Traits/GetterTest.php delete mode 100644 lucid/hash/src/HashBcrypt.php delete mode 100644 lucid/hash/src/HashInterface.php delete mode 100644 lucid/hash/src/HashKey.php delete mode 100644 lucid/hash/src/Helper/BcConvertHelper.php delete mode 100644 lucid/hash/tests/HashBcryptTest.php delete mode 100644 lucid/hash/tests/HashKeyTest.php delete mode 100644 lucid/hash/tests/HashTestCase.php delete mode 100644 lucid/resource/.coveralls.yml delete mode 100644 lucid/resource/.travis.yml delete mode 100644 lucid/resource/LICENSE.md delete mode 100644 lucid/resource/README.md delete mode 100644 lucid/resource/composer.json delete mode 100644 lucid/resource/phpunit.xml.dist delete mode 100644 lucid/resource/src/AbstractResource.php delete mode 100644 lucid/resource/src/Cache/ResourceCacheInterface.php delete mode 100644 lucid/resource/src/Collection.php delete mode 100644 lucid/resource/src/CollectionInterface.php delete mode 100644 lucid/resource/src/Exception/LoaderException.php delete mode 100644 lucid/resource/src/FileResource.php delete mode 100644 lucid/resource/src/Loader/AbstractFileLoader.php delete mode 100644 lucid/resource/src/Loader/AbstractLoader.php delete mode 100644 lucid/resource/src/Loader/ChainedLoader.php delete mode 100644 lucid/resource/src/Loader/ListenerInterface.php delete mode 100644 lucid/resource/src/Loader/LoaderInterface.php delete mode 100644 lucid/resource/src/Loader/Resolver.php delete mode 100644 lucid/resource/src/Loader/ResolverInterface.php delete mode 100644 lucid/resource/src/Locator.php delete mode 100644 lucid/resource/src/LocatorInterface.php delete mode 100644 lucid/resource/src/ObjectResource.php delete mode 100644 lucid/resource/src/ResourceInterface.php delete mode 100644 lucid/resource/tests/CollectionTest.php delete mode 100644 lucid/resource/tests/FileResourceTest.php delete mode 100644 lucid/resource/tests/Fixures/loc_a/file.txt delete mode 100644 lucid/resource/tests/Fixures/loc_b/file.txt delete mode 100644 lucid/resource/tests/Loader/AbstractFileLoaderTest.php delete mode 100644 lucid/resource/tests/Loader/AbstractLoaderTest.php delete mode 100644 lucid/resource/tests/Loader/ChainedLoaderTest.php delete mode 100644 lucid/resource/tests/Loader/ResolverTest.php delete mode 100644 lucid/resource/tests/LocatorTest.php delete mode 100644 lucid/resource/tests/ObjectResourceTest.php delete mode 100644 lucid/resource/tests/Stubs/PhpFileLoader.php delete mode 100644 lucid/signal/.coveralls.yml delete mode 100755 lucid/signal/.phpunit delete mode 100644 lucid/signal/.travis.yml delete mode 100644 lucid/signal/README.md delete mode 100644 lucid/signal/composer.json delete mode 100644 lucid/signal/phpunit.xml.dist delete mode 100644 lucid/signal/src/.coveralls.yml delete mode 100644 lucid/signal/src/.travis.yml delete mode 100644 lucid/signal/src/ChainedEvent.php delete mode 100644 lucid/signal/src/ChainedEventInterface.php delete mode 100644 lucid/signal/src/ContainerAwareDispatcher.php delete mode 100644 lucid/signal/src/Event.php delete mode 100644 lucid/signal/src/EventDispatcher.php delete mode 100644 lucid/signal/src/EventDispatcherInterface.php delete mode 100644 lucid/signal/src/EventInterface.php delete mode 100644 lucid/signal/src/EventName.php delete mode 100644 lucid/signal/src/EventTrait.php delete mode 100644 lucid/signal/src/HandlerInterface.php delete mode 100644 lucid/signal/src/Priority.php delete mode 100644 lucid/signal/src/PriorityInterface.php delete mode 100644 lucid/signal/src/SubscriberInterface.php delete mode 100644 lucid/signal/src/Subscription.php delete mode 100644 lucid/signal/src/SubscriptionInterface.php delete mode 100644 lucid/signal/tests/ChainedEventTest.php delete mode 100644 lucid/signal/tests/ContainerAwareDispatcherTest.php delete mode 100644 lucid/signal/tests/EventDispatcherTest.php delete mode 100644 lucid/signal/tests/EventNameTest.php delete mode 100644 lucid/signal/tests/EventTest.php delete mode 100644 lucid/signal/tests/PriorityTest.php delete mode 100644 lucid/signal/tests/Stubs/SimpleSubscriber.php delete mode 100644 lucid/signal/tests/SubscriptionTest.php delete mode 100644 lucid/template/.coveralls.yml delete mode 100755 lucid/template/.phpunit delete mode 100644 lucid/template/.travis.yml delete mode 100644 lucid/template/LICENSE.md delete mode 100644 lucid/template/README.md delete mode 100644 lucid/template/composer.json delete mode 100644 lucid/template/phpunit.xml.dist delete mode 100644 lucid/template/src/.coveralls.yml delete mode 100644 lucid/template/src/.travis.yml delete mode 100644 lucid/template/src/AbstractPhpEngine.php delete mode 100644 lucid/template/src/Data/Data.php delete mode 100644 lucid/template/src/Data/TemplateDataInterface.php delete mode 100644 lucid/template/src/DelegatingEngine.php delete mode 100644 lucid/template/src/DisplayInterface.php delete mode 100644 lucid/template/src/Engine.php delete mode 100644 lucid/template/src/EngineInterface.php delete mode 100644 lucid/template/src/Exception/LoaderException.php delete mode 100644 lucid/template/src/Exception/RenderException.php delete mode 100644 lucid/template/src/Exception/TemplateException.php delete mode 100644 lucid/template/src/Extension/AbstractExtension.php delete mode 100644 lucid/template/src/Extension/ExtensionInterface.php delete mode 100644 lucid/template/src/Extension/FunctionInterface.php delete mode 100644 lucid/template/src/Extension/PhpEngineExtension.php delete mode 100644 lucid/template/src/Extension/RoutingExtension.php delete mode 100644 lucid/template/src/Extension/TemplateFunction.php delete mode 100644 lucid/template/src/Identity.php delete mode 100644 lucid/template/src/IdentityInterface.php delete mode 100644 lucid/template/src/IdentityParser.php delete mode 100644 lucid/template/src/IdentityParserInterface.php delete mode 100644 lucid/template/src/Listener/ListenerInterface.php delete mode 100644 lucid/template/src/Loader/FilesystemLoader.php delete mode 100644 lucid/template/src/Loader/LoaderInterface.php delete mode 100644 lucid/template/src/Loader/LoggerAwareLoader.php delete mode 100644 lucid/template/src/PhpRenderInterface.php delete mode 100644 lucid/template/src/RenderEngineDecorator.php delete mode 100644 lucid/template/src/RenderInterface.php delete mode 100644 lucid/template/src/Resource/AbstractResource.php delete mode 100644 lucid/template/src/Resource/FileResource.php delete mode 100644 lucid/template/src/Resource/ResourceInterface.php delete mode 100644 lucid/template/src/Resource/StringResource.php delete mode 100644 lucid/template/src/Section.php delete mode 100644 lucid/template/src/Traits/ViewAwareTrait.php delete mode 100644 lucid/template/src/View.php delete mode 100644 lucid/template/src/ViewAwareInterface.php delete mode 100644 lucid/template/src/ViewManagerInterface.php delete mode 100644 lucid/template/src/composer.json delete mode 100644 lucid/template/tests/Data/DataTest.php delete mode 100644 lucid/template/tests/DelegatingEngineTest.php delete mode 100644 lucid/template/tests/EngineTest.php delete mode 100644 lucid/template/tests/Extension/PhpEngineExtensionTest.php delete mode 100644 lucid/template/tests/Fixures/view/content.php delete mode 100644 lucid/template/tests/Fixures/view/error.php delete mode 100644 lucid/template/tests/Fixures/view/func.0.php delete mode 100644 lucid/template/tests/Fixures/view/func.1.php delete mode 100644 lucid/template/tests/Fixures/view/include.0.php delete mode 100644 lucid/template/tests/Fixures/view/include.1.php delete mode 100644 lucid/template/tests/Fixures/view/index.0.php delete mode 100644 lucid/template/tests/Fixures/view/index.1.php delete mode 100644 lucid/template/tests/Fixures/view/index.2.php delete mode 100644 lucid/template/tests/Fixures/view/index.php delete mode 100644 lucid/template/tests/Fixures/view/partials/extend.0.php delete mode 100644 lucid/template/tests/Fixures/view/partials/extend.1.php delete mode 100644 lucid/template/tests/Fixures/view/partials/extend.2.php delete mode 100644 lucid/template/tests/Fixures/view/partials/footer.php delete mode 100644 lucid/template/tests/Fixures/view/partials/header.php delete mode 100644 lucid/template/tests/Fixures/view/partials/include.0.php delete mode 100644 lucid/template/tests/Fixures/view/partials/include.1.php delete mode 100644 lucid/template/tests/IdentityParserTest.php delete mode 100644 lucid/template/tests/IdentityTest.php delete mode 100644 lucid/template/tests/Listener/ListenerTest.php delete mode 100644 lucid/template/tests/Loader/FilesystemLoaderTest.php delete mode 100644 lucid/template/tests/Loader/LoggerAwareLoaderTest.php delete mode 100644 lucid/template/tests/RenderEngineDecoratorTest.php delete mode 100644 lucid/template/tests/RenderTest.php delete mode 100644 lucid/template/tests/Resource/FileResourceTest.php delete mode 100644 lucid/template/tests/Resource/StringResourceTest.php delete mode 100644 lucid/template/tests/SectionTest.php delete mode 100644 lucid/template/tests/Stubs/Displayable.php delete mode 100644 lucid/template/tests/Stubs/Listener.php delete mode 100644 lucid/template/tests/ViewTest.php delete mode 100644 lucid/writer/.coveralls.yml delete mode 100644 lucid/writer/.travis.yml delete mode 100644 lucid/writer/LICENSE.md delete mode 100644 lucid/writer/README.md delete mode 100644 lucid/writer/composer.json delete mode 100644 lucid/writer/phpunit.xml.dist delete mode 100644 lucid/writer/src/File/JsonGenerator.php delete mode 100644 lucid/writer/src/File/PhpGenerator.php delete mode 100644 lucid/writer/src/FormatterHelper.php delete mode 100644 lucid/writer/src/GeneratorInterface.php delete mode 100644 lucid/writer/src/Object/AbstractBlock.php delete mode 100644 lucid/writer/src/Object/AbstractWriter.php delete mode 100644 lucid/writer/src/Object/Annotateable.php delete mode 100644 lucid/writer/src/Object/Argument.php delete mode 100644 lucid/writer/src/Object/ClassWriter.php delete mode 100644 lucid/writer/src/Object/Constant.php delete mode 100644 lucid/writer/src/Object/DocBlock.php delete mode 100644 lucid/writer/src/Object/ImportHelper.php delete mode 100644 lucid/writer/src/Object/ImportInterface.php delete mode 100644 lucid/writer/src/Object/ImportResolver.php delete mode 100644 lucid/writer/src/Object/InterfaceMethod.php delete mode 100644 lucid/writer/src/Object/InterfaceWriter.php delete mode 100644 lucid/writer/src/Object/MemberInterface.php delete mode 100644 lucid/writer/src/Object/Method.php delete mode 100644 lucid/writer/src/Object/MethodInterface.php delete mode 100644 lucid/writer/src/Object/Property.php delete mode 100644 lucid/writer/src/Object/TraitAwareWriterHelper.php delete mode 100644 lucid/writer/src/Object/TraitWriter.php delete mode 100644 lucid/writer/src/Object/VisibilityHelperTrait.php delete mode 100644 lucid/writer/src/Stringable.php delete mode 100644 lucid/writer/src/Writer.php delete mode 100644 lucid/writer/src/WriterInterface.php delete mode 100644 lucid/writer/tests/File/JsonGeneratorTest.php delete mode 100644 lucid/writer/tests/File/PhpGeneratorTest.php delete mode 100644 lucid/writer/tests/FormatterHelperTest.php delete mode 100644 lucid/writer/tests/Object/AbstractWriterTest.php delete mode 100644 lucid/writer/tests/Object/ArgumentTest.php delete mode 100644 lucid/writer/tests/Object/ClassWriterTest.php delete mode 100644 lucid/writer/tests/Object/CommentBlockTest.php delete mode 100644 lucid/writer/tests/Object/ConstantTest.php delete mode 100644 lucid/writer/tests/Object/DocBlockTest.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.0.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.1.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.2.1.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.2.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.3.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.4.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.5.php delete mode 100644 lucid/writer/tests/Object/Fixures/class.test.php delete mode 100644 lucid/writer/tests/Object/Fixures/interface.0.php delete mode 100644 lucid/writer/tests/Object/Fixures/interface.1.php delete mode 100644 lucid/writer/tests/Object/Fixures/interface.2.php delete mode 100644 lucid/writer/tests/Object/Fixures/interface.4.1.php delete mode 100644 lucid/writer/tests/Object/Fixures/interface.4.php delete mode 100644 lucid/writer/tests/Object/Fixures/trait.0.php delete mode 100644 lucid/writer/tests/Object/Fixures/trait.1.php delete mode 100644 lucid/writer/tests/Object/ImportResolverTest.php delete mode 100644 lucid/writer/tests/Object/InterfaceMethodTest.php delete mode 100644 lucid/writer/tests/Object/InterfaceWriterTest.php delete mode 100644 lucid/writer/tests/Object/MethodTest.php delete mode 100644 lucid/writer/tests/Object/PropertyTest.php delete mode 100644 lucid/writer/tests/Object/TraitWriterTest.php delete mode 100644 lucid/writer/tests/Object/VisibilityHelperTraitTest.php delete mode 100644 lucid/writer/tests/WriterTest.php delete mode 100644 lucid/xml/.coveralls.yml delete mode 100644 lucid/xml/.travis.yml delete mode 100644 lucid/xml/LICENSE.md delete mode 100644 lucid/xml/README.md delete mode 100644 lucid/xml/composer.json delete mode 100644 lucid/xml/phpunit.xml.dist delete mode 100644 lucid/xml/src/Dom/DOMDocument.php delete mode 100644 lucid/xml/src/Dom/DOMElement.php delete mode 100644 lucid/xml/src/Inflector/InflectorInterface.php delete mode 100644 lucid/xml/src/Inflector/SimpleInflector.php delete mode 100644 lucid/xml/src/Loader/Loader.php delete mode 100644 lucid/xml/src/Loader/LoaderInterface.php delete mode 100644 lucid/xml/src/Loader/LoaderTrait.php delete mode 100644 lucid/xml/src/Normalizer/Normalizer.php delete mode 100644 lucid/xml/src/Normalizer/NormalizerInterface.php delete mode 100644 lucid/xml/src/Parser.php delete mode 100644 lucid/xml/src/ParserInterface.php delete mode 100644 lucid/xml/src/SimpleXMLElement.php delete mode 100644 lucid/xml/src/Traits/XmlHelperTrait.php delete mode 100644 lucid/xml/src/Writer.php delete mode 100644 lucid/xml/tests/Dom/DomDocumentTest.php delete mode 100644 lucid/xml/tests/Dom/DomElementTest.php delete mode 100644 lucid/xml/tests/Fixures/test.xml delete mode 100644 lucid/xml/tests/Inflector/SimpleInflectorTest.php delete mode 100644 lucid/xml/tests/Loader/LoaderTest.php delete mode 100644 lucid/xml/tests/Normalizer/NormalizerTest.php delete mode 100644 lucid/xml/tests/Normalizer/Stubs/ArrayableStub.php delete mode 100644 lucid/xml/tests/Normalizer/Stubs/ConvertToArrayStub.php delete mode 100644 lucid/xml/tests/Normalizer/Stubs/NestedPropertyStub.php delete mode 100644 lucid/xml/tests/Normalizer/Stubs/SinglePropertyStub.php delete mode 100644 lucid/xml/tests/Normalizer/Stubs/TraversableStub.php delete mode 100644 lucid/xml/tests/ParserTest.php delete mode 100644 lucid/xml/tests/SimpleXMLElementTest.php delete mode 100644 lucid/xml/tests/WriterTest.php create mode 100644 src/.gitignore create mode 100644 src/Exception/MatchException.php create mode 100644 src/Exception/MissingValueException.php create mode 100644 src/Exception/ParserException.php rename lucid/signal/tests/Stubs/NamedEvent.php => src/Exception/ResolverException.php (53%) create mode 100644 src/Handler/ContainerAwareResolverInterface.php create mode 100644 src/Handler/Dispatcher.php create mode 100644 src/Handler/DispatcherInterface.php rename lucid/template/src/ComposeableInterface.php => src/Handler/ParameterMapperInterface.php (51%) create mode 100644 src/Handler/ParserInterface.php rename lucid/writer/src/Object/CommentBlock.php => src/Handler/PassParameterMapper.php (50%) rename lucid/cache/src/Client/MySql.php => src/Handler/ReferenceInterface.php (60%) create mode 100644 src/Handler/Reflector.php create mode 100644 src/Handler/Resolver.php rename lucid/cache/src/SectionableInterface.php => src/Handler/ResolverInterface.php (50%) create mode 100644 src/Handler/StrictParameterMapper.php create mode 100644 src/Handler/TypeMapCollection.php create mode 100644 src/Handler/TypeMapCollectionInterface.php create mode 100644 src/Handler/TypeMapperInterface.php create mode 100644 src/Loader/LoaderInterface.php create mode 100644 src/Loader/PhpLoader.php create mode 100644 src/Matcher/Context.php create mode 100644 src/Matcher/ContextInterface.php create mode 100644 src/Matcher/MatcherTrait.php create mode 100644 src/Matcher/RequestMatcher.php create mode 100644 src/Matcher/RequestMatcherInterface.php rename lucid/cache/src/Driver/SqLiteDriver.php => src/Parser/Delimiter.php (57%) create mode 100644 src/Parser/ParserInterface.php create mode 100644 src/Parser/Standard.php rename lucid/cache/src/Client/SqLite.php => src/Parser/Text.php (56%) create mode 100644 src/Parser/Token.php rename lucid/cache/src/Driver/MySqlDriver.php => src/Parser/TokenInterface.php (57%) create mode 100644 src/Parser/Variable.php create mode 100644 src/Request/Context.php create mode 100644 src/Request/ContextInterface.php create mode 100644 src/Request/PassResponseMapper.php create mode 100644 src/Request/ResponseMapperInterface.php create mode 100644 src/Request/UrlGenerator.php create mode 100644 src/Request/UrlGeneratorInterface.php create mode 100644 src/Route.php create mode 100644 src/RouteCollectionBuilder.php create mode 100644 src/RouteCollectionInterface.php create mode 100644 src/RouteContext.php create mode 100644 src/RouteContextInterface.php create mode 100644 src/RouteGroup.php create mode 100644 src/RouteInterface.php create mode 100644 src/Router.php create mode 100644 src/RouterInterface.php create mode 100644 src/Routes.php create mode 100644 tests/Cache/RoutesTest.php create mode 100644 tests/Handler/DispatcherTest.php create mode 100644 tests/Handler/PassParameterMapperTest.php create mode 100644 tests/Handler/ReflectorTest.php create mode 100644 tests/Handler/ResolverTest.php create mode 100644 tests/Handler/StrictParameterMapperTest.php create mode 100644 tests/Handler/Stubs/AbstractHandler.php create mode 100644 tests/Handler/Stubs/SimpleHandler.php create mode 100644 tests/Handler/TypeMapCollectionTest.php create mode 100644 tests/Loader/Fixures/faulty.php create mode 100644 tests/Loader/Fixures/route_groups.0.php create mode 100644 tests/Loader/Fixures/route_groups.1.php create mode 100644 tests/Loader/Fixures/route_groups.2.php create mode 100644 tests/Loader/Fixures/routes.0.php create mode 100644 tests/Loader/PhpLoaderTest.php create mode 100644 tests/Matcher/ContextTest.php create mode 100644 tests/Matcher/RequestMatcherTest.php create mode 100644 tests/Parser/StandardTest.php create mode 100644 tests/Parser/VariableTest.php create mode 100644 tests/Request/ContextTest.php create mode 100644 tests/Request/Fixures/Server.php create mode 100644 tests/Request/UrlGeneratorTest.php create mode 100644 tests/RouteCollectionBuilderTest.php create mode 100644 tests/RouteContextTest.php create mode 100644 tests/RouteTest.php create mode 100644 tests/RouterTest.php create mode 100644 tests/RoutesTest.php diff --git a/lucid/cache/.coveralls.yml b/.coveralls.yml similarity index 100% rename from lucid/cache/.coveralls.yml rename to .coveralls.yml diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b62da6a..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -vendor/ -_* -/report/ diff --git a/lucid/cache/.travis.yml b/.travis.yml similarity index 94% rename from lucid/cache/.travis.yml rename to .travis.yml index 5feedc8..722d946 100644 --- a/lucid/cache/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ install: script: - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* src tests; fi + - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* --ignore=*/Fixures src tests; fi after_script: - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi diff --git a/LICENSE.md b/LICENSE.md index 182ad4e..a8d551b 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,7 +1,7 @@ -# Copyright (c) 2014-2016 Thomas Appel +# Copyright (c) 2015-2017 Thomas Appel -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 77f9d98..ab673ad 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,151 @@ -# Lucid Source Repository +# A PSR-7 compatible HTTP router + +[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) +[![Source Code](http://img.shields.io/badge/source-lucid/mux-blue.svg?style=flat-square)](https://github.com/lucidphp/mux/tree/master) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/mux/blob/master/LICENSE.md) + +[![Build Status](https://img.shields.io/travis/lucidphp/mux/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/mux) +[![Code Coverage](https://img.shields.io/coveralls/lucidphp/mux/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/mux) +[![HHVM](https://img.shields.io/hhvm/lucid/mux/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/mux) ## Installation -```bash -$ composer -v install +```shell +> composer require lucid/mux:dev-master +``` + +## Usage + +### Creating coute collections + +#### Manualy + +```php +add('index', new Route('/', 'Acme\FrontController@getIndex')); + +``` + +#### Using the Builder + +```php +get('/', 'Acme\FrontController@getIndex'); + +// adds a POST route +$builder->post('/user', 'Acme\UserController@createUser'); + +// adds a UPDATE route +$builder->update('/user/{id}', 'Acme\UserController@updateUser'); + +// adds a DELETE route +$builder->delete('/user/{id}', 'Acme\UserController@deleteUser'); + +``` + +### Dispatching routes + +The router component takes a request context object to dispatch the +corresponding routing action. + +```php +getCollection()); + +$request = new RequestContext( + current(explode('?', $_SERVER['REQUEST_URI'])), + $_SERVER['REQUEST_METHOD'] +); + +$response = $router->dispatch($request); + +``` + +#### Working with PSR-7 requests + +You can easily create a requestcontext from an existing psr7 compatible +server request by using the `Context::fromPsrRequest()` method. + +```php + 12 +]; + +$response = $router->route('user.delete', $options); +``` + +### Advanced router configuration + +The router mostly relies on two main components: + + 1. a handler dispatcher, which is responsible for finding and executing the + given action (defined on the route object) + - a response mapper, which is capable of mapping the responsens to a desired + type + +#### The handler dispatcher + +By default, the handler dispatcher/resolver will check if the given handler is +callable. If the handler is a string containing an @ symbol, it is assumed that +the left hand side represents a classname and the right hand site a method. + +##### Dependency Injection + +If the handler resolver (`Lucid\Mux\Handler\Resolver` by default) is constructed +with an instance of `Interop\Container\ContainerInterface` it will also check if +the left hand side is a service registered by the di container. + +```php +&2 - exit 1 - ;; - esac - shift -done - -# abort if error and clean up subsplit files -abortIf() { - if [[ ! "$1" == 0 ]]; then - cleanUp - echo "\e[31m%s\e[m\n" "$2" - exit $1 - fi - return 0 -} - -GIT=`which git` -# abort if subsplit init failed -if [[ `$GIT subtree &>/dev/null; echo $?` == 1 ]]; then - abortIf 1 "git subtree not supported.\n" - - exit 1; -fi - -if [[ `$GIT subsplit &>/dev/null; echo $?` == 1 ]]; then - echo "Installing git subsplit" - mkdir -p tmp/subsplit - `$GIT clone https://github.com/dflydev/git-subsplit.git tmp/subsplit` - cd tmp/subsplit - ./install.sh - printf "\e[33m%s\e[m\n" "Git subsplit installed." - cd - -else - printf "\e[33m%s\e[m\n" "Git subsplit already installed." -fi - -if [ -z "$BRANCH" ]; then - BRANCH=`$GIT rev-parse --abbrev-ref HEAD` - if [ ! `$GIT checkout $BRANCH &>/dev/null; echo $?` ]; then - abortIf 1 "Error checking out branch $BRANCH." - fi -fi - -if [[ ! -z "$DRYRUN" ]]; then - echo "DRYRUN: $GIT subsplit init git@github.com:lucidphp/lucid.git" -else - SUBSPLIT=`$GIT subsplit init git@github.com:lucidphp/lucid.git` - abortIf $? $SUBSPLIT -fi - -echo "current branch is $BRANCH" - -DIRS=`ls -d lucid/*/` - -for d in `ls -d lucid/* | xargs`; do - - printf "\e[33mtesting %s\e[m...\n" $d - - if [[ ! `$GIT ls-files $d/composer.json --error-unmatch 2>/dev/null; echo $?` ]]; then - printf "\e[31m%s\e[m: not in repository or composer.json missing.\n" $d - continue - fi - - name=`basename $d` - if [[ ! -z "$DRYRUN" ]]; then - echo "DRYRUN: $GIT subsplit publish --no-tags --heads="$BRANCH" "$d":git@github.com:lucidphp/"$name".git" - sleep 1 - else - RET=`$GIT subsplit publish --no-tags --heads="$BRANCH" "$d":git@github.com:lucidphp/"$name".git` - abortIf $? $RET - fi -done -unset d -cleanUp -exit 0 diff --git a/composer.json b/composer.json index 385fd9e..31caaaa 100644 --- a/composer.json +++ b/composer.json @@ -1,69 +1,36 @@ { - "name": "lucid/lucid", - "require": { - "php":">=5.6.0" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev", - "psr/http-message": "1.0.*@dev", - "container-interop/container-interop": "dev-master", - "phpseclib/phpseclib": "0.3.*@dev" - }, + "name": "lucid/mux", "license": "MIT", + "description": "Psr7 compatible http Routing Library", + "keywords": ["http", "prs7", "routing"], "authors": [ { "name": "iwyg", "email": "mail@thomas-appel.com" } ], + "require": { + "php":"^5.6 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "5.2.*@dev", + "lucid/resource": "dev-develop", + "container-interop/container-interop": "~1.1.0", + "zendframework/zend-diactoros":"^1.3" + }, "autoload": { "psr-4": { - "Lucid\\Cache\\":"lucid/cache/src/", - "Lucid\\Common\\":"lucid/common/src/", - "Lucid\\Config\\":"lucid/config/src/", - "Lucid\\DI\\":"lucid/di/src/", - "Lucid\\Filesystem\\":"lucid/filesystem/src/", - "Lucid\\Hash\\":"lucid/hash/src/", - "Lucid\\Http\\":"lucid/http/src/", - "Lucid\\Mux\\":"lucid/mux/src/", - "Lucid\\Mux\\Cache\\":"lucid/mux-cache/src/", - "Lucid\\Resource\\":"lucid/resource/src/", - "Lucid\\Signal\\":"lucid/signal/src/", - "Lucid\\Template\\":"lucid/template/src/", - "Lucid\\Writer\\":"lucid/writer/src/", - "Lucid\\Xml\\":"lucid/xml/src/" + "Lucid\\Mux\\":"src/" } }, "autoload-dev": { "psr-4": { - "Lucid\\Cache\\Tests\\":"lucid/cache/tests/", - "Lucid\\Common\\Tests\\":"lucid/common/tests/", - "Lucid\\Config\\Tests\\":"lucid/config/tests/", - "Lucid\\DI\\Tests\\":"lucid/di/tests/", - "Lucid\\Filesystem\\Tests\\":"lucid/filesystem/tests/", - "Lucid\\Hash\\Tests\\":"lucid/hash/tests/", - "Lucid\\Http\\Tests\\":"lucid/http/tests/", - "Lucid\\Mux\\Tests\\":"lucid/mux/tests/", - "Lucid\\Mux\\Cache\\Tests\\":"lucid/mux-cache/tests/", - "Lucid\\Resource\\Tests\\":"lucid/resource/tests/", - "Lucid\\Signal\\Tests\\":"lucid/signal/tests/", - "Lucid\\Template\\Tests\\":"lucid/template/tests/", - "Lucid\\Writer\\Tests\\":"lucid/writer/tests/", - "Lucid\\Xml\\Tests\\":"lucid/xml/tests/" + "Lucid\\Mux\\Tests\\":"tests/" } }, - "provides": { - "lucid/common": "dev-master", - "lucid/config": "dev-master", - "lucid/di": "dev-master", - "lucid/filesystem": "dev-master", - "lucid/mux": "dev-master", - "lucid/mux-cache": "dev-master", - "lucid/resource": "dev-master", - "lucid/signal": "dev-master", - "lucid/template": "dev-master", - "lucid/writer": "dev-master", - "lucid/xml": "dev-master" + "suggest": { + "lucid/resource": "For loading routes from file definitions.", + "container-interop/container-interop": "For resolving controllers via a DI container." }, "minimum-stability": "dev" } diff --git a/composer.lock b/composer.lock deleted file mode 100644 index dd29372..0000000 --- a/composer.lock +++ /dev/null @@ -1,1238 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "hash": "04c1a78c5197953c2f86694ace40e85d", - "content-hash": "1c35b7b053e2be21be02b2402a242f26", - "packages": [], - "packages-dev": [ - { - "name": "container-interop/container-interop", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/container-interop/container-interop.git", - "reference": "79455a819161193380629756ed3dd71dc8702e3c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/container-interop/container-interop/zipball/79455a819161193380629756ed3dd71dc8702e3c", - "reference": "79455a819161193380629756ed3dd71dc8702e3c", - "shasum": "" - }, - "type": "library", - "autoload": { - "psr-4": { - "Interop\\Container\\": "src/Interop/Container/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", - "time": "2015-11-04 15:33:13" - }, - { - "name": "doctrine/instantiator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/doctrine/instantiator.git", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", - "shasum": "" - }, - "require": { - "php": ">=5.3,<8.0-DEV" - }, - "require-dev": { - "athletic/athletic": "~0.1.8", - "ext-pdo": "*", - "ext-phar": "*", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Marco Pivetta", - "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.com/" - } - ], - "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://github.com/doctrine/instantiator", - "keywords": [ - "constructor", - "instantiate" - ], - "time": "2015-06-14 21:17:01" - }, - { - "name": "myclabs/deep-copy", - "version": "1.5.0", - "source": { - "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "e3abefcd7f106677fd352cd7c187d6c969aa9ddc" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/e3abefcd7f106677fd352cd7c187d6c969aa9ddc", - "reference": "e3abefcd7f106677fd352cd7c187d6c969aa9ddc", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "doctrine/collections": "1.*", - "phpunit/phpunit": "~4.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Create deep copies (clones) of your objects", - "homepage": "https://github.com/myclabs/DeepCopy", - "keywords": [ - "clone", - "copy", - "duplicate", - "object", - "object graph" - ], - "time": "2015-11-07 22:20:37" - }, - { - "name": "phpdocumentor/reflection-docblock", - "version": "2.0.4", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", - "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.0" - }, - "suggest": { - "dflydev/markdown": "~1.0", - "erusev/parsedown": "~1.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "psr-0": { - "phpDocumentor": [ - "src/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "mike.vanriel@naenius.com" - } - ], - "time": "2015-02-03 12:10:50" - }, - { - "name": "phpseclib/phpseclib", - "version": "0.3.10", - "source": { - "type": "git", - "url": "https://github.com/phpseclib/phpseclib.git", - "reference": "d15bba1edcc7c89e09cc74c5d961317a8b947bf4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/d15bba1edcc7c89e09cc74c5d961317a8b947bf4", - "reference": "d15bba1edcc7c89e09cc74c5d961317a8b947bf4", - "shasum": "" - }, - "require": { - "php": ">=5.0.0" - }, - "require-dev": { - "phing/phing": "~2.7", - "phpunit/phpunit": "~4.0", - "sami/sami": "~2.0", - "squizlabs/php_codesniffer": "~1.5" - }, - "suggest": { - "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.", - "ext-mcrypt": "Install the Mcrypt extension in order to speed up a wide variety of cryptographic operations.", - "pear-pear/PHP_Compat": "Install PHP_Compat to get phpseclib working on PHP < 4.3.3." - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.3-dev" - } - }, - "autoload": { - "psr-0": { - "Crypt": "phpseclib/", - "File": "phpseclib/", - "Math": "phpseclib/", - "Net": "phpseclib/", - "System": "phpseclib/" - }, - "files": [ - "phpseclib/Crypt/Random.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "phpseclib/" - ], - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jim Wigginton", - "email": "terrafrost@php.net", - "role": "Lead Developer" - }, - { - "name": "Patrick Monnerat", - "email": "pm@datasphere.ch", - "role": "Developer" - }, - { - "name": "Andreas Fischer", - "email": "bantu@phpbb.com", - "role": "Developer" - }, - { - "name": "Hans-Jürgen Petrich", - "email": "petrich@tronic-media.com", - "role": "Developer" - } - ], - "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", - "homepage": "http://phpseclib.sourceforge.net", - "keywords": [ - "BigInteger", - "aes", - "asn.1", - "asn1", - "blowfish", - "crypto", - "cryptography", - "encryption", - "rsa", - "security", - "sftp", - "signature", - "signing", - "ssh", - "twofish", - "x.509", - "x509" - ], - "time": "2015-01-28 21:50:33" - }, - { - "name": "phpspec/prophecy", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy.git", - "reference": "4f9b1eaf0a7da77c362f8d91cbc68ab1f4718d62" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/4f9b1eaf0a7da77c362f8d91cbc68ab1f4718d62", - "reference": "4f9b1eaf0a7da77c362f8d91cbc68ab1f4718d62", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.0.2", - "phpdocumentor/reflection-docblock": "~2.0", - "sebastian/comparator": "~1.1" - }, - "require-dev": { - "phpspec/phpspec": "~2.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.5.x-dev" - } - }, - "autoload": { - "psr-0": { - "Prophecy\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Konstantin Kudryashov", - "email": "ever.zet@gmail.com", - "homepage": "http://everzet.com" - }, - { - "name": "Marcello Duarte", - "email": "marcello.duarte@gmail.com" - } - ], - "description": "Highly opinionated mocking framework for PHP 5.3+", - "homepage": "https://github.com/phpspec/prophecy", - "keywords": [ - "Double", - "Dummy", - "fake", - "mock", - "spy", - "stub" - ], - "time": "2015-09-22 14:49:23" - }, - { - "name": "phpunit/php-code-coverage", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "3e8e8c5b7cc76a1199e251ded7b417dfcf7684a5" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3e8e8c5b7cc76a1199e251ded7b417dfcf7684a5", - "reference": "3e8e8c5b7cc76a1199e251ded7b417dfcf7684a5", - "shasum": "" - }, - "require": { - "php": ">=5.6", - "phpunit/php-file-iterator": "~1.3", - "phpunit/php-text-template": "~1.2", - "phpunit/php-token-stream": "~1.3", - "sebastian/environment": "^1.3.2", - "sebastian/version": "~1.0" - }, - "require-dev": { - "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "~5" - }, - "suggest": { - "ext-dom": "*", - "ext-xdebug": ">=2.2.1", - "ext-xmlwriter": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", - "homepage": "https://github.com/sebastianbergmann/php-code-coverage", - "keywords": [ - "coverage", - "testing", - "xunit" - ], - "time": "2015-11-06 13:55:10" - }, - { - "name": "phpunit/php-file-iterator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/6150bf2c35d3fc379e50c7602b75caceaa39dbf0", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", - "keywords": [ - "filesystem", - "iterator" - ], - "time": "2015-06-21 13:08:43" - }, - { - "name": "phpunit/php-text-template", - "version": "1.2.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-text-template.git", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Simple template engine.", - "homepage": "https://github.com/sebastianbergmann/php-text-template/", - "keywords": [ - "template" - ], - "time": "2015-06-21 13:50:34" - }, - { - "name": "phpunit/php-timer", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3e82f4e9fc92665fafd9157568e4dcb01d014e5b", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Utility class for timing", - "homepage": "https://github.com/sebastianbergmann/php-timer/", - "keywords": [ - "timer" - ], - "time": "2015-06-21 08:01:12" - }, - { - "name": "phpunit/php-token-stream", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "cab6c6fefee93d7b7c3a01292a0fe0884ea66644" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/cab6c6fefee93d7b7c3a01292a0fe0884ea66644", - "reference": "cab6c6fefee93d7b7c3a01292a0fe0884ea66644", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Wrapper around PHP's tokenizer extension.", - "homepage": "https://github.com/sebastianbergmann/php-token-stream/", - "keywords": [ - "tokenizer" - ], - "time": "2015-09-23 14:46:55" - }, - { - "name": "phpunit/phpunit", - "version": "5.2.x-dev", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "7a1a2641b4f6d71a1f7c080af9d0306aae63d2fb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/7a1a2641b4f6d71a1f7c080af9d0306aae63d2fb", - "reference": "7a1a2641b4f6d71a1f7c080af9d0306aae63d2fb", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-json": "*", - "ext-pcre": "*", - "ext-reflection": "*", - "ext-spl": "*", - "myclabs/deep-copy": "~1.3", - "php": ">=5.6", - "phpspec/prophecy": "^1.3.1", - "phpunit/php-code-coverage": "~3.0", - "phpunit/php-file-iterator": "~1.4", - "phpunit/php-text-template": "~1.2", - "phpunit/php-timer": ">=1.0.6", - "phpunit/phpunit-mock-objects": ">=3.0", - "sebastian/comparator": "~1.1", - "sebastian/diff": "~1.2", - "sebastian/environment": "~1.3", - "sebastian/exporter": "~1.2", - "sebastian/global-state": "~1.0", - "sebastian/resource-operations": "~1.0", - "sebastian/version": "~1.0", - "symfony/yaml": "~2.1|~3.0" - }, - "suggest": { - "phpunit/php-invoker": "~1.1" - }, - "bin": [ - "phpunit" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "5.2.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "The PHP Unit Testing framework.", - "homepage": "https://phpunit.de/", - "keywords": [ - "phpunit", - "testing", - "xunit" - ], - "time": "2015-11-06 15:17:56" - }, - { - "name": "phpunit/phpunit-mock-objects", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "382c729a52b7ef682e94c73fd6868f5ee8116ba7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/382c729a52b7ef682e94c73fd6868f5ee8116ba7", - "reference": "382c729a52b7ef682e94c73fd6868f5ee8116ba7", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.0.2", - "php": ">=5.6", - "phpunit/php-text-template": "~1.2", - "sebastian/exporter": "~1.2" - }, - "require-dev": { - "phpunit/phpunit": "~5" - }, - "suggest": { - "ext-soap": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Mock Object library for PHPUnit", - "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", - "keywords": [ - "mock", - "xunit" - ], - "time": "2015-10-18 07:55:54" - }, - { - "name": "psr/http-message", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/php-fig/http-message.git", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", - "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interface for HTTP messages", - "keywords": [ - "http", - "http-message", - "psr", - "psr-7", - "request", - "response" - ], - "time": "2015-05-04 20:22:00" - }, - { - "name": "sebastian/comparator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/937efb279bd37a375bcadf584dec0726f84dbf22", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "sebastian/diff": "~1.2", - "sebastian/exporter": "~1.2" - }, - "require-dev": { - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@2bepublished.at" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides the functionality to compare PHP values for equality", - "homepage": "http://www.github.com/sebastianbergmann/comparator", - "keywords": [ - "comparator", - "compare", - "equality" - ], - "time": "2015-07-26 15:48:44" - }, - { - "name": "sebastian/diff", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "6899b3e33bfbd386d88b5eea5f65f563e8793051" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/6899b3e33bfbd386d88b5eea5f65f563e8793051", - "reference": "6899b3e33bfbd386d88b5eea5f65f563e8793051", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Kore Nordmann", - "email": "mail@kore-nordmann.de" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Diff implementation", - "homepage": "http://www.github.com/sebastianbergmann/diff", - "keywords": [ - "diff" - ], - "time": "2015-06-22 14:15:55" - }, - { - "name": "sebastian/environment", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "6324c907ce7a52478eeeaede764f48733ef5ae44" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6324c907ce7a52478eeeaede764f48733ef5ae44", - "reference": "6324c907ce7a52478eeeaede764f48733ef5ae44", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides functionality to handle HHVM/PHP environments", - "homepage": "http://www.github.com/sebastianbergmann/environment", - "keywords": [ - "Xdebug", - "environment", - "hhvm" - ], - "time": "2015-08-03 06:14:51" - }, - { - "name": "sebastian/exporter", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "f88f8936517d54ae6d589166810877fb2015d0a2" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/f88f8936517d54ae6d589166810877fb2015d0a2", - "reference": "f88f8936517d54ae6d589166810877fb2015d0a2", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "sebastian/recursion-context": "~1.0" - }, - "require-dev": { - "ext-mbstring": "*", - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@2bepublished.at" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - } - ], - "description": "Provides the functionality to export PHP variables for visualization", - "homepage": "http://www.github.com/sebastianbergmann/exporter", - "keywords": [ - "export", - "exporter" - ], - "time": "2015-08-09 04:23:41" - }, - { - "name": "sebastian/global-state", - "version": "1.1.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bc37d50fea7d017d3d340f230811c9f1d7280af4", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.2" - }, - "suggest": { - "ext-uopz": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Snapshotting of global state", - "homepage": "http://www.github.com/sebastianbergmann/global-state", - "keywords": [ - "global state" - ], - "time": "2015-10-12 03:26:01" - }, - { - "name": "sebastian/recursion-context", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "994d4a811bafe801fb06dccbee797863ba2792ba" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/994d4a811bafe801fb06dccbee797863ba2792ba", - "reference": "994d4a811bafe801fb06dccbee797863ba2792ba", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - } - ], - "description": "Provides functionality to recursively process PHP variables", - "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2015-06-21 08:04:50" - }, - { - "name": "sebastian/resource-operations", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "shasum": "" - }, - "require": { - "php": ">=5.6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides a list of PHP built-in functions that operate on resources", - "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2015-07-28 20:34:47" - }, - { - "name": "sebastian/version", - "version": "1.0.6", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/version.git", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", - "shasum": "" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Library that helps with managing the version number of Git-hosted PHP projects", - "homepage": "https://github.com/sebastianbergmann/version", - "time": "2015-06-21 13:59:46" - }, - { - "name": "symfony/yaml", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "1fac4249ca3ea5949685fe36ca1eaceac19fd906" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/1fac4249ca3ea5949685fe36ca1eaceac19fd906", - "reference": "1fac4249ca3ea5949685fe36ca1eaceac19fd906", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Yaml\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "https://symfony.com", - "time": "2015-11-09 10:46:27" - } - ], - "aliases": [], - "minimum-stability": "dev", - "stability-flags": { - "phpunit/phpunit": 20, - "psr/http-message": 20, - "container-interop/container-interop": 20, - "phpseclib/phpseclib": 20 - }, - "prefer-stable": false, - "prefer-lowest": false, - "platform": { - "php": ">=5.6.0" - }, - "platform-dev": [] -} diff --git a/lucid/cache/.phpunit b/lucid/cache/.phpunit deleted file mode 100755 index 379530d..0000000 --- a/lucid/cache/.phpunit +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then - ./vendor/bin/phpunit --verbose; -else - ./vendor/bin/phpunit --verbose --coverage-text --coverage-clover /tmp/coverage/coverage.xml -fi diff --git a/lucid/cache/LICENSE.md b/lucid/cache/LICENSE.md deleted file mode 100644 index 4196805..0000000 --- a/lucid/cache/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright (c) 2013-2014 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/cache/README.md b/lucid/cache/README.md deleted file mode 100644 index 386ddd9..0000000 --- a/lucid/cache/README.md +++ /dev/null @@ -1,43 +0,0 @@ -# Caching library. - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/cache/tree/master) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/cache/blob/master/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/cache/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/cache) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/cache/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/cache) -[![HHVM](https://img.shields.io/hhvm/lucid/cache/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/cache) - -## Requirements -``` -php >= 5.6 -``` - -## Installation - -```bash -$ composer require lucid/cache -``` -## Using the storage - -```php -set('id', 'value'); -$cache->get('id'); // 'value' -``` - -### Included clients - -- `APCu` -- `Filesystem` -- `InMemory` -- `Redis` -- `Memcached` -- `Memcache (php < 7.0)` -- `XCache` diff --git a/lucid/cache/composer.json b/lucid/cache/composer.json deleted file mode 100644 index fba374a..0000000 --- a/lucid/cache/composer.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "lucid/cache", - "description": "Caching library", - "license": "MIT", - "authors": [ - { - "name": "Thomas Appel", - "email": "mail@thomas-appel.com" - } - ], - "require": { - "php":">=5.6.0", - "lucid/common":"dev-master" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev" - }, - "autoload": { - "psr-4": {"Lucid\\Cache\\": "src/"} - }, - "autoload-dev": { - "psr-4": {"Lucid\\Cache\\Tests\\": "tests/"} - }, - "minimum-stability": "dev" -} diff --git a/lucid/cache/phpunit.xml.dist b/lucid/cache/phpunit.xml.dist deleted file mode 100644 index 4582e10..0000000 --- a/lucid/cache/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/cache/src/.travis.yml b/lucid/cache/src/.travis.yml deleted file mode 100644 index 47d46a5..0000000 --- a/lucid/cache/src/.travis.yml +++ /dev/null @@ -1,25 +0,0 @@ -language: php -php: - - 5.4 - - 5.5 - - 5.6 - - hhvm - - hhvm-nightly -matrix: - allow_failures: - - php: hhvm - - php: hhvm-nightly - fast_finish: true -services: - - memcache - - memcached -before_script: - - composer self-update - - composer install --prefer-source --no-interaction --dev - - sh ./.travis_install.sh -script: - - bash -c 'if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then vendor/bin/phpunit --verbose; fi;' - - bash -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then vendor/bin/phpunit --verbose --coverage-text --coverage-clover /tmp/coverage/coverage.xml; fi;' -notififation: - on_success: never - on_failure: always diff --git a/lucid/cache/src/.travis_install.sh b/lucid/cache/src/.travis_install.sh deleted file mode 100755 index 8cf723d..0000000 --- a/lucid/cache/src/.travis_install.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/sh - -version=`php -v|grep -o "[PHP ]5\.[0-9]"`; - -if [ $version == '5.4']; - then - curl -o APC-3.1.10.tgz http://pecl.php.net/get/APC-3.1.10.tgz - tar -xzf APC-3.1.10.tgz - sh -c "cd APC-3.1.10 && phpize && ./configure && make && sudo make install && cd .." - rm -Rf APC-3.1.10 - rm APC-3.1.10.tgz - echo "extension=apc.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - echo "apc.enable_cli=On" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - - curl -o APCU-4.0.4.tgz http://pecl.php.net/get/apcu-4.0.4.tgz - tar -xzf APCU-4.0.4.tgz - sh -c "cd APCU-4.0.4 && phpize && ./configure && make && sudo make install && cd .." - rm -Rf APCU-4.0.4 - rm APCU-4.0.4.tgz - echo "extension=apcu.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - echo "apcu.enable_cli=On" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - phpenv rehash -fi - -if [ $TRAVIS_PHP_VERSION != "hhvm" ]; - then - printf "\n" | pecl install -f memcached-2.0.1 - echo "extension=\"memcached.so\"" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - printf "\n" | pecl install -f memcache - echo "extension=\"memcache.so\"" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` - phpenv rehash -fi; diff --git a/lucid/cache/src/CacheInterface.php b/lucid/cache/src/CacheInterface.php deleted file mode 100644 index d2e18d7..0000000 --- a/lucid/cache/src/CacheInterface.php +++ /dev/null @@ -1,132 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache; - -use Closure; - -/** - * @interface CacheInterface - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -interface CacheInterface -{ - const COMPRESSED = true; - - const UNCOMPRESSED = false; - - const PERSIST = -1; - - /** - * Check if an item is already cached. - * - * @param string $key the cache item identifier. - * - * @return boolean `TRUE` if exists, otherwise `FALSE` - */ - public function has($key); - - /** - * Retreive cached data by key. - * - * @param string $key the storage key. - * @param mixed $default the default value to return if nothing is found. - * - * @return mixed|null The cached data or `NULL` if no object was found - */ - public function get($key, $default = null); - - /** - * Write data to cache. - * - * @param string $key the storage key. - * @param mixed $data the data to be stored. - * @param int|string $expires expirey time in minutes or as valid UNIX date format. - * If a negative integer `-1` is passed, the cache will persist and not expire. - * @param boolean $compressed compress data when writing. Some caching - * engies will not offer compression. In this case, `$compress` will be - * ignored. - * - * @return boolean `TRUE` on success, `FALSE` on error - */ - public function set($key, $data, $expires = 60, $compressed = false); - - /** - * Persists a data set. - * - * Caches data with a far future expiry time. - * - * @see CacheInterface::set() - * - * @return boolean `TRUE` on success, `FALSE` on error - */ - public function persist($key, $data, $compressed = false); - - /** - * Increments a numeric value stored in the cache. - * - * @param string $key the storage key. - * @param int $value the value to increment by. - * - * @return int the incremented value. - */ - public function increment($key, $value = 1); - - /** - * Decrements a numeric value stored in the cache. - * - * @param string $key the storage key. - * @param int $value the value to decrement by. - * - * @return int the decremented value. - */ - public function decrement($key, $value = 1); - - /** - * Deletes data from cache. - * - * If `$key` is ommitted the whole soreage will be wiped, otherwise, the - * item stored under `$key` will be deleted. - * - * @param string $key the storage key - * @return void - */ - public function purge($key = null); - - /** - * Writes default data to cache. - * - * @param string $key the storage key - * @param callable $callback a callback to return the default data - * if a dataset for `$key` does not exist - * @param int|string $expires expirey time in minutes or as valid UNIX date format. - * @param boolean $compressed compress data - * - * @return mixed the cached item for `$key` or the results of the `$callback` - */ - public function setUsing($key, callable $callback, $expires = null, $compressed = false); - - /** - * Writes default data to cache with a far future expiry date. - * - * @param string $key the cache item identifier - * @param callable $callback A callback to returns the default data - * if a dataset for `$key` does not exist - * @param boolean $compressed compress data on storage - * - * @return mixed the cached item for `$key` or the results of the `$callback` - */ - public function persistUsing($key, callable $callback, $compressed = false); -} diff --git a/lucid/cache/src/Client/AbstractClient.php b/lucid/cache/src/Client/AbstractClient.php deleted file mode 100644 index ca45505..0000000 --- a/lucid/cache/src/Client/AbstractClient.php +++ /dev/null @@ -1,183 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use InvalidArgumentException; -use Lucid\Cache\CacheInterface; -use Lucid\Cache\ClientInterface; - -/** - * @class AbstractClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -abstract class AbstractClient implements ClientInterface -{ - /** @var float */ - protected $defaultExpiry = 60; - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, $this->getPersistLevel(), $compressed); - } - - /** - * increment - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @return void - */ - public function increment($key, $value) - { - $this->validateIncrementValue($value); - - return $this->incrementValue($key, $value); - } - - /** - * decrement - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @return void - */ - public function decrement($key, $value) - { - $this->validateIncrementValue($value); - - return $this->decrementValue($key, $value); - } - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToSeconds($expires); - } - - /** - * get default expiry time - */ - public function getDefaultExpiry() - { - return $this->$defaultExpiry; - } - - /** - * validateIncrementValue - * - * @param mixed $value - * - * @throws InvalidArgumentException - * @access private - * @return void - */ - private function validateIncrementValue($value) - { - if (!is_int($value) || $value < 1) { - throw new InvalidArgumentException('Value must be Integer and greater that zero'); - } - } - - /** - * incrementValue - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @abstract - * @return mixed - */ - abstract protected function incrementValue($key, $value); - - /** - * decrementValue - * - * @param mixed $key - * @param mixed $value - * - * @access protected - * @abstract - * @return mixed - */ - abstract protected function decrementValue($key, $value); - - /** - * expiryToSeconds - * - * @param int|string $expiry - * - * @return int - */ - protected function expiryToSeconds($expiry) - { - if (is_string($expiry = $this->validateExpires($expiry))) { - return strtotime($expiry) - time(); - } - - return CacheInterface::PERSIST === $expiry ? $this->getPersistLevel() : $expiry * 60; - } - - /** - * expiryToUnixTimestamp - * - * @param mixed $expiry - * - * @return int - */ - protected function expiryToUnixTimestamp($expiry) - { - if (is_string($expiry = $this->validateExpires($expiry))) { - return strtotime($expiry); - } - - return CacheInterface::PERSIST === $expiry ? $this->getPersistLevel() : time() + ($expiry * 60); - } - - /** - * validateExpires - * - * @param mixed $expiry - * - * @return int - */ - protected function validateExpires($expires) - { - if (($str = is_string($expires)) && false === @strtotime($expires)) { - throw new InvalidArgumentException(sprintf('Invalid expiry time "%s".', $expires)); - } - - return $str ? $expires : (int)$expires; - } - - /** - * getPersistLevel - * - * @return int - */ - protected function getPersistLevel() - { - return 0; - } -} diff --git a/lucid/cache/src/Client/AbstractSqlClient.php b/lucid/cache/src/Client/AbstractSqlClient.php deleted file mode 100644 index 5a4f261..0000000 --- a/lucid/cache/src/Client/AbstractSqlClient.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use PDO; - -/** - * @class AbstractSqlClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class AbstractSqlClient implements DirverInterface -{ - /** @var PDO */ - private $pdo; - - /** - * Constructor. - * - * @param PDO $pdo - */ - public function __construct(PDO $pdo) - { - $this->pdo = $pdo; - } - - /** - * Returns the PDO instance. - * - * @return PDO - */ - final protected function pdo() - { - return $this->pdo; - } -} diff --git a/lucid/cache/src/Client/Apcu.php b/lucid/cache/src/Client/Apcu.php deleted file mode 100644 index 5773aca..0000000 --- a/lucid/cache/src/Client/Apcu.php +++ /dev/null @@ -1,96 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use RuntimeException; - -/** - * @class Apcu - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Apcu extends AbstractClient -{ - /** - * Constructor. - */ - public function __construct() - { - if (!extension_loaded('apcu')) { - throw new RuntimeException('APCu extension not loaded.'); - } - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return apcu_exists($key); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $res = apcu_fetch($key, $success); - - return $success ? $res : null; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - return apcu_store($key, $data, $expires); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return apcu_delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - return apcu_clear_cache('user'); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - $val = apcu_inc($key, $value, $success); - - return $success ? (int)$val : false; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - $val = apcu_dec($key, $value, $success); - - return $success ? (int)$val : false; - } -} diff --git a/lucid/cache/src/Client/ConnectionInterface.php b/lucid/cache/src/Client/ConnectionInterface.php deleted file mode 100644 index e021d1f..0000000 --- a/lucid/cache/src/Client/ConnectionInterface.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Client; - -/** - * @interface ConnectionInterface - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -interface ConnectionInterface -{ - /** - * Connect to a server. - * - * @return boolean `TRUE` on success, `FALSE` on failure. - */ - public function connect(); - - /** - * Close connection to a server. - * - * @return boolean `TRUE` on success, `FALSE` on failure. - */ - public function close(); - - /** - * Tell if server is connected. - * - * @return boolean `TRUE` if connected, otherwise `FALSE`. - */ - public function isConnected(); - - /** - * Get the specified server driver. - * - * @return mixed - */ - public function getClient(); - - /** - * Get the specified server driver and connect. - * - * @return mixed - */ - public function getClientAndConnect(); -} diff --git a/lucid/cache/src/Client/Filesystem.php b/lucid/cache/src/Client/Filesystem.php deleted file mode 100644 index 527f686..0000000 --- a/lucid/cache/src/Client/Filesystem.php +++ /dev/null @@ -1,306 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Client; - -use FilesystemIterator; -use RecursiveDirectoryIterator; -use Lucid\Cache\CacheInterface; - -/** - * @class FilesystemClient - * @see AbstractClient - * - * @package Selene\Module\Cache - * @version $Id$ - * @author iwyg - */ -class Filesystem extends AbstractClient -{ - /** @var string */ - private $cachedir; - - /** - * Constructor. - * - * @param FilesystemInterface $fs - * @param string $location - */ - public function __construct($path) - { - $this->setCacheDir($path); - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - if (!is_file($file = $this->getFilePath($key))) { - return false; - } - - return time() < filemtime($file); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if (!$this->exists($key)) { - return; - } - - list ($data,) = $this->getFileContent($this->getFilePath($key)); - - return $data; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - if (CacheInterface::PERSIST === $expires) { - $expires = strtotime('2037-12-31'); - } - - list($contents, $timestamp) = $this->serializeData($data, $compressed, (int)$expires); - - if (!file_exists($parent = dirname($file = $this->getFilePath($key)))) { - mkdir($parent); - } - - if (false === file_put_contents($file, $contents, LOCK_EX)) { - return false; - } - - touch($file, $timestamp); - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, CacheInterface::PERSIST === $expires, $compressed); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - if (false !== @unlink($this->getFilePath($key))) { - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function flush() - { - if (!is_dir($this->cachedir)) { - return false; - } - - $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | - FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS; - - $this->rmDir($this->cachedir, $flags); - - return true; - } - - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->setIncrementValue($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->setIncrementValue($key, 0 - (int)$value); - } - - /** - * setIncrementValue - * - * @param mixed $key - * @param mixed $value - * - * @return boolean - */ - private function setIncrementValue($key, $value) - { - if (!$this->exists($key)) { - return false; - } - - list($data, $state) = $this->getFileContent($file = $this->getFilePath($key)); - - $timestamp = filemtime($file); - - list($contents,) = $this->serializeData($ret = ((int)$data + $value), static::C_UNCOMPRESSED === $state); - - file_put_contents($file, $contents, LOCK_EX); - - touch($file, $timestamp); - - return $ret; - } - - /** - * getFileContent - * - * @param mixed $file - * - * @access protected - * @return mixed - */ - protected function getFileContent($file) - { - $state = (int)substr($contents = file_get_contents($file), 0, 1); - $data = substr($contents, 2); - - $data = static::C_UNCOMPRESSED === $state ? - $data = unserialize($data) : - unserialize($this->uncompressData($data)); - - return [$data, $state]; - } - - /** - * {@inheritdoc} - */ - protected function getPersistLevel() - { - return CacheInterface::PERSIST; - } - - /** - * Returns a base64 encoded comperessed string - * - * @param string $data - * - * @return string - */ - private function compressData($data) - { - return base64_encode(gzcompress($data)); - } - - /** - * uncompressData - * - * @param string $data - * - * @return string - */ - private function uncompressData($data) - { - return gzuncompress(base64_decode($data)); - } - - /** - * getFilePath - * - * @param mixed $key - * - * @access private - * @return string - */ - private function getFilePath($key) - { - $hash = hash('md5', $key); - - return $this->cachedir.DIRECTORY_SEPARATOR.substr($hash, 0, 4).DIRECTORY_SEPARATOR.substr($hash, 4, 20); - } - - /** - * serializeData - * - * @param mixed $data - * @param boolean $compressed - * @param int $timestamp - * - * @return array - */ - private function serializeData($data, $compressed = self::C_UNCOMPRESSED, $timestamp = 0) - { - $data = serialize($data); - $data = $compressed ? $this->compressData($data) : $data; - $contents = sprintf('%d;%s', $compressed ? static::C_COMPRESSED : self::C_UNCOMPRESSED, $data); - - return [$contents, $timestamp]; - } - - /** - * setCacheDir - * - * @param string $path - * - * @return void - */ - private function setCacheDir($path) - { - if (!file_exists($path)) { - mkdir($path, 0775 & umask(), true); - } - - $this->cachedir = $path; - } - - /** - * wipes a directory. - * - * @param string $rpath - * @param int $flags - * - * @return void - */ - private function rmDir($rpath, $flags) - { - $itr = new Filesystemiterator($rpath, $flags); - - foreach ($itr as $path => $info) { - if ($info->isFile()) { - unlink($path); - } - - if ($info->isDir()) { - $this->rmDir($path, $flags); - rmdir($path); - } - } - } -} diff --git a/lucid/cache/src/Client/InMemory.php b/lucid/cache/src/Client/InMemory.php deleted file mode 100644 index 6f37ec9..0000000 --- a/lucid/cache/src/Client/InMemory.php +++ /dev/null @@ -1,158 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Client; - -use ArrayObject; - -/** - * @class ArrayClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class InMemory extends AbstractClient -{ - /** @var ArrayObject */ - private $storage; - - /** @var bool */ - private $persist; - - /** @var string */ - private $persistPath; - - /** - * Constructor. - * - * @param bool $persist - * @param string $path - */ - public function __construct($persist = false, $path = '') - { - $this->persistPath = $path; - $this->persist = (bool)$persist; - $this->boot(); - } - - /** - * @return void - */ - public function __destruct() - { - //$this->persistStorage(); - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return isset($this->storage[$key]); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if ($this->exists($key)) { - return $this->storage[$key]; - } - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $this->storage[$key] = $data; - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, 0, $compressed); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - unset($this->storage[$key]); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - unset($this->storage); - - $this->storage = new \ArrayObject; - $this->persistStorage(); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->storage[$key] = (int)$this->storage[$key] + (int)$value; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->storage[$key] = (int)$this->storage[$key] - (int)$value; - } - - /** - * setUpStorage - * - * @return void - */ - private function boot() - { - if ($this->persist and file_exists($this->persistPath)) { - try { - $this->storage = unserialize(file_get_contents($this->persistPath)); - } catch (\Exception $e) { - $this->storage = new ArrayObject; - } - return; - } - - $this->storage = new ArrayObject; - } - - /** - * persistStorage - * - * - * @return mixed - */ - private function persistStorage() - { - if ($this->persist) { - file_put_contents($this->persistPath, serialize($this->storage), LOCK_EX); - } - } -} diff --git a/lucid/cache/src/Client/Memcache.php b/lucid/cache/src/Client/Memcache.php deleted file mode 100644 index d9eb148..0000000 --- a/lucid/cache/src/Client/Memcache.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use Memcache as MemcacheClient; - -/** - * @class MemcacheClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Memcache extends MemcachedClient -{ - /** - * Flag stored along with cached items. - * - * @var int - */ - const ITEM_EXISTS = 4; - - /** - * Constructor. - * - * @param Memcache $memcache - */ - public function __construct(MemcacheClient $memcache) - { - $this->driver = $memcache; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - $flags = 0; - $res = $this->driver->get($key, $flags); - - return false !== $res ? true : $this->itemExists($flags); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $flags = 0; - $res = $this->driver->get($key, $flags); - - return $res ? $res : ($this->itemExists($flags) ? $res : null); - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $flags = $compressed ? MEMCACHE_COMPRESSED : 0; - $cached = $this->driver->set($key, $data, $flags | self::ITEM_EXISTS, $expires); - - return $cached; - } - - protected function itemExists(&$flags) - { - return 0 === (self::ITEM_EXISTS & ~$flags); - } -} diff --git a/lucid/cache/src/Client/MemcacheConnection.php b/lucid/cache/src/Client/MemcacheConnection.php deleted file mode 100644 index a484e49..0000000 --- a/lucid/cache/src/Client/MemcacheConnection.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Selene\Module\Cache\Client; - -use Memcache; -use RuntimeException; - -/** - * @class MemcacheConnection - * - * @package Selene\Module\Cache\Client - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -class MemcacheConnection implements ConnectionInterface -{ - /** - * memcached - * - * @var Memcached - * @access private - */ - private $memcached; - - /** - * Connection Status - * - * @var boolean - */ - private $connected; - - /** - * Constructor. - * - * @param array $servers - * @param Memcached $memcached - */ - public function __construct(array $servers, Memcache $memcache = null) - { - $this->memcache = $memcache ?: new Memcache; - $this->servers = $servers; - } - - /** - * {@inheritdoc} - */ - public function connect() - { - if ($this->isConnected()) { - return false; - } - - $this->addServers(); - - try { - $this->memcache->getVersion(); - } catch (\Exception $e) { - throw new RuntimeException('Cannot initialize Memcache: ' . $e->getMessage()); - } - - return $this->connected = true; - } - - /** - * {@inheritdoc} - */ - public function close() - { - $this->connected = false; - - return $this->memcache->close(); - } - - /** - * {@inheritdoc} - */ - public function isConnected() - { - return (bool)$this->connected; - } - - /** - * {@inheritdoc} - * - * @return Memcache - */ - public function getClient() - { - return $this->memcache; - } - - /** - * {@inheritdoc} - * - * @return Memcache - */ - public function getClientAndConnect() - { - $this->connect(); - - return $this->getClient(); - } - - /** - * Adds given server connections to memcache. - * - * @return void - */ - protected function addServers() - { - foreach ($this->servers as $server) { - $this->memcache->addServer($server['host'], (int)$server['port'], true, (int)$server['weight']); - } - } -} diff --git a/lucid/cache/src/Client/Memcached.php b/lucid/cache/src/Client/Memcached.php deleted file mode 100644 index f5bf021..0000000 --- a/lucid/cache/src/Client/Memcached.php +++ /dev/null @@ -1,122 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use Memcached as MemcachedClient; - -/** - * @class MemcachedClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Memcached extends AbstractClient -{ - /** - * Memcached instance - * - * @var Memcached - */ - protected $driver; - - /** - * Constructor. - * - * @param Memcached $memcached - * - * @return void - */ - public function __construct(MemcachedClient $memcached) - { - $this->driver = $memcached; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - if (false === $this->driver->get($key)) { - return Memcached::RES_NOTFOUND !== $this->driver->getResultCode(); - } - - return true; - - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $res = $this->driver->get($key); - - return Memcached::RES_NOTFOUND === $this->driver->getResultCode() ? null : $res; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $cmp = $this->driver->getOption(Memcached::OPT_COMPRESSION); - $this->driver->setOption(Memcached::OPT_COMPRESSION, $compressed); - - $cached = $this->driver->set($key, $data, $expires); - $this->driver->setOption(Memcached::OPT_COMPRESSION, $cmp); - - return $cached; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return $this->driver->delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - return $this->driver->flush(); - } - - /** - * {@inheritdoc} - * - * @return int Unix timestamp - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->driver->increment($key, $value); - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->driver->decrement($key, $value); - } -} diff --git a/lucid/cache/src/Client/MemcachedConnection.php b/lucid/cache/src/Client/MemcachedConnection.php deleted file mode 100644 index d47e516..0000000 --- a/lucid/cache/src/Client/MemcachedConnection.php +++ /dev/null @@ -1,112 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Selene\Module\Cache\Client; - -use Memcached; -use RuntimeException; - -/** - * @class MemcachedConnection - * - * @package Selene\Module\Cache\Client - * @version $Id$ - * @author Thomas Appel servers = $servers; - $this->memcached = $memcached ?: new Memcached; - } - - /** - * {@inheritdoc} - */ - public function connect() - { - if ($this->isConnected()) { - return false; - } - - $this->memcached->addServers($this->servers); - - if (!$this->isConnected()) { - throw new RuntimeException('Cannot initialize Memcached'); - } - - return true; - } - - /** - * {@inheritdoc} - */ - public function close() - { - // quit doesn't always close the connection - $this->memcached->quit(); - - return $this->isConnected() ? false : true; - } - - /** - * {@inheritdoc} - */ - public function isConnected() - { - return (bool)$this->memcached->getVersion(); - } - - /** - * {@inheritdoc} - * - * @return Memcached - */ - public function getClient() - { - return $this->memcached; - } - - /** - * {@inheritdoc} - * - * @return Memcached - */ - public function getClientAndConnect() - { - $this->connect(); - - return $this->getClient(); - } -} diff --git a/lucid/cache/src/Client/Redis.php b/lucid/cache/src/Client/Redis.php deleted file mode 100644 index 08014b7..0000000 --- a/lucid/cache/src/Client/Redis.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use Redis as RedisClient; -use Lucid\Cache\CacheInterface; - -/** - * @class RedisClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Redis extends AbstractClient -{ - private $redis; - - /** - * Constructor. - * - * @param redis $client - * - * @return void - */ - public function __construct(RedisClient $redis) - { - $this->redis = $redis; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return false !== $this->redis->get($key); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if (false !== ($res = $this->redis->get($key))) { - return unserialize($res); - } - - return null; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - if (CacheInterface::PERSIST === $expires) { - return $this->saveForever($key, $data, $compressed); - } - - if ($this->redis->set($key, serialize($data))) { - $this->redis->expireAt($key, $expires); - - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return 0 < $this->redis->delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - $this->redis->flushAll(); - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - if (!$this->redis->set($key, serialize($data))) { - return false; - } - - $this->redis->persist($key); - - return true; - } - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->redis->incrBy($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->redis->decrBy($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function getPersistLevel() - { - return CacheInterface::PERSIST; - } -} diff --git a/lucid/cache/src/Client/Xcache.php b/lucid/cache/src/Client/Xcache.php deleted file mode 100644 index 990e3c3..0000000 --- a/lucid/cache/src/Client/Xcache.php +++ /dev/null @@ -1,115 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -use RuntimeException; - -/** - * @class XcacheClient - * @see AbstractClient - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Xcache extends AbstractClient -{ - /** - * Constructor. - */ - public function __construct() - { - if (!extension_loaded('xcache')) { - throw new RuntimeException('XCache extension not loaded.'); - } - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return xcache_isset($key); - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expiry = 0, $compress = false) - { - return xcache_set($key, serialize($data), (int)$expiry); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - return xcache_isset($key) ? unserialize(xcache_get($key)) : null; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return xcache_unset($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - try { - xcache_clear_cache(); - } catch (\Exception $e) { - return false; - } - - return true; - } - - /** - * {@inheritdoc} - * - * @return int `$expires` as seconds. - */ - public function parseExpireTime($expires) - { - return $this->expiryToSeconds($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - if (is_int($inc = xcache_inc($key, (int)$value))) { - return $inc; - } - - return false; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - if (is_int($dec = xcache_dec($key, (int)$value))) { - return $dec; - } - - return false; - } -} diff --git a/lucid/cache/src/ClientInterface.php b/lucid/cache/src/ClientInterface.php deleted file mode 100644 index 124338b..0000000 --- a/lucid/cache/src/ClientInterface.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache; - -/** - * @interface ClientInterface - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -interface ClientInterface -{ - /** - * Flag a file as compressed - * - * @var int - */ - const C_COMPRESSED = 1; - - /** - * Flag a file as uncompressed - * - * @var int - */ - const C_UNCOMPRESSED = 0; - - /** - * Check if item exists and is valid. - * - * @param string $key - * - * @return boolean `TRUE` if exists, otherwise `FALSE` - */ - public function exists($key); - - /** - * Get a cached item by key. - * - * @param string $key - * - * @return mixed The cached content, `null` if item doesn't exist. - */ - public function read($key); - - /** - * Put data to the cache. - * - * @param String $key the cache item identifier - * @param mixed $data Data to be cached - * @param int|string $expires Integer value of the expiry time in minutes or - * a unix date format. - * @param boolean $compressed compress data - * @abstract - * @access public - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function write($key, $data, $expires = 60, $compressed = false); - - /** - * Deletes an item from the cache. - * - * @param string $key the store key - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function delete($key); - - /** - * flushCache - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function flush(); - - /** - * Stores the data with a far future expiry date. - * - * @param String $key the cache item identifier - * @param Mixed $data Data to be cached - * @param boolean $compressed compress data - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function saveForever($key, $data, $compressed = false); - - /** - * increment - * - * @param mixed $key - * @param mixed $value - * - * @return int the incremented value, `FALSE` on failure - */ - public function increment($key, $value); - - /** - * decrement - * - * @param mixed $key - * @param mixed $value - * - * @return int the decremented value, `FALSE` on failure - */ - public function decrement($key, $value); - - /** - * Get default expiry time - * - * @return int time in minutes. - */ - public function getDefaultExpiry(); - - public function parseExpireTime($expires); -} diff --git a/lucid/cache/src/Driver/AbstractDriver.php b/lucid/cache/src/Driver/AbstractDriver.php deleted file mode 100644 index 61391cb..0000000 --- a/lucid/cache/src/Driver/AbstractDriver.php +++ /dev/null @@ -1,189 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -use Lucid\Cache\CacheInterface; - -/** - * @class AbstractDriver - * @see DriverInterface - * @abstract - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -abstract class AbstractDriver implements DriverInterface -{ - - /** - * default cached expiry time in minutes - * - * @var float - * @access public - */ - protected $defaultExpiry = 60; - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, $this->getPersistLevel(), $compressed); - } - - /** - * increment - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @return void - */ - public function increment($key, $value) - { - $this->validateIncrementValue($value); - - return $this->incrementValue($key, $value); - } - - /** - * decrement - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @return void - */ - public function decrement($key, $value) - { - $this->validateIncrementValue($value); - - return $this->decrementValue($key, $value); - } - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToSeconds($expires); - } - - /** - * get default expiry time - */ - public function getDefaultExpiry() - { - return $this->$defaultExpiry; - } - - /** - * validateIncrementValue - * - * @param mixed $value - * - * @throws \InvalidArgumentException - * @access private - * @return void - */ - private function validateIncrementValue($value) - { - if (!is_int($value) || $value < 1) { - throw new \InvalidArgumentException('Value must be Integer and greater that zero'); - } - } - - /** - * incrementValue - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @abstract - * @return mixed - */ - abstract protected function incrementValue($key, $value); - - /** - * decrementValue - * - * @param mixed $key - * @param mixed $value - * - * @access protected - * @abstract - * @return mixed - */ - abstract protected function decrementValue($key, $value); - - /** - * expiryToSeconds - * - * @param int|string $expiry - * - * @return int - */ - protected function expiryToSeconds($expiry) - { - if (is_string($expiry = $this->validateExpires($expiry))) { - return strtotime($expiry) - time(); - } - - return CacheInterface::PERSIST === $expiry ? $this->getPersistLevel() : $expiry * 60; - } - - /** - * expiryToUnixTimestamp - * - * @param mixed $expiry - * - * @return int - */ - protected function expiryToUnixTimestamp($expiry) - { - if (is_string($expiry = $this->validateExpires($expiry))) { - return strtotime($expiry); - } - - return CacheInterface::PERSIST === $expiry ? $this->getPersistLevel() : time() + ($expiry * 60); - } - - /** - * validateExpires - * - * @param mixed $expiry - * - * @return int - */ - protected function validateExpires($expires) - { - if (($str = is_string($expires)) && false === @strtotime($expires)) { - throw new \InvalidArgumentException(sprintf('Invalid expiry time "%s".', $expires)); - } - - return $str ? $expires : (int)$expires; - } - - /** - * getPersistLevel - * - * @return int - */ - protected function getPersistLevel() - { - return 0; - } -} diff --git a/lucid/cache/src/Driver/AbstractSqlDriver.php b/lucid/cache/src/Driver/AbstractSqlDriver.php deleted file mode 100644 index 0cc6173..0000000 --- a/lucid/cache/src/Driver/AbstractSqlDriver.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -use PDO; - -/** - * @class AbstractSqlDriver - * - * @package Lucid\\Cache\Driver - * @version $Id$ - * @author iwyg - */ -class AbstractSqlDriver implements DirverInterface -{ - /** - * client - * - * @var PDO - */ - private $client; - - /** - * Constructor. - * - * @param PDO $client - * - * @return void - */ - public function __construct(PDO $client) - { - $this->client = $client; - } - - /** - * client - * - * - * @return PDO - */ - final protected function client() - { - return $this->client; - } -} diff --git a/lucid/cache/src/Driver/ApcDriver.php b/lucid/cache/src/Driver/ApcDriver.php deleted file mode 100644 index 9874592..0000000 --- a/lucid/cache/src/Driver/ApcDriver.php +++ /dev/null @@ -1,120 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -/** - * @class ApcDriver - * @see AbstractDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class ApcDriver extends AbstractDriver -{ - /** - * Constructor. - */ - public function __construct() - { - if (!extension_loaded('apc')) { - throw new \RuntimeException('Apc extension not loaded.'); - } - } - - /** - * Check if cached item exists - * - * @param Mixed $key - * @return void - */ - public function exists($key) - { - return apc_exists($key); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $res = apc_fetch($key, $success); - - return $success ? $res : null; - } - - /** - * write data to cache - * - * @param String $key the cache item identifier - * @param Mixed $data Data to be cached - * @param Mixed $expires Integer value of the expiry time in minutes or - * @param boolean $compressed compress data - * unix timestamp - * @return void - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - return apc_store($key, $data, $expires); - } - - /** - * delete a cached item - * - * @param string $key - * @return void - */ - public function delete($key) - { - return apc_delete($key); - } - - /** - * delete all cached items - * - * @return boolean - */ - public function flush() - { - return apc_clear_cache('user'); - } - - /** - * incrementValue - * - * @param string $key - * @param int $value - * - * @return int - */ - protected function incrementValue($key, $value) - { - $val = apc_inc($key, $value, $success); - - return $success ? (int)$val : false; - } - - /** - * decrementValue - * - * @param string $key - * @param int $value - * - * @return int - */ - protected function decrementValue($key, $value) - { - $val = apc_dec($key, $value, $success); - - return $success ? (int)$val : false; - } -} diff --git a/lucid/cache/src/Driver/ApcuDriver.php b/lucid/cache/src/Driver/ApcuDriver.php deleted file mode 100644 index eba2461..0000000 --- a/lucid/cache/src/Driver/ApcuDriver.php +++ /dev/null @@ -1,95 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -/** - * @class ApcuDriver - * @see ApcDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class ApcuDriver extends ApcDriver -{ - /** - * Constructor. - */ - public function __construct() - { - if (!extension_loaded('apcu')) { - throw new \RuntimeException('APCu extension not loaded.'); - } - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return apcu_exists($key); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $res = apcu_fetch($key, $success); - - return $success ? $res : null; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - return apcu_store($key, $data, $expires); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return apcu_delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - return apcu_clear_cache('user'); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - $val = apcu_inc($key, $value, $success); - - return $success ? (int)$val : false; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - $val = apcu_dec($key, $value, $success); - - return $success ? (int)$val : false; - } -} diff --git a/lucid/cache/src/Driver/ArrayDriver.php b/lucid/cache/src/Driver/ArrayDriver.php deleted file mode 100644 index 2496879..0000000 --- a/lucid/cache/src/Driver/ArrayDriver.php +++ /dev/null @@ -1,170 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Driver; - -use ArrayObject; - -/** - * @class ArrayDriver - * @see AbstractDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class ArrayDriver extends AbstractDriver -{ - /** - * storage - * - * @var ArrayObject - */ - protected $storage; - - /** - * persist - * - * @var bool - */ - protected $persist; - - /** - * persistPath - * - * @var string - */ - protected $persistPath; - - /** - * @param mixed $persist - * @param string $path - * - */ - public function __construct($persist = false, $path = '') - { - $this->persistPath = $path; - $this->persist = (bool)$persist; - $this->setUpStorage(); - } - - /** - * @return void - */ - public function __destruct() - { - //$this->persistStorage(); - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return isset($this->storage[$key]); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if ($this->exists($key)) { - return $this->storage[$key]; - } - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $this->storage[$key] = $data; - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, 0, $compressed); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - unset($this->storage[$key]); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - unset($this->storage); - - $this->storage = new \ArrayObject; - $this->persistStorage(); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->storage[$key] = (int)$this->storage[$key] + (int)$value; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->storage[$key] = (int)$this->storage[$key] - (int)$value; - } - - /** - * setUpStorage - * - * @return void - */ - private function setUpStorage() - { - if ($this->persist and file_exists($this->persistPath)) { - try { - $this->storage = unserialize(file_get_contents($this->persistPath)); - } catch (\Exception $e) { - $this->storage = new ArrayObject; - } - return; - } - - $this->storage = new ArrayObject; - } - - /** - * persistStorage - * - * - * @return mixed - */ - private function persistStorage() - { - if ($this->persist) { - file_put_contents($this->persistPath, serialize($this->storage), LOCK_EX); - } - } -} diff --git a/lucid/cache/src/Driver/ConnectionInterface.php b/lucid/cache/src/Driver/ConnectionInterface.php deleted file mode 100644 index 9b48afd..0000000 --- a/lucid/cache/src/Driver/ConnectionInterface.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Driver; - -/** - * @interface ConnectionInterface - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -interface ConnectionInterface -{ - /** - * Connect to a server. - * - * @return boolean `TRUE` on success, `FALSE` on failure. - */ - public function connect(); - - /** - * Close connection to a server. - * - * @return boolean `TRUE` on success, `FALSE` on failure. - */ - public function close(); - - /** - * Tell if server is connected. - * - * @return boolean `TRUE` if connected, otherwise `FALSE`. - */ - public function isConnected(); - - /** - * Get the specified server driver. - * - * @return mixed - */ - public function getDriver(); - - /** - * Get the specified server driver and connect. - * - * @return mixed - */ - public function getDriverAndConnect(); -} diff --git a/lucid/cache/src/Driver/DriverInterface.php b/lucid/cache/src/Driver/DriverInterface.php deleted file mode 100644 index b545b6d..0000000 --- a/lucid/cache/src/Driver/DriverInterface.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -/** - * @interface DriverInterface - * - * @package Lucid\Cache\Driver - * @version $Id$ - * @author iwyg - */ -interface DriverInterface -{ - /** - * Flag a file as compressed - * - * @var int - */ - const C_COMPRESSED = 1; - - /** - * Flag a file as uncompressed - * - * @var int - */ - const C_UNCOMPRESSED = 0; - - /** - * Check if item exists and is valid. - * - * @param string $key - * - * @return boolean `TRUE` if exists, otherwise `FALSE` - */ - public function exists($key); - - /** - * Get a cached item by key. - * - * @param string $key - * - * @return mixed The cached content, `null` if item doesn't exist. - */ - public function read($key); - - /** - * Put data to the cache. - * - * @param String $key the cache item identifier - * @param mixed $data Data to be cached - * @param int|string $expires Integer value of the expiry time in minutes or - * a unix date format. - * @param boolean $compressed compress data - * @abstract - * @access public - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function write($key, $data, $expires = 60, $compressed = false); - - /** - * Deletes an item from the cache. - * - * @param string $key the store key - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function delete($key); - - /** - * flushCache - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function flush(); - - /** - * Stores the data with a far future expiry date. - * - * @param String $key the cache item identifier - * @param Mixed $data Data to be cached - * @param boolean $compressed compress data - * - * @return boolean `TRUE` on success, otherwise `FALSE` - */ - public function saveForever($key, $data, $compressed = false); - - /** - * increment - * - * @param mixed $key - * @param mixed $value - * - * @return int the incremented value, `FALSE` on failure - */ - public function increment($key, $value); - - /** - * decrement - * - * @param mixed $key - * @param mixed $value - * - * @return int the decremented value, `FALSE` on failure - */ - public function decrement($key, $value); - - /** - * Get default expiry time - * - * @return int time in minutes. - */ - public function getDefaultExpiry(); - - public function parseExpireTime($expires); -} diff --git a/lucid/cache/src/Driver/FilesystemDriver.php b/lucid/cache/src/Driver/FilesystemDriver.php deleted file mode 100644 index 85d3dfd..0000000 --- a/lucid/cache/src/Driver/FilesystemDriver.php +++ /dev/null @@ -1,269 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Cache\Driver; - -use Lucid\Cache\CacheInterface; - -/** - * @class FilesystemDriver - * @see AbstractDriver - * - * @package Selene\Module\Cache - * @version $Id$ - * @author iwyg - */ -class FilesystemDriver extends AbstractDriver -{ - /** - * cache directory - * - * @var Stream\FSDirectory - * @access protected - */ - protected $cachedir; - - /** - * Constructor. - * - * @param FilesystemInterface $fs - * @param string $location - */ - public function __construct($path) - { - $this->setCacheDir($path); - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - if (!is_file($file = $this->getFilePath($key))) { - return false; - } - - return time() < filemtime($file); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if (!$this->exists($key)) { - return; - } - - list ($data,) = $this->getFileContent($this->getFilePath($key)); - - return $data; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - if (CacheInterface::PERSIST === $expires) { - $expires = strtotime('2037-12-31'); - } - - list($contents, $timestamp) = $this->serializeData($data, $compressed, (int)$expires); - - if (!file_put_contents($file = $this->getFilePath($key), $contents, LOCK_EX)) { - return false; - } - - touch($file, $timestamp); - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - return $this->write($key, $data, CacheInterface::PERSIST === $expires, $compressed); - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - if (false !== @unlink($this->getFilePath($key))) { - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function flush() - { - foreach ($itr = new \DirectoryIterator($this->cachedir) as $path) { - if ($itr->isFile()) { - unlink($itr->getPathname()); - } - } - } - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->setIncrementValue($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->setIncrementValue($key, 0 - (int)$value); - } - - /** - * setIncrementValue - * - * @param mixed $key - * @param mixed $value - * - * @return boolean - */ - private function setIncrementValue($key, $value) - { - if (!$this->exists($key)) { - return false; - } - - list($data, $state) = $this->getFileContent($file = $this->getFilePath($key)); - - $timestamp = filemtime($file); - - list($contents,) = $this->serializeData((int)$data + $value, static::C_UNCOMPRESSED === $state); - - file_put_contents($file, $contents, LOCK_EX); - - touch($file, $timestamp); - - return true; - } - - /** - * getFileContent - * - * @param mixed $file - * - * @access protected - * @return mixed - */ - protected function getFileContent($file) - { - $state = (int)substr($contents = file_get_contents($file), 0, 1); - $data = substr($contents, 2); - - $data = static::C_UNCOMPRESSED === $state ? - $data = unserialize($data) : - unserialize($this->uncompressData($data)); - - return [$data, $state]; - } - - /** - * compressData - * - * @param Mixed $data - * @access private - * @return String base64 string representation of gzip compressed input - * data - */ - private function compressData($data) - { - return base64_encode(gzcompress($data)); - } - - /** - * uncompressData - * - * @param Mixed $data - * @access private - * @return String Mixed contents of the cached item - */ - private function uncompressData($data) - { - return gzuncompress(base64_decode($data)); - } - - /** - * getFilePath - * - * @param mixed $key - * - * @access private - * @return string - */ - private function getFilePath($key) - { - $hash = hash('md5', $key); - - return substr($hash, 0, 4).DIRECTORY_SEPARATOR.substr($hash, 4, 20); - } - - /** - * serializeWithTime - * - * @param Mixed $data - * @param Mixed $time - * @param Mixed $compressed - * @access private - * @return Mixed file contents - */ - private function serializeData($data, $compressed = false, $timestamp = 0) - { - $data = serialize($data); - $data = $compressed ? $this->compressData($data) : $data; - $contents = sprintf('%d;%s', $compressed ? static::C_COMPRESSED : self::C_UNCOMPRESSED, $data); - - return [$contents, $timestamp]; - } - - private function setCacheDir($path) - { - if (!file_exists($path)) { - mkdir($path, 0775 & umask(), true); - } - - $this->cachedir = $path; - } - - /** - * {@inheritdoc} - */ - protected function getPersistLevel() - { - return CacheInterface::PERSIST; - } -} diff --git a/lucid/cache/src/Driver/MemcacheConnection.php b/lucid/cache/src/Driver/MemcacheConnection.php deleted file mode 100644 index 1b97f29..0000000 --- a/lucid/cache/src/Driver/MemcacheConnection.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Selene\Module\Cache\Driver; - -use Memcache; -use RuntimeException; - -/** - * @class MemcacheConnection - * - * @package Selene\Module\Cache\Driver - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -class MemcacheConnection implements ConnectionInterface -{ - /** - * memcached - * - * @var Memcached - * @access private - */ - private $memcached; - - /** - * Connection Status - * - * @var boolean - */ - private $connected; - - /** - * Constructor. - * - * @param array $servers - * @param Memcached $memcached - */ - public function __construct(array $servers, Memcache $memcache = null) - { - $this->memcache = $memcache ?: new Memcache; - $this->servers = $servers; - } - - /** - * {@inheritdoc} - */ - public function connect() - { - if ($this->isConnected()) { - return false; - } - - $this->addServers(); - - try { - $this->memcache->getVersion(); - } catch (\Exception $e) { - throw new RuntimeException('Cannot initialize Memcache: ' . $e->getMessage()); - } - - return $this->connected = true; - } - - /** - * {@inheritdoc} - */ - public function close() - { - $this->connected = false; - - return $this->memcache->close(); - } - - /** - * {@inheritdoc} - */ - public function isConnected() - { - return (bool)$this->connected; - } - - /** - * {@inheritdoc} - * - * @return Memcache - */ - public function getDriver() - { - return $this->memcache; - } - - /** - * {@inheritdoc} - * - * @return Memcache - */ - public function getDriverAndConnect() - { - $this->connect(); - - return $this->getDriver(); - } - - /** - * Adds given server connections to memcache. - * - * @return void - */ - protected function addServers() - { - foreach ($this->servers as $server) { - $this->memcache->addServer($server['host'], (int)$server['port'], true, (int)$server['weight']); - } - } -} diff --git a/lucid/cache/src/Driver/MemcacheDriver.php b/lucid/cache/src/Driver/MemcacheDriver.php deleted file mode 100644 index 4fa15bc..0000000 --- a/lucid/cache/src/Driver/MemcacheDriver.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -use Memcache; - -/** - * @class MemcacheDriver - * @see MemcachedDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class MemcacheDriver extends MemcachedDriver -{ - /** - * Flag stored along with cached items. - * - * @var int - */ - const ITEM_EXISTS = 4; - - /** - * Constructor. - * - * @param Memcache $memcache - */ - public function __construct(Memcache $memcache) - { - $this->driver = $memcache; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - $flags = 0; - $res = $this->driver->get($key, $flags); - - return false !== $res ? true : $this->itemExists($flags); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $flags = 0; - $res = $this->driver->get($key, $flags); - - return $res ? $res : ($this->itemExists($flags) ? $res : null); - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $flags = $compressed ? MEMCACHE_COMPRESSED : 0; - $cached = $this->driver->set($key, $data, $flags | self::ITEM_EXISTS, $expires); - - return $cached; - } - - protected function itemExists(&$flags) - { - return 0 === (self::ITEM_EXISTS & ~$flags); - } -} diff --git a/lucid/cache/src/Driver/MemcachedConnection.php b/lucid/cache/src/Driver/MemcachedConnection.php deleted file mode 100644 index d4c8a0a..0000000 --- a/lucid/cache/src/Driver/MemcachedConnection.php +++ /dev/null @@ -1,112 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Selene\Module\Cache\Driver; - -use Memcached; -use RuntimeException; - -/** - * @class MemcachedConnection - * - * @package Selene\Module\Cache\Driver - * @version $Id$ - * @author Thomas Appel servers = $servers; - $this->memcached = $memcached ?: new Memcached; - } - - /** - * {@inheritdoc} - */ - public function connect() - { - if ($this->isConnected()) { - return false; - } - - $this->memcached->addServers($this->servers); - - if (!$this->isConnected()) { - throw new RuntimeException('Cannot initialize Memcached'); - } - - return true; - } - - /** - * {@inheritdoc} - */ - public function close() - { - // quit doesn't always close the connection - $this->memcached->quit(); - - return $this->isConnected() ? false : true; - } - - /** - * {@inheritdoc} - */ - public function isConnected() - { - return (bool)$this->memcached->getVersion(); - } - - /** - * {@inheritdoc} - * - * @return Memcached - */ - public function getDriver() - { - return $this->memcached; - } - - /** - * {@inheritdoc} - * - * @return Memcached - */ - public function getDriverAndConnect() - { - $this->connect(); - - return $this->getDriver(); - } -} diff --git a/lucid/cache/src/Driver/MemcachedDriver.php b/lucid/cache/src/Driver/MemcachedDriver.php deleted file mode 100644 index 6422d08..0000000 --- a/lucid/cache/src/Driver/MemcachedDriver.php +++ /dev/null @@ -1,131 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -use Memcached; - -/** - * @class MemcachedDriver - * @see AbstractDriver - * - * @package Lucid - * @version $Id$ - * @author iwyg - */ -class MemcachedDriver extends AbstractDriver -{ - /** - * Memcached instance - * - * @var Memcached - */ - protected $driver; - - /** - * Constructor. - * - * @param Memcached $memcached - * - * @return void - */ - public function __construct(Memcached $memcached) - { - $this->driver = $memcached; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - if (false === $this->driver->get($key)) { - return Memcached::RES_NOTFOUND !== $this->driver->getResultCode(); - } - - return true; - - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - $res = $this->driver->get($key); - - return Memcached::RES_NOTFOUND === $this->driver->getResultCode() ? null : $res; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - $cmp = $this->driver->getOption(Memcached::OPT_COMPRESSION); - - $this->driver->setOption(Memcached::OPT_COMPRESSION, $compressed); - - $cached = $this->driver->set($key, $data, $expires); - - $this->driver->setOption(Memcached::OPT_COMPRESSION, $cmp); - - return $cached; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return $this->driver->delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - return $this->driver->flush(); - } - - /** - * {@inheritdoc} - * - * @return int Unix timestamp - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->driver->increment($key, $value); - } - - /** - * incrementValue - * - * @param mixed $key - * @param mixed $value - * - * @access public - * @return void - */ - protected function decrementValue($key, $value) - { - return $this->driver->decrement($key, $value); - } -} diff --git a/lucid/cache/src/Driver/RedisDriver.php b/lucid/cache/src/Driver/RedisDriver.php deleted file mode 100644 index 938de30..0000000 --- a/lucid/cache/src/Driver/RedisDriver.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -use Redis; -use Lucid\Cache\CacheInterface; - -/** - * @class RedisDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class RedisDriver extends AbstractDriver -{ - private $redis; - - /** - * Constructor. - * - * @param redis $client - * - * @return void - */ - public function __construct(Redis $redis) - { - $this->redis = $redis; - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return false !== $this->redis->get($key); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - if (false !== ($res = $this->redis->get($key))) { - return unserialize($res); - } - - return null; - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expires = 60, $compressed = false) - { - if (CacheInterface::PERSIST === $expires) { - return $this->saveForever($key, $data, $compressed); - } - - if ($this->redis->set($key, serialize($data))) { - $this->redis->expireAt($key, $expires); - - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return 0 < $this->redis->delete($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - $this->redis->flushAll(); - - return true; - } - - /** - * {@inheritdoc} - */ - public function saveForever($key, $data, $compressed = false) - { - if (!$this->redis->set($key, serialize($data))) { - return false; - } - - $this->redis->persist($key); - - return true; - } - - /** - * {@inheritdoc} - */ - public function parseExpireTime($expires) - { - return $this->expiryToUnixTimestamp($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - return $this->redis->incrBy($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - return $this->redis->decrBy($key, (int)$value); - } - - /** - * {@inheritdoc} - */ - protected function getPersistLevel() - { - return CacheInterface::PERSIST; - } -} diff --git a/lucid/cache/src/Driver/XcacheDriver.php b/lucid/cache/src/Driver/XcacheDriver.php deleted file mode 100644 index 1210e26..0000000 --- a/lucid/cache/src/Driver/XcacheDriver.php +++ /dev/null @@ -1,113 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -/** - * @class XcacheDriver - * @see AbstractDriver - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class XcacheDriver extends AbstractDriver -{ - /** - * Constructor. - */ - public function __construct() - { - if (!extension_loaded('xcache')) { - throw new \RuntimeException('XCache extension not loaded.'); - } - } - - /** - * {@inheritdoc} - */ - public function exists($key) - { - return xcache_isset($key); - } - - /** - * {@inheritdoc} - */ - public function write($key, $data, $expiry = 0, $compress = false) - { - return xcache_set($key, serialize($data), (int)$expiry); - } - - /** - * {@inheritdoc} - */ - public function read($key) - { - return xcache_isset($key) ? unserialize(xcache_get($key)) : null; - } - - /** - * {@inheritdoc} - */ - public function delete($key) - { - return xcache_unset($key); - } - - /** - * {@inheritdoc} - */ - public function flush() - { - try { - xcache_clear_cache(); - } catch (\Exception $e) { - return false; - } - - return true; - } - - /** - * {@inheritdoc} - * - * @return int `$expires` as seconds. - */ - public function parseExpireTime($expires) - { - return $this->expiryToSeconds($expires); - } - - /** - * {@inheritdoc} - */ - protected function incrementValue($key, $value) - { - if (is_int($inc = xcache_inc($key, (int)$value))) { - return $inc; - } - - return false; - } - - /** - * {@inheritdoc} - */ - protected function decrementValue($key, $value) - { - if (is_int($dec = xcache_dec($key, (int)$value))) { - return $dec; - } - - return false; - } -} diff --git a/lucid/cache/src/Section.php b/lucid/cache/src/Section.php deleted file mode 100644 index 5a87f5b..0000000 --- a/lucid/cache/src/Section.php +++ /dev/null @@ -1,173 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache; - -use Closure; - -/** - * @class Section - * @see CacheInterface - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Section implements CacheInterface, SectionableInterface -{ - /** @var CacheInterface */ - private $cache; - - /** @var string */ - private $section; - - /** - * Constructor. - * - * @param CacheInterface $storage - * @param string $section - */ - public function __construct(CacheInterface $storage, $section) - { - $this->cache = $storage; - $this->section = $section; - } - - /** - * {@inheritdoc} - */ - public function get($key, $default = null) - { - if ($value = $this->cache->get($this->getItemKey($key))) { - return $value; - } - - return $default; - } - - /** - * {@inheritdoc} - */ - public function set($key, $data, $expires = null, $compressed = false) - { - return $this->cache->set($this->getItemKey($key), $data, $expires, $compressed); - } - - /** - * {@inheritdoc} - */ - public function persist($key, $data, $compressed = false) - { - return $this->cache->persist($this->getItemKey($key), $data, $compressed); - } - - /** - * {@inheritdoc} - */ - public function section($section) - { - return new static($this, $section); - } - - /** - * {@inheritdoc} - */ - public function increment($key, $value = 1) - { - return $this->cache->increment($this->getItemKey($key), $value); - } - - /** - * {@inheritdoc} - */ - public function decrement($key, $value = 1) - { - return $this->cache->decrement($this->getItemKey($key), $value); - } - - /** - * {@inheritdoc} - */ - public function purge($key = null) - { - if (null === $key) { - return $this->cache->increment($this->getKey()); - } - - return $this->cache->purge($this->getItemKey($key)); - } - - /** - * {@inheritdoc} - */ - public function has($key) - { - return null !== $this->get($key); - } - - /** - * {@inheritdoc} - */ - public function setUsing($key, callable $callback, $expires = null, $compressed = false) - { - if ($this->has($key)) { - return $this->get($key); - } - - $this->set($key, $data = $callback(), $expires, $compressed); - - return $data; - } - - /** - * {@inheritdoc} - */ - public function persistUsing($key, callable $callback, $compressed = false) - { - return $this->persist($key, call_user_func($callback), $compressed); - } - - /** - * getKey - * - * @access protected - * @return mixed - */ - protected function getKey() - { - return sprintf('section:%s:key', $this->section); - } - - /** - * getItemKey - * - * - * @return mixed - */ - protected function getItemKey($key) - { - return sprintf('%s:%s:%s', $this->getSectionKey(), $this->section, $key); - } - - /** - * getSectionKey - * - * @return mixed - */ - protected function getSectionKey() - { - if (null === ($key = $this->cache->get($skey = $this->getKey()))) { - $this->cache->persist($skey, $key = rand(1, 10000)); - } - - return $key; - } -} diff --git a/lucid/cache/src/Storage.php b/lucid/cache/src/Storage.php deleted file mode 100644 index 1c9b28d..0000000 --- a/lucid/cache/src/Storage.php +++ /dev/null @@ -1,243 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache; - -use Closure; -use ArrayAccess; - -/** - * @class Storage - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class Storage implements CacheInterface, SectionableInterface, ArrayAccess -{ - /** @var array */ - private $pool = []; - - /** @var ClientInterface */ - private $driver; - - /** @var string */ - private $prefix; - - /** - * Constructor. - * - * @param ClientInterface $client - * @param string $prefix cache id prefix - */ - public function __construct(ClientInterface $client, $prefix = 'cache') - { - $this->client = $client; - $this->setCachePrefix($prefix); - } - - /** - * {@inheritdoc} - */ - public function has($key) - { - if (isset($this->pool[$key = $this->getPrefixed($key)])) { - return true; - } - - return $this->client->exists($key); - } - - /** - * {@inheritdoc} - */ - public function get($key, $default = null) - { - if (isset($this->pool[$key = $this->getPrefixed($key)])) { - return $this->pool[$key]; - } - - if ($data = $this->client->read($key)) { - return $data; - } - - return $default; - } - - /** - * {@inheritdoc} - */ - public function set($key, $data, $expires = 60, $compressed = false) - { - $expires = $this->client->parseExpireTime($expires); - - if ($this->client->write($key = $this->getPrefixed($key), $data, $expires, $compressed)) { - $this->pool[$key] = $this->client->read($key); - - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function persist($key, $data, $compressed = false) - { - return $this->client->saveForever($this->getPrefixed($key), $data, $compressed); - } - - /** - * {@inheritdoc} - */ - public function setUsing($key, callable $callback, $expires = null, $compressed = false) - { - $this->set($key, $default = $this->execDefaultVal($key, $callback), $expires, $compressed); - - return $default; - - } - - /** - * {@inheritdoc} - */ - public function persistUsing($key, callable $callback, $compressed = false) - { - $this->persist($key, $default = $this->execDefaultVal($key, $callback), $compressed); - - return $default; - } - - /** - * {@inheritdoc} - */ - public function purge($key = null) - { - if (null === ($key)) { - $this->pool = []; - - return $this->client->flush(); - } - - if ($del = $this->client->delete($key = $this->getPrefixed($key))) { - unset($this->pool[$key]); - } - - return (bool)$del; - } - - /** - * {@inheritdoc} - */ - public function increment($key, $value = 1) - { - if ($this->client->increment($key = $this->getPrefixed($key), $value)) { - unset($this->pool[$key]); - } - } - - /** - * {@inheritdoc} - */ - public function decrement($key, $value = 1) - { - if ($this->client->decrement($key = $this->getPrefixed($key), $value)) { - unset($this->pool[$key]); - } - } - - /** - * {@inheritdoc} - */ - public function section($section) - { - return new Section($this, $section); - } - - /** - * {@inheritdoc} - * - * @see CacheInterface::has() - */ - public function offsetExists($offset) - { - return $this->has($offset); - } - - /** - * {@inheritdoc} - * - * @see CacheInterface::purge() - */ - public function offsetUnset($offset) - { - return $this->purge($offset); - } - - /** - * {@inheritdoc} - * - * @see CacheInterface::set() - */ - public function offsetSet($offset, $value) - { - return $this->set($offset, $value); - } - - /** - * {@inheritdoc} - * - * @see CacheInterface::get() - */ - public function offsetGet($offset) - { - return $this->get($offset); - } - - /** - * getPrefixed - * - * @param Mixed $key - * @return void - */ - protected function getPrefixed($key) - { - return null !== $this->prefix ? sprintf("%s_%s", $this->prefix, $key) : $key; - } - - /** - * setCachePrefix - * - * @param Mixed $prefix - * @return void - */ - protected function setCachePrefix($prefix = null) - { - $this->prefix = $prefix; - } - - /** - * execDefaultVal - * - * @param Mixed $key - * @param Closure $callback - * @return void - */ - protected function execDefaultVal($key, callable $callback) - { - if ($this->has($key)) { - return $this->get($key); - } - - return call_user_func($callback); - } -} diff --git a/lucid/cache/src/Util/Time.php b/lucid/cache/src/Util/Time.php deleted file mode 100644 index 6d59c61..0000000 --- a/lucid/cache/src/Util/Time.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Util; - -/** - * @class Time - * - * @package Lucid\Cache\Util - * @version $Id$ - * @author iwyg - */ -final class Time -{ - - public function validateExpires($expires) - { - } - - public function expiryToUnixTimestamp($expiry) - { - if (is_string($expiry = self::validateExpires($expiry))) { - return strtotime($expiry); - } - - return CacheInterface::PERSIST === $expiry ? $this->getPersistLevel() : time() + ($expiry * 60); - } -} diff --git a/lucid/cache/tests/Client/AbstractClientTest.php b/lucid/cache/tests/Client/AbstractClientTest.php deleted file mode 100644 index c867340..0000000 --- a/lucid/cache/tests/Client/AbstractClientTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -/** - * @class ClientTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -abstract class AbstractClientTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof( - 'Lucid\Cache\ClientInterface', - $this->newClient() - ); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowOnInvalidDateFormat() - { - $driver = $this->newClient(); - - $driver->parseExpireTime('foo bar'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowIfIncrementsIsString() - { - $driver = $this->newClient(); - - $driver->increment('item.fails', '12'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowIfIncrementsIsInvalid() - { - $driver = $this->newClient(); - - $driver->increment('item.fails', -1); - } - - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - $driver = $this->newClient(); - - $this->assertFalse($driver->exists('item.fails')); - } - - /** @test */ - public function itShouldReturnTrueIfItemExists() - { - $this->assertTrue($this->newClient()->exists('item.exists')); - } - - /** @test */ - public function itShouldFetchStoredItems() - { - $this->assertSame('exists', $this->newClient()->read('item.exists')); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - $this->assertNull($this->newClient()->read('item.fails')); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - $driver = $this->newClient(); - - $this->assertTrue($driver->write('item.success', 'data', $time)); - $this->assertFalse($driver->write('item.fails', 'data', $time)); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - $driver = $this->newClient(); - - $this->assertTrue($driver->delete('item.success')); - $this->assertFalse($driver->delete('item.fails')); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - $driver = $this->newClient(); - - $this->assertSame(2, $driver->increment('item.inc', 1)); - $this->assertFalse($driver->increment('item.fails', 1)); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - $driver = $this->newClient(); - - $this->assertSame(0, $driver->decrement('item.dec', 1)); - $this->assertFalse($driver->decrement('item.fails', 1)); - } - - /** @test */ - abstract public function flushingCacheShouldReturnBoolean(); - - abstract protected function newClient(); -} diff --git a/lucid/cache/tests/Client/ApcuTest.php b/lucid/cache/tests/Client/ApcuTest.php deleted file mode 100644 index 6e47e16..0000000 --- a/lucid/cache/tests/Client/ApcuTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -use Lucid\Cache\Client\Apcu; - -/** - * @class ApcuTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class ApcuTest extends AbstractClientTest -{ - /** @test */ - public function itShouldParseMinutesToSeconts() - { - $driver = $this->newClient(); - - $this->assertSame(60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToSeconds() - { - $driver = $this->newClient(); - - $this->assertSame(60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function storingItemsShouldReturnBoolean() - { - $driver = $this->newClient(); - - $this->assertTrue($driver->saveForever('item.exists', 'data')); - $this->assertFalse($driver->saveForever('item.fails', 'data')); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $driver = $this->newClient(); - - $this->assertTrue($driver->flush()); - } - - public function timeProvider() - { - return [ - [60] - ]; - } - - protected function newClient() - { - return new Apcu; - } - - protected function setUp() - { - include_once dirname(__DIR__).'/Fixures/helper.php'; - include_once dirname(__DIR__).'/Fixures/apcuhelper.php'; - } -} diff --git a/lucid/cache/tests/Client/FilesystemTest.php b/lucid/cache/tests/Client/FilesystemTest.php deleted file mode 100644 index 0385f1d..0000000 --- a/lucid/cache/tests/Client/FilesystemTest.php +++ /dev/null @@ -1,127 +0,0 @@ -newClient()->write('item.exists', 'exists', time() + 2000); - - $driver = $this->newClient(); - $this->assertTrue($driver->flush()); - } - - /** @test */ - public function itShouldReturnTrueIfItemExists() - { - $this->newClient()->write('item.exists', 'exists', time() + 2000); - parent::itShouldReturnTrueIfItemExists(); - } - - /** @test */ - public function itShouldFetchStoredItems() - { - $this->newClient()->write('item.exists', 'exists', time() + 2000); - parent::itShouldFetchStoredItems(); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - $driver = $this->newClient(); - - $this->assertTrue($driver->write('item.success', 'data', $time)); - //$this->assertFalse($driver->write('item.fails', 'data', $time)); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - $driver = $this->newClient(); - $driver->write('item.exists', 'data', time() + 2000); - - $this->assertFalse($driver->delete('item.fails')); - $this->assertTrue($driver->delete('item.exists')); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - $driver = $this->newClient(); - $driver->write('item.inc', 1, time() + 2000); - - parent::itShouldReturnIncrementedValue(); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - $driver = $this->newClient(); - $driver->write('item.dec', 1, time() + 2000); - - parent::itShouldReturnDecrementedValue(); - } - - public function timeProvider() - { - return [ - [60] - ]; - } - - protected function newClient() - { - return new Filesystem($this->getCachePath()); - } - - protected function tearDown() - { - if (null !== $this->path) { - $this->rmDir($this->path); - $this->path = null; - } - - parent::tearDown(); - } - - private function rmDir($cpath) - { - $itr = new \FilesystemIterator($cpath, \FilesystemIterator::SKIP_DOTS); - - foreach ($itr as $path => $item) { - - if ($item->isDir()) { - $this->rmDir($path); - } - - if ($item->isFile()) { - unlink($path); - } - } - - rmdir($cpath); - } - - private function getCachePath() - { - if (null === $this->path) { - $this->path = sys_get_temp_dir() . '/fscachetest' . time(); - } - - if (!is_dir($this->path)) { - mkdir($this->path, 0777, true); - } - - return $this->path; - } -} diff --git a/lucid/cache/tests/Client/MemcacheTest.php b/lucid/cache/tests/Client/MemcacheTest.php deleted file mode 100644 index b3d2011..0000000 --- a/lucid/cache/tests/Client/MemcacheTest.php +++ /dev/null @@ -1,142 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -use Memcache; -use Lucid\Cache\Client\Memcache as MemcacheClient; - -/** - * @class MemcachedClientTest - * - * @package Lucid\Cache\Tests\Client - * @version $Id$ - * @author iwyg - */ -class MemcacheTest extends MemcachedTest -{ - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - list (, $mc) = $this->getClient(); - - $mc->method('get')->willReturn(false); - - return ClientTest::itShouldReturnFalseIfItemDoesNotExist(); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - list (, $mc) = $this->getClient(); - $mc->method('get')->with('item.fails')->willReturn(false); - - return ClientTest::itShouldReturnNullIfItemDoesNotExist(); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - list (, $mc) = $this->getClient(); - $flags = 0 | Memcache::ITEM_EXISTS; - $map = [ - ['item.success', 'data', $flags, $time, true], - ['item.fails', 'data',$flags, $time, false] - ]; - - $mc->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($map)); - - return ClientTest::itShouldReturnBooleanWhenStoringItems($time); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - list ($driver, $mc) = $this->getClient(); - - $map = [ - ['item.success', true], - ['item.fails', false] - ]; - - $mc->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($map)); - - return ClientTest::itShouldReturnBooleanWhenDeletingItems(); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - list (, $mc) = $this->getClient(); - $map = [ - ['item.inc', 1, 2], - ['item.fails', 1, false] - ]; - - $mc->expects($this->any()) - ->method('increment') - ->will($this->returnValueMap($map)); - - return ClientTest::itShouldReturnIncrementedValue(); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - list (, $mc) = $this->getClient(); - $map = [ - ['item.dec', 1, 0], - ['item.fails', 1, false] - ]; - - $mc->expects($this->any()) - ->method('decrement') - ->will($this->returnValueMap($map)); - - return ClientTest::itShouldReturnDecrementedValue(); - } - - protected function getMemcache() - { - $methods = ['get', 'set', 'delete', 'flush', 'increment', 'decrement']; - - return $this->getMock('Memcache', $methods); - } - - protected function getClient() - { - if (null === $this->driver) { - $this->driver = new Memcache($this->mc = $this->getMemcache()); - } - - return [$this->driver, $this->mc]; - } - - protected function newClient() - { - list($driver, ) = $this->getClient(); - - return $driver; - } - - protected function tearDown() - { - $this->mc = null; - $this->driver = null; - } -} diff --git a/lucid/cache/tests/Client/MemcachedTest.php b/lucid/cache/tests/Client/MemcachedTest.php deleted file mode 100644 index 21fa531..0000000 --- a/lucid/cache/tests/Client/MemcachedTest.php +++ /dev/null @@ -1,230 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -use Memcached; -use Lucid\Cache\Client\Memcached as MemcachedClient; - -/** - * @class MemcachedClientTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class MemcachedTest extends AbstractClientTest -{ - /** @var Memcached */ - private $mc; - - /** @var Lucid\Cache\Client\ClientInterface */ - protected $driver; - - /** @test */ - public function itShouldParseMinutesToUnixTimestamp() - { - $driver = $this->newClient(); - - $this->assertSame(time() + 60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToUnixTimestamp() - { - $driver = $this->newClient(); - - $this->assertSame(time() + 60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - list (, $mc) = $this->getClient(); - - - return parent::itShouldReturnFalseIfItemDoesNotExist(); - } - - /** @test */ - public function itShouldReturnTrueIfItemExists() - { - list (, $mc) = $this->getClient(); - $mc->method('get')->willReturn(true); - - return parent::itShouldReturnTrueIfItemExists(); - } - - /** @test */ - public function itShouldFetchStoredItems() - { - list (, $mc) = $this->getClient(); - $mc->method('get')->with('item.exists')->willReturn('exists'); - - return parent::itShouldFetchStoredItems(); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - list (, $mc) = $this->getClient(); - $mc->method('get')->with('item.fails')->willReturn(false); - $mc->method('getResultCode')->willReturn(\Memcached::RES_NOTFOUND); - - return parent::itShouldReturnNullIfItemDoesNotExist(); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - list (, $mc) = $this->getClient(); - $map = [ - ['item.success', 'data', $time, true], - ['item.fails', 'data', $time, false] - ]; - - $mc->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnBooleanWhenStoringItems($time); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - list (, $mc) = $this->getClient(); - - $map = [ - ['item.success', null, true], - ['item.fails', null, false] - ]; - - $mc->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnBooleanWhenDeletingItems(); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - list ($driver, $mc) = $this->getClient(); - $mc->method('flush')->willReturn(true); - - $this->assertTrue($driver->flush()); - - $this->mc = null; - $this->driver = null; - - list ($driver, $mc) = $this->getClient(); - $driver = new MemcachedClient($mc = $this->getMemcached()); - $mc->method('flush')->willReturn(false); - - $this->assertFalse($driver->flush()); - } - - /** @test */ - public function memcachedShouldReceiveZeroEpireytime() - { - $driver = new MemcachedClient($mc = $this->getMemcached()); - $mc->method('set')->with('item.success', 'data', 0)->willReturn(true); - - $this->assertTrue($driver->saveForever('item.success', 'data')); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - list (, $mc) = $this->getClient(); - $map = [ - ['item.inc', 1, null, null, 2], - ['item.fails', 1, null, null, false] - ]; - - $mc->expects($this->any()) - ->method('increment') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnIncrementedValue(); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - list (, $mc) = $this->getClient(); - $map = [ - ['item.dec', 1, null, null, 0], - ['item.fails', 1, null, null, false] - ]; - - $mc->expects($this->any()) - ->method('decrement') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnDecrementedValue(); - } - - public function timeProvider($time) - { - return [ - [time() + 60] - ]; - } - - - protected function getClient() - { - if (null === $this->driver) { - $this->driver = new MemcachedClient($this->mc = $this->getMemcached()); - } - - return [$this->driver, $this->mc]; - } - - protected function newClient() - { - list($driver, ) = $this->getClient(); - - return $driver; - } - - protected function tearDown() - { - $this->mc = null; - $this->driver = null; - } - - protected function setUp() - { - $this->markTestSkipped( - 'Skipping tests until https://github.com/php-memcached-dev/php-memcached/issues/126 is resolved.' - ); - // $this->mc = null; - // $this->driver = null; - } - - /** - * libmemcached currently doesn't expose its correct api to the reflection - * api of php, thus resulting in foul mock objects. - * @see https://github.com/php-memcached-dev/php-memcached/issues/126 - * - * @return Memcached - */ - protected function getMemcached() - { - return $this->getMock('Memcached', ['get', 'set', 'delete', 'flush', 'increment', 'decrement']); - } -} diff --git a/lucid/cache/tests/Client/RedisTest.php b/lucid/cache/tests/Client/RedisTest.php deleted file mode 100644 index 1ee822c..0000000 --- a/lucid/cache/tests/Client/RedisTest.php +++ /dev/null @@ -1,133 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -use Lucid\Cache\CacheInterface; -use Lucid\Cache\Client\Redis as RedisClient; - -/** - * @class RedisClientTest - * - * @package Lucid\Cache\Tests\Client - * @version $Id$ - * @author iwyg - */ -class RedisTest extends AbstractClientTest -{ - protected $rd; - protected $driver; - - /** @test */ - public function itShouldParseMinutesToUnixTimestamp() - { - $driver = $this->newClient(); - - $this->assertSame(time() + 60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToUnixTimestamp() - { - $driver = $this->newClient(); - - $this->assertSame(time() + 60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $this->assertTrue($this->newClient()->flush()); - } - - /** @test */ - public function persistingShouldReturnBoolean() - { - $driver = $this->newClient(); - - $this->assertTrue($driver->write('item.success', 'data', CacheInterface::PERSIST)); - $this->assertTrue($driver->saveforever('item.success', 'data')); - - $this->assertFalse($driver->write('item.fails', 'data', CacheInterface::PERSIST)); - $this->assertFalse($driver->saveforever('item.fails', 'data')); - } - - public function timeProvider() - { - return [ - [time() + 60] - ]; - } - - protected function newClient() - { - return $this->driver = new RedisClient($this->getRedis()); - } - - protected function getRedis() - { - $mock = $this->rd = $this->getMock( - 'Redis', - ['get', 'set', 'persist', 'expireAt', 'delete', 'flushAll', 'incrBy', 'decrBy'] - ); - - $get = [ - ['item.fails', false], - ['item.exists', serialize('exists')] - ]; - - $set = [ - ['item.fails', serialize('data'), false], - ['item.success', serialize('data'), true] - ]; - - $delete = [ - ['item.fails', false], - ['item.success', true] - ]; - - $increment = [ - ['item.inc', 1, 2], - ['item.fails', 1, false] - ]; - - $decrement = [ - ['item.dec', 1, 0], - ['item.fails', 1, false] - ]; - - $mock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($get)); - - $mock->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($set)); - - $mock->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($delete)); - - $mock->expects($this->any()) - ->method('incrBy') - ->will($this->returnValueMap($increment)); - - $mock->expects($this->any()) - ->method('decrBy') - ->will($this->returnValueMap($decrement)); - - $mock->expects($this->any()) - ->method('flushAll') - ->willReturn(true); - - return $mock; - } -} diff --git a/lucid/cache/tests/Client/XcacheTest.php b/lucid/cache/tests/Client/XcacheTest.php deleted file mode 100644 index 76c8f6c..0000000 --- a/lucid/cache/tests/Client/XcacheTest.php +++ /dev/null @@ -1,68 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Client; - -use ReflectionClass; -use Lucid\Cache\Client\Xcache; - -/** - * @class XcacheClientTest - * @see ClientTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class XcacheTest extends AbstractClientTest -{ - /** @test */ - public function itShouldParseMinutesToSeconts() - { - $driver = $this->newClient(); - $this->assertSame(60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToSeconds() - { - $driver = $this->newClient(); - $this->assertSame(60, $driver->parseExpireTime('60 seconds')); - } - - /** - * {@inheritdoc} - */ - public function timeProvider() - { - return [ - [60] - ]; - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $driver = $this->newClient(); - $this->assertTrue($driver->flush()); - } - - protected function newClient() - { - return (new ReflectionClass('Lucid\Cache\Client\Xcache'))->newInstanceWithoutConstructor(); - } - - protected function setUp() - { - include_once dirname(__DIR__).'/Fixures/helper.php'; - include_once dirname(__DIR__).'/Fixures/xcachehelper.php'; - } -} diff --git a/lucid/cache/tests/Driver/ApcDriverTest.php b/lucid/cache/tests/Driver/ApcDriverTest.php deleted file mode 100644 index 2387ded..0000000 --- a/lucid/cache/tests/Driver/ApcDriverTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Lucid\Cache\Driver\ApcDriver; - -/** - * @class ApcDriverTest - * - * @package Lucid\Cache\Tests\Driver - * @version $Id$ - * @author iwyg - */ -class ApcDriverTest extends DriverTest -{ - /** @test */ - public function itShouldParseMinutesToSeconts() - { - $driver = $this->newDriver(); - - $this->assertSame(60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToSeconds() - { - $driver = $this->newDriver(); - - $this->assertSame(60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function storingItemsShouldReturnBoolean() - { - $driver = $this->newDriver(); - - $this->assertTrue($driver->saveForever('item.exists', 'data')); - $this->assertFalse($driver->saveForever('item.fails', 'data')); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $driver = $this->newDriver(); - - $this->assertTrue($driver->flush()); - } - - public function timeProvider() - { - return [ - [60] - ]; - } - - protected function newDriver() - { - return new ApcDriver; - } - - protected function setUp() - { - include_once dirname(__DIR__).'/Fixures/helper.php'; - include_once dirname(__DIR__).'/Fixures/apchelper.php'; - } -} diff --git a/lucid/cache/tests/Driver/ApcuDriverTest.php b/lucid/cache/tests/Driver/ApcuDriverTest.php deleted file mode 100644 index 579fec9..0000000 --- a/lucid/cache/tests/Driver/ApcuDriverTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Lucid\Cache\Driver\ApcuDriver; - -/** - * @class ApcDriverTest - * - * @package Lucid\Cache\Tests\Driver - * @version $Id$ - * @author iwyg - */ -class ApcuDriverTest extends ApcDriverTest -{ - public function timeProvider() - { - return [ - [60] - ]; - } - - protected function newDriver() - { - return new ApcuDriver; - } - - protected function setUp() - { - include_once dirname(__DIR__).'/Fixures/helper.php'; - include_once dirname(__DIR__).'/Fixures/apcuhelper.php'; - } -} diff --git a/lucid/cache/tests/Driver/DriverTest.php b/lucid/cache/tests/Driver/DriverTest.php deleted file mode 100644 index af57309..0000000 --- a/lucid/cache/tests/Driver/DriverTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -/** - * @class DriverTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -abstract class DriverTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof( - 'Lucid\Cache\Driver\DriverInterface', - $this->newDriver() - ); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowOnInvalidDateFormat() - { - $driver = $this->newDriver(); - - $driver->parseExpireTime('foo bar'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowIfIncrementsIsString() - { - $driver = $this->newDriver(); - - $driver->increment('item.fails', '12'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowIfIncrementsIsInvalid() - { - $driver = $this->newDriver(); - - $driver->increment('item.fails', -1); - } - - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - $driver = $this->newDriver(); - - $this->assertFalse($driver->exists('item.fails')); - } - - /** @test */ - public function itShouldReturnTrueIfItemExists() - { - $this->assertTrue($this->newDriver()->exists('item.exists')); - } - - /** @test */ - public function itShouldFetchStoredItems() - { - $this->assertSame('exists', $this->newDriver()->read('item.exists')); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - $this->assertNull($this->newDriver()->read('item.fails')); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - $driver = $this->newDriver(); - - $this->assertTrue($driver->write('item.success', 'data', $time)); - $this->assertFalse($driver->write('item.fails', 'data', $time)); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - $driver = $this->newDriver(); - - $this->assertTrue($driver->delete('item.success')); - $this->assertFalse($driver->delete('item.fails')); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - $driver = $this->newDriver(); - - $this->assertSame(2, $driver->increment('item.inc', 1)); - $this->assertFalse($driver->increment('item.fails', 1)); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - $driver = $this->newDriver(); - - $this->assertSame(0, $driver->decrement('item.dec', 1)); - $this->assertFalse($driver->decrement('item.fails', 1)); - } - - /** @test */ - abstract public function flushingCacheShouldReturnBoolean(); - - abstract protected function newDriver(); -} diff --git a/lucid/cache/tests/Driver/MemcacheDriverTest.php b/lucid/cache/tests/Driver/MemcacheDriverTest.php deleted file mode 100644 index 49ba01d..0000000 --- a/lucid/cache/tests/Driver/MemcacheDriverTest.php +++ /dev/null @@ -1,141 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Lucid\Cache\Driver\MemcacheDriver; - -/** - * @class MemcachedDriverTest - * - * @package Lucid\Cache\Tests\Driver - * @version $Id$ - * @author iwyg - */ -class MemcacheDriverTest extends MemcachedDriverTest -{ - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - list (, $mc) = $this->getDriver(); - - $mc->method('get')->willReturn(false); - - return DriverTest::itShouldReturnFalseIfItemDoesNotExist(); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - list (, $mc) = $this->getDriver(); - $mc->method('get')->with('item.fails')->willReturn(false); - - return DriverTest::itShouldReturnNullIfItemDoesNotExist(); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - list (, $mc) = $this->getDriver(); - $flags = 0 | MemcacheDriver::ITEM_EXISTS; - $map = [ - ['item.success', 'data', $flags, $time, true], - ['item.fails', 'data',$flags, $time, false] - ]; - - $mc->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($map)); - - return DriverTest::itShouldReturnBooleanWhenStoringItems($time); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - list ($driver, $mc) = $this->getDriver(); - - $map = [ - ['item.success', true], - ['item.fails', false] - ]; - - $mc->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($map)); - - return DriverTest::itShouldReturnBooleanWhenDeletingItems(); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - list (, $mc) = $this->getDriver(); - $map = [ - ['item.inc', 1, 2], - ['item.fails', 1, false] - ]; - - $mc->expects($this->any()) - ->method('increment') - ->will($this->returnValueMap($map)); - - return DriverTest::itShouldReturnIncrementedValue(); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - list (, $mc) = $this->getDriver(); - $map = [ - ['item.dec', 1, 0], - ['item.fails', 1, false] - ]; - - $mc->expects($this->any()) - ->method('decrement') - ->will($this->returnValueMap($map)); - - return DriverTest::itShouldReturnDecrementedValue(); - } - - protected function getMemcache() - { - $methods = ['get', 'set', 'delete', 'flush', 'increment', 'decrement']; - - return $this->getMock('Memcache', $methods); - } - - protected function getDriver() - { - if (null === $this->driver) { - $this->driver = new MemcacheDriver($this->mc = $this->getMemcache()); - } - - return [$this->driver, $this->mc]; - } - - protected function newDriver() - { - list($driver, ) = $this->getDriver(); - - return $driver; - } - - protected function tearDown() - { - $this->mc = null; - $this->driver = null; - } -} diff --git a/lucid/cache/tests/Driver/MemcachedDriverTest.php b/lucid/cache/tests/Driver/MemcachedDriverTest.php deleted file mode 100644 index be03065..0000000 --- a/lucid/cache/tests/Driver/MemcachedDriverTest.php +++ /dev/null @@ -1,229 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Memcached; -use Lucid\Cache\Driver\MemcachedDriver; - -/** - * @class MemcachedDriverTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class MemcachedDriverTest extends DriverTest -{ - /** @var Memcached */ - private $mc; - - /** @var Lucid\Cache\Driver\DriverInterface */ - protected $driver; - - /** @test */ - public function itShouldParseMinutesToUnixTimestamp() - { - $driver = $this->newDriver(); - - $this->assertSame(time() + 60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToUnixTimestamp() - { - $driver = $this->newDriver(); - - $this->assertSame(time() + 60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function itShouldReturnFalseIfItemDoesNotExist() - { - list (, $mc) = $this->getDriver(); - - - return parent::itShouldReturnFalseIfItemDoesNotExist(); - } - - /** @test */ - public function itShouldReturnTrueIfItemExists() - { - list (, $mc) = $this->getDriver(); - $mc->method('get')->willReturn(true); - - return parent::itShouldReturnTrueIfItemExists(); - } - - /** @test */ - public function itShouldFetchStoredItems() - { - list (, $mc) = $this->getDriver(); - $mc->method('get')->with('item.exists')->willReturn('exists'); - - return parent::itShouldFetchStoredItems(); - } - - /** @test */ - public function itShouldReturnNullIfItemDoesNotExist() - { - list (, $mc) = $this->getDriver(); - $mc->method('get')->with('item.fails')->willReturn(false); - $mc->method('getResultCode')->willReturn(\Memcached::RES_NOTFOUND); - - return parent::itShouldReturnNullIfItemDoesNotExist(); - } - - /** - * @test - * @dataProvider TimeProvider - */ - public function itShouldReturnBooleanWhenStoringItems($time) - { - list (, $mc) = $this->getDriver(); - $map = [ - ['item.success', 'data', $time, true], - ['item.fails', 'data', $time, false] - ]; - - $mc->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnBooleanWhenStoringItems($time); - } - - /** @test */ - public function itShouldReturnBooleanWhenDeletingItems() - { - list (, $mc) = $this->hashgetDriver(); - - $map = [ - ['item.success', null, true], - ['item.fails', null, false] - ]; - - $mc->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnBooleanWhenDeletingItems(); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - list ($driver, $mc) = $this->getDriver(); - $mc->method('flush')->willReturn(true); - - $this->assertTrue($driver->flush()); - - $this->mc = null; - $this->driver = null; - - list ($driver, $mc) = $this->getDriver(); - $driver = new MemcachedDriver($mc = $this->getMemcached()); - $mc->method('flush')->willReturn(false); - - $this->assertFalse($driver->flush()); - } - - /** @test */ - public function memcachedShouldReceiveZeroEpireytime() - { - $driver = new MemcachedDriver($mc = $this->getMemcached()); - $mc->method('set')->with('item.success', 'data', 0)->willReturn(true); - - $this->assertTrue($driver->saveForever('item.success', 'data')); - } - - /** @test */ - public function itShouldReturnIncrementedValue() - { - list (, $mc) = $this->getDriver(); - $map = [ - ['item.inc', 1, null, null, 2], - ['item.fails', 1, null, null, false] - ]; - - $mc->expects($this->any()) - ->method('increment') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnIncrementedValue(); - } - - /** @test */ - public function itShouldReturnDecrementedValue() - { - list (, $mc) = $this->getDriver(); - $map = [ - ['item.dec', 1, null, null, 0], - ['item.fails', 1, null, null, false] - ]; - - $mc->expects($this->any()) - ->method('decrement') - ->will($this->returnValueMap($map)); - - return parent::itShouldReturnDecrementedValue(); - } - - public function timeProvider($time) - { - return [ - [time() + 60] - ]; - } - - - protected function getDriver() - { - if (null === $this->driver) { - $this->driver = new MemcachedDriver($this->mc = $this->getMemcached()); - } - - return [$this->driver, $this->mc]; - } - - protected function newDriver() - { - list($driver, ) = $this->getDriver(); - - return $driver; - } - - protected function tearDown() - { - $this->mc = null; - $this->driver = null; - } - - protected function setUp() - { - $this->mc = null; - $this->driver = null; - $this->markTestIncomplete(); - } - - /** - * libmemcached currently doesn't expose its correct api to the reflection - * api of php, thus resulting in foul mock objects. - * @see https://github.com/php-memcached-dev/php-memcached/issues/126 - * - * @return Memcached - */ - private function getMemcached() - { - //return new Memcached; - return $this->getMockBuilder('Memcached')->disableOriginalConstructor()->getMock(); - } -} diff --git a/lucid/cache/tests/Driver/RedisDriverTest.php b/lucid/cache/tests/Driver/RedisDriverTest.php deleted file mode 100644 index 4b03a71..0000000 --- a/lucid/cache/tests/Driver/RedisDriverTest.php +++ /dev/null @@ -1,133 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Lucid\Cache\CacheInterface; -use Lucid\Cache\Driver\RedisDriver; - -/** - * @class RedisDriverTest - * - * @package Lucid\Cache\Tests\Driver - * @version $Id$ - * @author iwyg - */ -class RedisDriverTest extends DriverTest -{ - protected $rd; - protected $driver; - - /** @test */ - public function itShouldParseMinutesToUnixTimestamp() - { - $driver = $this->newDriver(); - - $this->assertSame(time() + 60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToUnixTimestamp() - { - $driver = $this->newDriver(); - - $this->assertSame(time() + 60, $driver->parseExpireTime('60 seconds')); - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $this->assertTrue($this->newDriver()->flush()); - } - - /** @test */ - public function persistingShouldReturnBoolean() - { - $driver = $this->newDriver(); - - $this->assertTrue($driver->write('item.success', 'data', CacheInterface::PERSIST)); - $this->assertTrue($driver->saveforever('item.success', 'data')); - - $this->assertFalse($driver->write('item.fails', 'data', CacheInterface::PERSIST)); - $this->assertFalse($driver->saveforever('item.fails', 'data')); - } - - public function timeProvider() - { - return [ - [time() + 60] - ]; - } - - protected function newDriver() - { - return $this->driver = new RedisDriver($this->getRedis()); - } - - protected function getRedis() - { - $mock = $this->rd = $this->getMock( - 'Redis', - ['get', 'set', 'persist', 'expireAt', 'delete', 'flushAll', 'incrBy', 'decrBy'] - ); - - $get = [ - ['item.fails', false], - ['item.exists', serialize('exists')] - ]; - - $set = [ - ['item.fails', serialize('data'), false], - ['item.success', serialize('data'), true] - ]; - - $delete = [ - ['item.fails', false], - ['item.success', true] - ]; - - $increment = [ - ['item.inc', 1, 2], - ['item.fails', 1, false] - ]; - - $decrement = [ - ['item.dec', 1, 0], - ['item.fails', 1, false] - ]; - - $mock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($get)); - - $mock->expects($this->any()) - ->method('set') - ->will($this->returnValueMap($set)); - - $mock->expects($this->any()) - ->method('delete') - ->will($this->returnValueMap($delete)); - - $mock->expects($this->any()) - ->method('incrBy') - ->will($this->returnValueMap($increment)); - - $mock->expects($this->any()) - ->method('decrBy') - ->will($this->returnValueMap($decrement)); - - $mock->expects($this->any()) - ->method('flushAll') - ->willReturn(true); - - return $mock; - } -} diff --git a/lucid/cache/tests/Driver/XcacheDriverTest.php b/lucid/cache/tests/Driver/XcacheDriverTest.php deleted file mode 100644 index d724516..0000000 --- a/lucid/cache/tests/Driver/XcacheDriverTest.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests\Driver; - -use Lucid\Cache\Driver\XcacheDriver; - -/** - * @class XcacheDriverTest - * @see DriverTest - * - * @package Lucid\Cache - * @version $Id$ - * @author iwyg - */ -class XcacheDriverTest extends DriverTest -{ - /** @test */ - public function itShouldParseMinutesToSeconts() - { - $driver = $this->newDriver(); - $this->assertSame(60, $driver->parseExpireTime(1)); - } - - /** @test */ - public function itShouldParseDateToSeconds() - { - $driver = $this->newDriver(); - $this->assertSame(60, $driver->parseExpireTime('60 seconds')); - } - - public function timeProvider() - { - return [ - [60] - ]; - } - - /** @test */ - public function flushingCacheShouldReturnBoolean() - { - $driver = $this->newDriver(); - $this->assertTrue($driver->flush()); - } - - protected function newDriver() - { - return new XcacheDriver; - } - - protected function setUp() - { - include_once dirname(__DIR__).'/Fixures/helper.php'; - include_once dirname(__DIR__).'/Fixures/xcachehelper.php'; - } -} diff --git a/lucid/cache/tests/Fixures/apchelper.php b/lucid/cache/tests/Fixures/apchelper.php deleted file mode 100644 index d0d7433..0000000 --- a/lucid/cache/tests/Fixures/apchelper.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -function apc_exists($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apc_store($key, $data, $ttl = 0) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apc_fetch($key, &$success) -{ - if ('item.fails' === $key) { - $success = false; - return false; - } - - $success = true; - - if ('item.exists' === $key) { - return 'exists'; - } - - return 'item'; -} - -function apc_delete($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apc_inc($key, $value, &$success = null) -{ - if ('item.fails' === $key) { - $success = false; - return false; - } - - $success = true; - - return 1 + $value; -} - -function apc_dec($key, $value, &$success = null) -{ - if ('item.fails' === $key) { - $success = false; - return false; - } - - $success = true; - - return 1 - $value; -} - -function apc_clear_cache($type = null) -{ - return true; -} diff --git a/lucid/cache/tests/Fixures/apcuhelper.php b/lucid/cache/tests/Fixures/apcuhelper.php deleted file mode 100644 index bd688fc..0000000 --- a/lucid/cache/tests/Fixures/apcuhelper.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -function apcu_exists($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apcu_store($key, $data, $ttl = 0) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apcu_fetch($key, &$success) -{ - if ('item.fails' === $key) { - $success = false; - - return false; - } - - $success = true; - - if ('item.exists' === $key) { - return 'exists'; - } - - return 'item'; -} - -function apcu_delete($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function apcu_inc($key, $value, &$success = null) -{ - if ('item.fails' === $key) { - $success = false; - return false; - } - - $success = true; - - return 1 + $value; -} - -function apcu_dec($key, $value, &$success = null) -{ - if ('item.fails' === $key) { - $success = false; - return false; - } - - $success = true; - - return 1 - $value; -} - -function apcu_clear_cache($type = null) -{ - return true; -} diff --git a/lucid/cache/tests/Fixures/helper.php b/lucid/cache/tests/Fixures/helper.php deleted file mode 100644 index 74ed91d..0000000 --- a/lucid/cache/tests/Fixures/helper.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -function extension_loaded($ext) -{ - if (in_array($ext, ['apc', 'apcu', 'xcache'])) { - return true; - } - - return call_user_func_array('extension_loaded', func_get_args()); -} diff --git a/lucid/cache/tests/Fixures/xcachehelper.php b/lucid/cache/tests/Fixures/xcachehelper.php deleted file mode 100644 index ae79b77..0000000 --- a/lucid/cache/tests/Fixures/xcachehelper.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Client; - -function xcache_isset($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function xcache_get($key) -{ - if ('item.fails' === $key) { - return false; - } - - if ('item.exists' === $key) { - return serialize('exists'); - } - - return serialize('item'); -} - -function xcache_set($key, $value) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function xcache_unset($key) -{ - if ('item.fails' === $key) { - return false; - } - - return true; -} - -function xcache_inc($key, $value) -{ - if ('item.fails' === $key) { - return false; - } - - return 1 + $value; -} - -function xcache_dec($key, $value) -{ - if ('item.fails' === $key) { - return false; - } - - return 1 - $value; -} - -function xcache_clear_cache() -{ - return true; -} diff --git a/lucid/cache/tests/SectionTest.php b/lucid/cache/tests/SectionTest.php deleted file mode 100644 index e5f3c39..0000000 --- a/lucid/cache/tests/SectionTest.php +++ /dev/null @@ -1,129 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests; - -use Lucid\Cache\Section; -use Lucid\Cache\Storage; - -/** - * @class SectionTest - * @package Selene\Module\Cache\Tests - * @version $Id$ - */ -class SectionTest extends \PHPUnit_Framework_TestCase -{ - protected $cache; - - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Cache\SectionableInterface', new Section($this->mockStorage(), 'section')); - } - - /** @test */ - public function itShouldReturnSection() - { - $this->assertInstanceof( - 'Lucid\Cache\Section', - $this->newSection()->section('test') - ); - } - - /** @test */ - public function itShouldSetValue() - { - $section = $this->newSection(); - $this->cache->method('get')->with('section:section:key')->willReturn('rnd'); - $this->cache->method('set')->with('rnd:section:key', 'data', '', false)->willReturn(true); - - $this->assertTrue($section->set('key', 'data')); - } - - /** @test */ - public function itShouldIncrement() - { - $section = $this->newSection(); - $this->cache->method('get')->with('section:section:key')->willReturn('rnd'); - $this->cache->method('increment')->with('rnd:section:key', 1)->willReturn(1); - - $this->assertSame(1, $section->increment('key')); - } - - /** @test */ - public function itShouldDecrement() - { - $section = $this->newSection(); - $this->cache->method('get')->with('section:section:key')->willReturn('rnd'); - $this->cache->method('decrement')->with('rnd:section:key', 1)->willReturn(1); - - $this->assertSame(1, $section->decrement('key')); - } - - /** - * @test - * @dataProvider cmpProvider - */ - public function itShouldSeal($compress) - { - $section = $this->newSection(); - $this->cache->method('get')->with('section:section:key')->willReturn('rnd'); - $this->cache->method('persist')->with('rnd:section:key', 'data', $compress)->willReturn(true); - - $this->assertTrue($section->persist('key', 'data', $compress)); - } - - /** - * @test - * @dataProvider cmpProvider - */ - public function itShouldSealDefault($compress) - { - $callback = function () { - return 'data'; - }; - - $section = $this->newSection(); - $this->cache->method('get')->with('section:section:key')->willReturn('rnd'); - $this->cache->method('persist')->with('rnd:section:key', 'data', $compress)->willReturn(true); - - $this->assertTrue($section->persistUsing('key', $callback, $compress)); - } - - public function cmpProvider() - { - return [ - [true], - [false], - ]; - } - - protected function newSection($name = 'section', $storage = null) - { - if (null !== $storage) { - return new Section($storage, $name); - } - - return new Section($this->cache = $this->mockStorage(), $name); - } - - protected function mockStorage() - { - return $this->getMockbuilder('\Lucid\Cache\CacheInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - //protected function setUp() - //{ - //$this->markTestIncomplete('replace mockery first'); - //} -} diff --git a/lucid/cache/tests/StorageTest.php b/lucid/cache/tests/StorageTest.php deleted file mode 100644 index 7b80284..0000000 --- a/lucid/cache/tests/StorageTest.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Tests; - -use Lucid\Cache\Storage; -use Lucid\Cache\Client\InMemory; - -/** - * @class CacheTest - * - * @package Lucid\Cache\Tests - * @version $Id$ - * @author iwyg - */ -class StorageTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof( - 'Lucid\Cache\CacheInterface', - $cache = $this->newCache() - ); - - $this->assertInstanceof( - 'Lucid\Cache\SectionableInterface', - $cache - ); - } - - /** @test */ - public function itShouldCheckForItemsToExist() - { - $cache = $this->newCache(); - - $cache->set('item.exists', 'item'); - - $this->assertFalse($cache->has('item.fails')); - $this->assertTrue($cache->has('item.exists')); - } - - /** @test */ - public function itShouldRetreiveItemsFromCache() - { - $cache = $this->newCache(); - - $cache->set('item.exists', 'item'); - - $this->assertNull($cache->get('item.fails')); - $this->assertSame('item', $cache->get('item.exists')); - } - - /** @test */ - public function itShouldRetreiveDefaultValues() - { - $this->assertSame('item', $this->newCache()->get('item.fails', 'item')); - } - - /** @test */ - public function itShouldBeSectionAble() - { - $cache = $this->newCache(); - - $this->assertInstanceof( - 'Lucid\Cache\SectionableInterface', - $sec = $cache->section('sect') - ); - - $this->assertFalse($cache === $sec); - } - - protected function newCache() - { - return new Storage(new InMemory(false)); - } -} diff --git a/lucid/cache/tests/Stubs/Memcached.php b/lucid/cache/tests/Stubs/Memcached.php deleted file mode 100644 index 424c9a3..0000000 --- a/lucid/cache/tests/Stubs/Memcached.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Cache\Driver; - -/** - * @class Memcached - * - * @package Lucid\Cache\Tests\Stubs - * @version $Id$ - * @author iwyg - */ -class Memcached extends \Memcached -{ -} diff --git a/lucid/common/.coveralls.yml b/lucid/common/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/common/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/common/.phpunit b/lucid/common/.phpunit deleted file mode 100755 index 379530d..0000000 --- a/lucid/common/.phpunit +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then - ./vendor/bin/phpunit --verbose; -else - ./vendor/bin/phpunit --verbose --coverage-text --coverage-clover /tmp/coverage/coverage.xml -fi diff --git a/lucid/common/.travis.yml b/lucid/common/.travis.yml deleted file mode 100644 index 5feedc8..0000000 --- a/lucid/common/.travis.yml +++ /dev/null @@ -1,38 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - allow_failures: - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/common/LICENSE.md b/lucid/common/LICENSE.md deleted file mode 100644 index 4196805..0000000 --- a/lucid/common/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright (c) 2013-2014 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/common/README.md b/lucid/common/README.md deleted file mode 100644 index fb90049..0000000 --- a/lucid/common/README.md +++ /dev/null @@ -1,21 +0,0 @@ -# Shared utilities for lucid/* packages - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/common/tree/master) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/common/blob/master/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/common/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/common) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/common/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/common) -[![HHVM](https://img.shields.io/hhvm/lucid/common/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/common) - -## Requirements - -``` -php >= 5.6 -``` - -## Installation - -```bash -$ composer require lucid/common -``` diff --git a/lucid/common/composer.json b/lucid/common/composer.json deleted file mode 100644 index c8c1615..0000000 --- a/lucid/common/composer.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "lucid/common", - "license": "MIT", - "description": "Shared utilities for lucid/* packages", - "keywords": ["strings", "arrays", "structs", "queues", "lists", "getters"], - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require": { - "php":">=5.6.0" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev" - }, - "autoload": { - "psr-4": { - "Lucid\\Common\\":"src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Common\\Tests\\":"tests/" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/common/phpunit.xml.dist b/lucid/common/phpunit.xml.dist deleted file mode 100644 index 7218cd4..0000000 --- a/lucid/common/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/common/src/Contract/Arrayable.php b/lucid/common/src/Contract/Arrayable.php deleted file mode 100644 index 8479d31..0000000 --- a/lucid/common/src/Contract/Arrayable.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Contract; - -/** - * @interface Arrayable - * - * @package lucid/common - * @version $Id$ - * @author iwyg - */ -interface Arrayable -{ - public function toArray(); -} diff --git a/lucid/common/src/Contract/Cachable.php b/lucid/common/src/Contract/Cachable.php deleted file mode 100644 index 452c744..0000000 --- a/lucid/common/src/Contract/Cachable.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Contract; - -/** - * @interface Cachable - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -interface Cachable -{ - /** - * store - * - * @return void - */ - public function store(); - - /** - * restore - * - * @return mixed - */ - public function restore(); -} diff --git a/lucid/common/src/Contract/Jsonable.php b/lucid/common/src/Contract/Jsonable.php deleted file mode 100644 index 21cf0bc..0000000 --- a/lucid/common/src/Contract/Jsonable.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Contract; - -/** - * @interface Arrayable - * - * @package lucid/common - * @version $Id$ - * @author iwyg - */ -interface Jsonable -{ - public function toJson(); -} diff --git a/lucid/common/src/Helper/Arr.php b/lucid/common/src/Helper/Arr.php deleted file mode 100644 index b1718f1..0000000 --- a/lucid/common/src/Helper/Arr.php +++ /dev/null @@ -1,240 +0,0 @@ - - * - * For full copyright and limense information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Helper; - -use RecursiveArrayIterator; -use RecursiveIteratorIterator; - -/** - * @class Arr - * @final - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -final class Arr -{ - /** @var string */ - const NS_SEPARATOR = '.'; - - /** - * Flattens a multidimensional array. - * - * @param array $array - * - * @return array - */ - public static function flatten(array $array) - { - $out = []; - - foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $item) { - $out[$key] = $item; - } - - return $out; - } - - /** - * column - * - * @param array $array - * @param string $key - * @param mixed $index - * - * @return array - */ - public static function column(array $array, $key, $index = null) - { - return array_column($array, $key, $index); - } - - /** - * Plucks values by key from a list of arrays or objects. - * - * @param array $array - * @param string $key - * - * @return array - */ - public static function pluck(array $array, $key) - { - return array_map(function ($item) use ($key) { - return is_object($item) ? $item->$key : $item[$key]; - }, $array); - } - - /** - * Zips to or more arrays - * - * @return void - */ - public static function zip(...$args) - { - $args = array_values($args); - $count = count($args); - - $out = []; - - for ($i = 0; $i < $count; $i++) { - $out[] = self::pluck($args, $i); - } - - return $out; - } - - /** - * Get the highest value. - * - * @param array $args - * - * @return int - */ - public static function max(array $args) - { - return count(call_user_func_array('max', $args)); - } - - /** - * Get the lowest value. - * - * @param array $args - * - * @return int - */ - public static function min(array $args) - { - return count(call_user_func_array('min', $args)); - } - - /** - * Determines if a given array is an indexed list. - * - * @param array $array - * @param bool $strict - * - * @return boolean - */ - public static function isList(array $array, $strict = false) - { - $isNumbers = ctype_digit(implode('', $keys = array_keys($array))); - - if (!$strict) { - return $isNumbers; - } - - if (!$isNumbers) { - return false; - } - - return $keys === range(0, count($array) - 1); - } - - /** - * Getter for multidimensional arrays. - * - * @param array $array - * @param string|null $namespace - * @param string $separator - * - * @return mixed - */ - public static function get(array $array, $namespace = null, $separator = self::NS_SEPARATOR) - { - if (!is_string($namespace)) { - return $array; - } - - $keys = explode($separator, $namespace); - - while (count($keys) > 0 && !is_null($array)) { - $key = array_shift($keys); - $array = isset($array[$key]) ? $array[$key] : null; - } - - return $array; - } - - /** - * Sets a segmented string to an array - * - * @param string $namespace - * @param mixed $value - * @param array $array - * @param string $separator - * - * @return array - */ - public static function set(array &$input, $namespace, $value, $separator = self::NS_SEPARATOR) - { - $keys = explode($separator, $namespace); - $pointer = &$input; - - while (count($keys) > 0) { - $key = array_shift($keys); - $pointer[$key] = isset($pointer[$key]) ? $pointer[$key] : []; - $pointer = &$pointer[$key]; - } - - $pointer = $value; - - return $input; - } - - /** - * Unsets a value from a multidimensional array - * - * @param array $array - * @param string $namespace - * @param string $separator - * - * @return void - */ - public static function unsetKey(array &$array, $namespace, $separator = self::NS_SEPARATOR) - { - if (!is_string($namespace)) { - return $array; - } - - $keys = explode($separator, $namespace); - - while (($count = count($keys)) > 0 && !is_null($array)) { - $key = array_shift($keys); - if (isset($array[$key])) { - if ($count < 2) { - unset($array[$key]); - } else { - $array =& $array[$key]; - } - } - } - } - - /** - * Filters out boolish items that resemble none "truthy" values. - * - * @return array - */ - public static function compact($array, $reindex = false) - { - $out = array_filter($array, function ($item) { - return false !== (bool)$item; - }); - - return false !== $reindex && self::isList($out) ? array_values($out) : $out; - } - - private function __construct() - { - } -} diff --git a/lucid/common/src/Helper/Str.php b/lucid/common/src/Helper/Str.php deleted file mode 100644 index bbbe24a..0000000 --- a/lucid/common/src/Helper/Str.php +++ /dev/null @@ -1,174 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Helper; - -use RuntimeException; -use InvalidArgumentException; - -/** - * @class Str - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -final class Str -{ - /** - * rchars - * - * @var string - */ - private static $rchars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - - /** - * lowDash - * - * @param string $string - * @param string $delim - * - * @return string - */ - public static function lowDash($string, $delim = '_') - { - return self::snakeCase($string, $delim); - } - - /** - * snakeCase - * - * @param string $string the string to convert to snake case. - * @param string $delim world delimiter, defaults to '_' - * - * @return string - */ - public static function snakeCase($string, $delim = '_') - { - return strtolower(preg_replace('#[A-Z]#', $delim.'$0', lcfirst($string))); - } - - /** - * camelcase notataion - * - * @param string $str - * @param array[string => string] $replacement - * - * @return string - */ - public static function camelCase($string, array $replacement = ['-' => ' ', '_' => ' ']) - { - return lcfirst(self::camelCaseAll($string, $replacement)); - } - - /** - * all camelcase notataion - * - * @param string $string - * @param array $replacement - * - * @return string - */ - public static function camelCaseAll($string, array $replacement = ['-' => ' ', '_' => ' ']) - { - return strtr(ucwords(strtr($string, $replacement)), [' ' => '']); - } - - /** - * Compares two strings using strcmp. - * - * @param string $string - * @param string $input - * - * @return boolean - */ - public static function equals($string, $input) - { - return 0 === strcmp($string, $input); - } - - /** - * Safe compare two strings. - * - * @param string $string - * @param string $input - * - * @return boolean - */ - public static function safeCmp($string, $input) - { - $pad = static::rand(4); - - $string .= $pad; - $input .= $pad; - - $strLen = mb_strlen($string); - $inpLen = mb_strlen($input); - - $result = $strLen ^ $inpLen; - - for ($i = 0; $i < $inpLen; $i++) { - $result |= (ord($string[$i % $strLen]) ^ ord($input[$i])); - } - - return 0 === $result; - } - - /** - * Generates random strings of a given length. - * - * @param int $length - * - * @return string - */ - public static function rand($length) - { - if (!is_int($length)) { - throw new InvalidArgumentException( - sprintf( - '%s::%s() expects first argument to be integer, instead saw %s.', - __CLASS__, - __METHOD__, - gettype($length) - ) - ); - } - - if (!function_exists('openssl_random_pseudo_bytes')) { - return self::quickRand($length); - } - - if (null === ($bytes = openssl_random_pseudo_bytes($length * 2))) { - throw new RuntimeException('Cannot generate random string'); - } - - return substr(str_replace(['/', '=', '+'], '', base64_encode($bytes)), 0, $length); - } - - /** - * Quick generation of pseudo-random strings. - * - * @param int $length - * - * @return string - */ - public static function quickRand($length) - { - return substr(str_shuffle(str_repeat(static::$rchars, 5)), 0, $length); - } - - /** - * Disable Constructor. - */ - private function __construct() - { - } -} diff --git a/lucid/common/src/Struct/Items.php b/lucid/common/src/Struct/Items.php deleted file mode 100644 index 9171f44..0000000 --- a/lucid/common/src/Struct/Items.php +++ /dev/null @@ -1,191 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Common\Struct; - -use Countable; -use ArrayAccess; -use Serializable; -use ArrayIterator; -use IteratorAggregate; -use InvalidArgumentException; - -/** - * @class Items - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -class Items implements ListInterface, ArrayAccess, Countable, Serializable, IteratorAggregate -{ - /** @var array */ - private $data; - - /** - * Constructor. - * - * @param mixed $args - */ - public function __construct(...$args) - { - $this->data = $args; - } - - /** - * {@inheritdoc} - */ - public function push($value) - { - $this->data[] = $value; - } - - /** - * {@inheritdoc} - */ - public function insert($index, $value) - { - array_splice($this->data, (int)$index, 0, $value); - } - - /** - * {@inheritdoc} - */ - public function pop($index = null) - { - return null === $index ? array_pop($this->data) : current(array_splice($this->data, $index, 1)); - } - - /** - * {@inheritdoc} - */ - public function remove($value) - { - if (false === ($index = array_search($value, $this->data, true))) { - throw new InvalidArgumentException('index out of bounds'); - } - - $this->pop($index); - } - - /** - * {@inheritdoc} - */ - public function countValue($value) - { - return count(array_filter($this->data, function ($item) use ($value) { - return $value === $item; - })); - } - - /** - * {@inheritdoc} - */ - public function count() - { - return count($this->data); - } - - /** - * {@inheritdoc} - */ - public function sort() - { - sort($this->data); - } - - /** - * {@inheritdoc} - */ - public function reverse() - { - $this->data = array_reverse($this->data); - } - - /** - * {@inheritdoc} - */ - public function extend(ListInterface $list) - { - $args = $list->toArray(); - array_unshift($args, null); - $args[0] = &$this->data; - - call_user_func_array('array_push', $args); - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return $this->data; - } - - /** - * {@inheritdoc} - */ - public function offsetSet($offset, $value) - { - $this->push($value); - } - - /** - * {@inheritdoc} - */ - public function offsetGet($offset) - { - return $this->data[$offset]; - } - - /** - * {@inheritdoc} - */ - public function offsetUnset($offset) - { - unset($this->data[$offset]); - } - - /** - * {@inheritdoc} - */ - public function offsetExists($offset) - { - return isset($this->data[$offset]); - } - - /** - * {@inheritdoc} - */ - public function getIterator() - { - return new ArrayIterator($this->data); - } - - /** - * {@inheritdoc} - */ - public function serialize() - { - return serialize($this->data); - } - - /** - * {@inheritdoc} - */ - public function unserialize($data) - { - $this->data = unserialize($data); - - return $this; - } -} diff --git a/lucid/common/src/Struct/ListInterface.php b/lucid/common/src/Struct/ListInterface.php deleted file mode 100644 index 23c3702..0000000 --- a/lucid/common/src/Struct/ListInterface.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Struct; - -/** - * @class ListInterface - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -interface ListInterface -{ - /** - * push - * - * @param mixed $value - * - * @return void - */ - public function push($value); - - /** - * Insert a value at a given index. - * - * @param int $index the index - * @param mixed $value the value to insert. - * - * @return void - */ - public function insert($index, $value); - - /** - * Removes a value from the end of the list - * - * If an index is given the value at the index is removed instead. - * - * @param int $index the index - * - * @return mixed the value specified by the index or the last one in the - * list. - */ - public function pop($index = null); - - /** - * Removes an item in the by its value. - * - * @param mixed $value the value to be removed. - * - * @return void - */ - public function remove($value); - - /** - * Gets the amount of values that you are looking for. - * - * @param mixed $value the value to search. - * - * @return int the amount - */ - public function countValue($value); - - /** - * sort - * - * @return mixed - */ - public function sort(); - - /** - * reverse - * - * @return mixed - */ - public function reverse(); - - /** - * extend - * - * @param ListStruct $list - * - * @return mixed - */ - public function extend(ListInterface $list); -} diff --git a/lucid/common/src/Struct/PriorityQueue.php b/lucid/common/src/Struct/PriorityQueue.php deleted file mode 100644 index 308539f..0000000 --- a/lucid/common/src/Struct/PriorityQueue.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Struct; - -use SplPriorityQueue; - -/** - * @class PriorityQueue - * @see \SplPriorityQueue - * - * @package Lucid\Common\Data - * @version $Id$ - * @author iwyg - */ -class PriorityQueue extends SplPriorityQueue -{ - /** - * queueOrder - * - * @var int - */ - private $queueOrder; - - /** - * Constructor. - */ - public function __construct() - { - $this->queueOrder = PHP_INT_MAX; - } - - /** - * {@inheritdoc} - */ - public function insert($data, $priority) - { - if (is_int($priority)) { - $priority = [$priority, $this->queueOrder--]; - } - - parent::insert($data, $priority); - } -} diff --git a/lucid/common/src/Struct/ReversePriorityQueue.php b/lucid/common/src/Struct/ReversePriorityQueue.php deleted file mode 100644 index ee03e02..0000000 --- a/lucid/common/src/Struct/ReversePriorityQueue.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Struct; - -/** - * @class ReversePriorityQueue - * @see PriorityQueue - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -class ReversePriorityQueue extends PriorityQueue -{ - /** - * {@inheritdoc} - */ - public function compare($prio1, $prio2) - { - list($p1, $order1) = (array)$prio1; - list($p2, $order2) = (array)$prio2; - - if ($p1 === $p2) { - return (int)$order1 > (int)$order2 ? 1 : -1; - } - - return $p1 < $p2 ? 1 : -1; - } -} diff --git a/lucid/common/src/Traits/Getter.php b/lucid/common/src/Traits/Getter.php deleted file mode 100644 index f922d34..0000000 --- a/lucid/common/src/Traits/Getter.php +++ /dev/null @@ -1,90 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Traits; - -use Closure; - -/** - * @trait Getter - * - * @package lucid/common - * @version $Id$ - * @author iwyg - */ -trait Getter -{ - /** - * Gets a value from a given associative array. - * - * If the key is not present in the input array, the default value will be returned. - * - * @param array $pool The input array - * @param string $attr the key to get from the input array - * @param mixed $default the default value - * - * @return mixed Result derived from `$pool`, otherwise `$default`. - */ - protected function getDefault(array $pool, $attr, $default = null) - { - return array_key_exists($attr, $pool) ? $pool[$attr] : $default; - } - - /** - * Gets a value from a given associative array. - * - * If the key is not present in the input array, a callback will be called to retreive the - * default value. - * - * @param array $pool The input array - * @param string $attr the key to get from the input array - * @param \Closure $default the default getter - * - * @return mixed Result derived from `$pool`, otherwise results from `$default`. - */ - protected function getDefaultUsing(array $pool, $attr, Closure $default) - { - return array_key_exists($attr, $pool) ? $pool[$attr] : $default(); - } - - /** - * Gets a value from a given associative array. - * - * If the value is not set, the default value will be returned. - * - * @param array $pool The input array - * @param string $attr the key to get from the input array - * @param mixed $default the default value - * - * @return mixed Result derived from `$pool`, otherwise `$default`. - */ - protected function getStrictDefault(array $pool, $attr, $default = null) - { - return isset($pool[$attr]) ? $pool[$attr] : $default; - } - - /** - * Gets a value from a given associative array. - * - * If the value is not set, a callback will be called to retreive the - * default value. - * - * @param array $pool The input array - * @param string $attr the key to get from the input array - * @param \Closure $default the default getter - * - * @return mixed Result derived from `$pool`, otherwise results from `$default`. - */ - protected function getStrictDefaultUsing(array $pool, $attr, Closure $default) - { - return isset($pool[$attr]) ? $pool[$attr] : $default(); - } -} diff --git a/lucid/common/tests/Helper/ArrTest.php b/lucid/common/tests/Helper/ArrTest.php deleted file mode 100644 index 40f2b62..0000000 --- a/lucid/common/tests/Helper/ArrTest.php +++ /dev/null @@ -1,210 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Tests\Helper; - -use Lucid\Common\Helper\Arr; - -/** - * @class ArrTest - * - * @package Lucid\Common\Tests\Helper - * @version $Id$ - * @author iwyg - */ -class ArrTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function flatten() - { - $this->assertSame(['b' => 'c'], Arr::flatten(['a' => ['b' => 'c']])); - $this->assertSame(['c', 'd' => 'e'], Arr::flatten([['b' => ['c']], ['d' => 'e']])); - } - - /** @test */ - public function compact() - { - $subj = [0, 1, 'yep', true, null]; - - $this->assertSame([1 => 1, 2 => 'yep', 3 => true], Arr::compact($subj)); - $this->assertSame([1, 'yep', true], Arr::compact($subj, true)); - } - - /** @test */ - public function column() - { - $in = [ - [ - 'id' => '12', - 'name' => 'rand', - 'handle' => 'xkd23', - ], - [ - 'id' => '14', - 'name' => 'band', - 'handle' => 'xkd25', - ], - [ - 'id' => '22', - 'name' => 'land', - 'handle' => 'xkd77', - ], - ]; - - $this->assertEquals(['12', '14', '22'], Arr::column($in, 'id')); - $this->assertEquals( - ['xkd23' => '12', 'xkd25' => '14', 'xkd77' => '22'], - Arr::column($in, 'id', 'handle') - ); - } - - /** @test */ - public function isList() - { - $this->assertTrue(Arr::isList([1, 2, 3])); - $this->assertTrue(Arr::isList([0 => 1, 1 => 2, 2 => 3])); - $this->assertFalse(Arr::isList(['a' => 1, 'b' => 2])); - $this->assertFalse(Arr::isList([1 => 1, 2 => 2, 3 => 3], true)); - $this->assertFalse(Arr::isList(["0" => 1, "1" => 2, "bla" => 3], true)); - } - - /** @test */ - public function testMax() - { - $this->assertEquals(3, Arr::max([['a', 'b', 'c'], ['A', 'C'], [1, 2, 3]])); - } - - /** @test */ - public function testMin() - { - $this->assertEquals(2, Arr::min([['a', 'b', 'c'], ['A', 'C'], [1, 2, 3]])); - } - - /** @test */ - public function testArrayGetShouldReturnNullOnUnknowenKeys() - { - $this->assertNull(Arr::get(['foo' => ['bar' => 'baz']], 'baz.bar')); - $this->assertNull(Arr::get(['foo' => ['bar' => 'baz']], 'fo.baz')); - } - - /** - * @test - * @dataProvider arrayGetDataProvider - */ - public function testArrayGet($query, $array, $expected) - { - $this->assertEquals($expected, Arr::get($array, $query)); - } - - /** @test */ - public function testArrayGetReturnInput() - { - $this->assertEquals(['a' => 'b'], Arr::get(['a' => 'b'])); - $this->assertEquals(['a' => 'b'], Arr::get(['a' => 'b'])); - } - - /** @test */ - public function set() - { - $array = []; - Arr::set($array, 'foo', 'bar'); - Arr::set($array, 'service.location.locale', 'en'); - Arr::set($array, 'service.location.name', 'myservice'); - Arr::set($array, 'service.namespace', 'myserviceNS'); - Arr::set($array, 'service.location.0', 'in1'); - Arr::set($array, 'service.location.1', 'in2'); - - $this->assertTrue(isset($array['foo']) && $array['foo'] === 'bar'); - $this->assertTrue(isset($array['service'])); - $this->assertTrue( - isset($array['service']['namespace']) && $array['service']['namespace'] === 'myserviceNS' - ); - $this->assertTrue(isset($array['service']['location'])); - $this->assertTrue( - isset($array['service']['location']['locale']) && $array['service']['location']['locale'] === 'en' - ); - $this->assertTrue( - isset($array['service']['location']['name']) && $array['service']['location']['name'] === 'myservice' - ); - - $data = []; - - Arr::set($data, 'foo', 'bar'); - Arr::set($data, 'baz', ['doo']); - Arr::set($data, 'baz.some', 'goo'); - Arr::set($data, 'baz.glue', 'fuxk'); - } - - /** @test */ - public function unsetKey() - { - $data = ['foo' => ['bar' => 'baz', 'baz' => 'tab']]; - - Arr::unsetKey($data, 'foo.bar'); - - $this->assertSame(['foo' => ['baz' => 'tab']], $data); - } - - /** @test */ - public function testPluck() - { - $a = new \stdClass(); - $b = new \stdClass(); - - $a->name = 'foo'; - $b->name = 'bar'; - - $this->assertSame(['foo', 'bar'], Arr::pluck([$a, $b], 'name')); - $this->assertSame(['foo', 'bar'], Arr::pluck([['name' => 'foo'], ['name' => 'bar']], 'name')); - } - - /** @test */ - public function testZip() - { - $zipped = Arr::zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); - $this->assertSame( - [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]], - $zipped - ); - - } - - public function arrayGetDataProvider() - { - return [ - [ - 'foo.bar', - ['foo' => ['bar' => 'baz']], - 'baz' - ], - [ - 'foo.bar.baz', - ['foo' => ['bar'=> ['baz' => 'boom']]], - 'boom' - ], - [ - 'foo.bar.baz.boom', - ['foo' => ['bar'=> ['baz' => ['boom' => 'baz']]]], - 'baz' - ], - [ - 'foo.bar.0', - ['foo' => ['bar' => [1, 2, 3]]], - 1 - ], - [ - 'foo.bar.1', - ['foo' => ['bar' => [1, 2, 3]]], - 2 - ] - ]; - } -} diff --git a/lucid/common/tests/Helper/StrTest.php b/lucid/common/tests/Helper/StrTest.php deleted file mode 100644 index 5232fb9..0000000 --- a/lucid/common/tests/Helper/StrTest.php +++ /dev/null @@ -1,92 +0,0 @@ -assertTrue(Str::safeCmp($a, $b)); - $a = 'secret'; - $b = 'somesecretkey'; - $this->assertFalse(Str::safeCmp($a, $b)); - $a = 'secret'; - $b = 'secrut'; - $this->assertFalse(Str::safeCmp($a, $b)); - } - - /** @test */ - public function itShouldTellIfStringEqulasString() - { - - $a = 'string'; - $b = 'string'; - - $this->assertTrue(Str::equals($a, $b)); - - $a = 'string'; - $b = 'otherstring'; - - $this->assertFalse(Str::equals($a, $b)); - } - - /** @test */ - public function itShouldCamelCaseStrings() - { - $this->assertEquals('fooBar', Str::camelCase('foo_bar')); - } - - /** @test */ - public function strcamelCaseAll() - { - $this->assertEquals('FooBar', Str::camelCaseAll('foo_bar')); - } - - /** @test */ - public function itShouldSnakeCaseStrings() - { - $this->assertEquals('foo_bar', Str::lowDash('fooBar')); - $this->assertEquals('foo_bar_baz', Str::lowDash('fooBarBaz')); - } - - /** @test */ - public function itShouldSnakeCaseStringsUsingACustomDelimiter() - { - $this->assertEquals('foo@bar', Str::lowDash('fooBar', '@')); - $this->assertEquals('foo:bar:baz', Str::lowDash('fooBarBaz', ':')); - } - - /** - * @test - * @dataProvider strrandLengthProvider - */ - public function strrand($length) - { - $this->assertSame($length, strlen(Str::rand($length))); - } - - /** - * @test - * @dataProvider strrandLengthProvider - */ - public function strquickRand($length) - { - $this->assertSame($length, strlen(Str::quickRand($length))); - } - - public function strrandLengthProvider() - { - return [ - [12], - [22], - [40], - [25], - [125] - ]; - } -} diff --git a/lucid/common/tests/Struct/ItemsTest.php b/lucid/common/tests/Struct/ItemsTest.php deleted file mode 100644 index 0fd250f..0000000 --- a/lucid/common/tests/Struct/ItemsTest.php +++ /dev/null @@ -1,173 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Tests\Struct; - -use InvalidArgumentException; -use Lucid\Common\Struct\Items; - -/** - * @class ItemsTest - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -class ItemsTest extends \PHPUnit_Framework_TestCase -{ - - /** @test */ - public function testPush() - { - $list = new Items(1, 2); - $list->push(3); - - $this->assertSame([1, 2, 3], $list->toArray()); - } - - /** @test */ - public function itShouldHaveArrayAccess() - { - $list = new Items(1, 2); - $list[] = 3; - - $this->assertSame([1, 2, 3], $list->toArray()); - - unset($list[2]); - - $this->assertSame([1, 2], $list->toArray()); - $this->assertTrue(isset($list[0])); - $this->assertTrue(isset($list[1])); - $this->assertFalse(isset($list[2])); - - $this->assertSame(1, $list[0]); - $this->assertSame(2, $list[1]); - } - - /** @test */ - public function itShouldBeIteratable() - { - $list = new Items(1, 2, 3); - $res = []; - foreach ($list as $key => $value) { - $res[$key] = $value; - } - - $this->assertSame($res, $list->toArray()); - } - - /** @test */ - public function itShouldBeSerializable() - { - $list = new Items(1, 2, 3); - $serialized = serialize($list); - - $this->assertInstanceOf('Lucid\Common\Struct\ListInterface', $unserialized = unserialize($serialized)); - $this->assertSame($list->toArray(), $unserialized->toArray()); - } - - /** @test */ - public function testConstructWithData() - { - $list = new Items(1, 2, 3, 4, 5); - $this->assertEquals(5, count($list)); - $this->assertEquals([1, 2, 3, 4, 5], $list->toArray()); - } - - /** @test */ - public function testPop() - { - $list = new Items(1, 2, 3, 4, 5); - - $this->assertEquals(5, $list->pop()); - $this->assertEquals(2, $list->pop(1)); - $this->assertEquals(4, $list->pop(2)); - } - - /** @test */ - public function popShouldThrowErrorOnInvalidIndex() - { - $list = new Items(1, 2); - - try { - $this->assertEquals(4, $list->remove(3)); - } catch (InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } - $this->fail(); - } - - /** @test */ - public function testInsert() - { - $list = new Items(1, 2, 3, 4, 5); - - $list->insert(3, 'foo'); - $this->assertEquals([1, 2, 3, 'foo', 4, 5], $list->toArray()); - } - - - /** @test */ - public function testCountValue() - { - $list = new Items(1, 'red', 'green', 3, 'blue', 4, 'red', 5); - - $this->assertEquals(2, $list->countValue('red')); - $this->assertEquals(1, $list->countValue('green')); - } - - /** @test */ - public function testSort() - { - $list = new Items(120, -1, 3, 20, -110); - - $list->sort(); - - $this->assertEquals([-110, -1, 3, 20, 120], $list->toArray()); - } - - /** @test */ - public function testRemove() - { - $list = new Items(1, 2, 3, 4, 5); - - $list->remove(3); - - $this->assertEquals([1, 2, 4, 5], $list->toArray()); - - $list = new Items('red', 'green', 'blue'); - - $list->remove('green'); - - $this->assertEquals(['red', 'blue'], $list->toArray()); - } - - /** @test */ - public function testReverse() - { - $list = new Items(1, 2, 3, 4, 5); - $list->reverse(); - - $this->assertEquals([5, 4, 3, 2, 1], $list->toArray()); - } - - /** @test */ - public function testExtend() - { - $listA = new Items(1, 2, 3, 4, 5); - $listB = new Items('red', 'green'); - - $listA->extend($listB); - - $this->assertEquals([1, 2, 3, 4, 5, 'red', 'green'], $listA->toArray()); - } -} diff --git a/lucid/common/tests/Struct/PriorityQueueTest.php b/lucid/common/tests/Struct/PriorityQueueTest.php deleted file mode 100644 index 470038a..0000000 --- a/lucid/common/tests/Struct/PriorityQueueTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Tests\Struct; - -use Lucid\Common\Struct\PriorityQueue; - -/** - * @class PriorityQueueTest - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -class PriorityQueueTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('SplPriorityQueue', new PriorityQueue); - } - - /** @test */ - public function itShouldPriorizeFromHighToLow() - { - $q = new PriorityQueue; - - $q->insert('foo', 10); - $q->insert('bar', 100); - - $this->assertSame('bar', $q->extract()); - $this->assertSame('foo', $q->extract()); - } -} diff --git a/lucid/common/tests/Struct/ReversePriorityQueueTest.php b/lucid/common/tests/Struct/ReversePriorityQueueTest.php deleted file mode 100644 index 05c1b1a..0000000 --- a/lucid/common/tests/Struct/ReversePriorityQueueTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Common\Tests\Struct; - -use Lucid\Common\Struct\ReversePriorityQueue; - -/** - * @class ReversePriorityQueueTest - * - * @package Lucid\Common - * @version $Id$ - * @author iwyg - */ -class ReversePriorityQueueTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('SplPriorityQueue', new ReversePriorityQueue); - } - - /** @test */ - public function itShouldPriorizeFromLowToHigh() - { - $q = new ReversePriorityQueue; - - $q->insert('bar', 100); - $q->insert('foo', 10); - - $this->assertSame('foo', $q->extract()); - $this->assertSame('bar', $q->extract()); - } -} diff --git a/lucid/common/tests/Traits/GetterTest.php b/lucid/common/tests/Traits/GetterTest.php deleted file mode 100644 index 0d9a229..0000000 --- a/lucid/common/tests/Traits/GetterTest.php +++ /dev/null @@ -1,61 +0,0 @@ - 'bar']; - $this->assertSame('bar', $this->getDefault($p, 'foo')); - $this->assertNull($this->getDefault($p, 'bar')); - } - - /** - * @test - */ - public function testGetStrictDefault() - { - $p = ['foo' => null]; - - $this->assertNull($this->getStrictDefault($p, 'foo')); - $this->assertSame('baz', $this->getStrictDefault($p, 'bar', 'baz')); - } - - /** - * @test - */ - public function testGetDefaultUsing() - { - $p = ['foo' => 'bar']; - $this->assertSame('bar', $this->getDefaultUsing($p, 'foo', function () { - return 'bar'; - })); - - $this->assertSame('baz', $this->getDefaultUsing([], 'bar', function () { - return 'baz'; - })); - } - - /** - * @test - */ - public function testGetStrictDefaultUsing() - { - $p = ['foo' => null]; - $this->assertSame('bar', $this->getStrictDefaultUsing($p, 'foo', function () { - return 'bar'; - })); - - //$this->assertSame('baz', $this->getStrictDefaultUsing([], 'bar', function () { - //return 'baz'; - //})); - } -} diff --git a/lucid/hash/src/HashBcrypt.php b/lucid/hash/src/HashBcrypt.php deleted file mode 100644 index 697c964..0000000 --- a/lucid/hash/src/HashBcrypt.php +++ /dev/null @@ -1,57 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash; - -/** - * @class HashBcrypt - * @see HashInterface - * - * @package Lucid\Hash - * @version $Id$ - * @author Thomas Appel 7]; - - /** - * hash - * - * @param mixed $password - * @param array $options - */ - public function hash($password, array $options = null) - { - $options = is_array($options) ? array_merge(static::$default, $options) : static::$default; - - return password_hash($password, PASSWORD_BCRYPT, $options); - } - - /** - * check - * - * @param mixed $password - * @param string $hash - * - * @boolean - */ - public function check($password, $hash, $options = null) - { - return password_verify($password, $hash); - } -} diff --git a/lucid/hash/src/HashInterface.php b/lucid/hash/src/HashInterface.php deleted file mode 100644 index 1e60f3d..0000000 --- a/lucid/hash/src/HashInterface.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash; - -/** - * @interface HashInterface - * - * @package Lucid\Hash - * @version $Id$ - * @author iwyg - */ -interface HashInterface -{ - /** - * Creates a hash from string - * - * @param string $string string to be hased - * @param array $options optional configuration - * - * @return string the hashed input string. - */ - public function hash($string, array $options = null); - - /** - * Check an input value against a hash. - * - * @param string $string value to be compared agains hash - * @param string $hash hash to be compared against string - * - * @boolean - */ - public function check($string, $hash, $options = null); -} diff --git a/lucid/hash/src/HashKey.php b/lucid/hash/src/HashKey.php deleted file mode 100644 index 0fa0cdc..0000000 --- a/lucid/hash/src/HashKey.php +++ /dev/null @@ -1,111 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash; - -use Lucid\Common\Helper\Str; -use Lucid\Hash\Helper\BcConvertHelper; - -/** - * @class HashKey - * @see HashInterface - * - * @package Selene\Module\Cryptography - * @version $Id$ - * @author Thomas Appel key = $key; - } - - /** - * hash - * - * @param mixed $value - * @param array $options - * - * @access public - * @return string - */ - public function hash($value, array $options = null) - { - $options = $this->getOptions($options ?: []); - - return $this->generate($value, $options); - } - - /** - * check - * - * @param mixed $value - * @param mixed $hash - * @param mixed $options - * - * @access public - * @return bool - */ - public function check($input, $hash, $options = null) - { - return Str::safeCmp($hash, $this->hash($input, $options)); - } - - /** - * generate - * - * @param mixed $value - * @param mixed $options - * - * @return string - */ - private function generate($value, $options) - { - $base = hash_hmac('md5', $value, $options['secret']); - - if (!function_exists('gmp_init')) { - return BcConvertHelper::baseConvert($base, 16, 62); - } - - $init = gmp_init($base, 16); - - return gmp_strval($init, 62); - } - - /** - * getOptions - * - * @param array $options - * - * @access private - * @return array - */ - private function getOptions(array $options = []) - { - if (!isset($options['secret'])) { - $options['secret'] = $this->key; - } - - return $options; - } -} diff --git a/lucid/hash/src/Helper/BcConvertHelper.php b/lucid/hash/src/Helper/BcConvertHelper.php deleted file mode 100644 index 3727888..0000000 --- a/lucid/hash/src/Helper/BcConvertHelper.php +++ /dev/null @@ -1,100 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash\Helper; - -/** - * @class BcConvertHelper BcConvertHelper - * - * @package Lucid\Hash - * @version $Id$ - * @author Thomas Appel - */ -final class BcConvertHelper -{ - /** - * character storage for bcBaseConvert fallback - * - * @var string - */ - private static $storage = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - - /** - * register - * - * @var string - */ - private static $register = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; - - /** - * baseConvert - * - * @param mixed $value - * @param mixed $sourceformat - * @param mixed $targetformat - * - * @return string; - */ - public static function baseConvert($value, $sourceformat, $targetformat) - { - if (max($sourceformat, $targetformat) > strlen(static::$storage)) { - trigger_error('Bad Format max: ' . strlen(static::$storage), E_USER_ERROR); - } - - if (min($sourceformat, $targetformat) < 2) { - trigger_error('Bad Format min: 2', E_USER_ERROR); - } - - $dec = '0'; - $level = 0; - $result = ''; - $value = trim((string)$value, "\r\n\t +"); - $prefix = '-' === $value{0} ? '-' : ''; - $value = ltrim($value, "-0"); - $len = strlen($value); - - for ($i = 0; $i < $len; $i++) { - - $val = strpos(static::$storage, $value{$len - 1 - $i}); - - if (false === $val) { - trigger_error('Bad Char in input 1', E_USER_ERROR); - } - - if ($val >= $sourceformat) { - trigger_error('Bad Char in input 2', E_USER_ERROR); - } - - $dec = bcadd($dec, bcmul(bcpow($sourceformat, $i), $val)); - } - - if (10 === $targetformat) { - return $prefix . $dec; - } - - while (1 !== bccomp(bcpow($targetformat, $level++), $dec)) { - } - - for ($i = ($level - 2); $i >= 0; $i--) { - $factor = bcpow($targetformat, $i); - $number = bcdiv($dec, $factor, 0); - $dec = bcmod($dec, $factor); - $result .= static::$register{$number}; - } - - $result = empty($result) ? '0' : $result; - return $prefix . $result; - } - - private function __construct() - { - } -} diff --git a/lucid/hash/tests/HashBcryptTest.php b/lucid/hash/tests/HashBcryptTest.php deleted file mode 100644 index e3a4115..0000000 --- a/lucid/hash/tests/HashBcryptTest.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash\Tests; - -use Lucid\Hash\HashBcrypt; - -/** - * @class HashBcryptTest - * @see HashTestCase - * - * @package Lucid\Hash - * @version $Id$ - * @author iwyg - */ -class HashBcryptTest extends HashTestCase -{ - protected function setUp() - { - $this->hash = new HashBcrypt(); - } -} diff --git a/lucid/hash/tests/HashKeyTest.php b/lucid/hash/tests/HashKeyTest.php deleted file mode 100644 index 39b72e8..0000000 --- a/lucid/hash/tests/HashKeyTest.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Hash\Tests; - -use Lucid\Hash\HashKey; - -/** - * Class HashKeyTest - * @author - */ -class HashKeyTest extends HashTestCase -{ - protected function setUp() - { - $this->hash = new HashKey('9asdz47aazrasda72haHgGadh3hasgdh32OO8'); - } -} diff --git a/lucid/hash/tests/HashTestCase.php b/lucid/hash/tests/HashTestCase.php deleted file mode 100644 index a42f3f9..0000000 --- a/lucid/hash/tests/HashTestCase.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Hash\Tests; - -/** - * @class HashTestCase - * @see \PHPUnit_Framework_TestCase - * @abstract - * - * @package Lucid\Hash - * @version $Id$ - * @author iwyg - */ -abstract class HashTestCase extends \PHPUnit_Framework_TestCase -{ - protected $hash; - - public function testHashCreateAndValidate() - { - $hash = $this->hash->hash('bragging'); - $this->assertTrue($this->hash->check('bragging', $hash)); - $this->assertFalse($this->hash->check('brAgging', $hash)); - } -} diff --git a/lucid/resource/.coveralls.yml b/lucid/resource/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/resource/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/resource/.travis.yml b/lucid/resource/.travis.yml deleted file mode 100644 index 3d97e8c..0000000 --- a/lucid/resource/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/resource/LICENSE.md b/lucid/resource/LICENSE.md deleted file mode 100644 index 182ad4e..0000000 --- a/lucid/resource/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright (c) 2014-2016 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/resource/README.md b/lucid/resource/README.md deleted file mode 100644 index 10c3b01..0000000 --- a/lucid/resource/README.md +++ /dev/null @@ -1,77 +0,0 @@ -# Resource locator - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/resource-blue.svg?style=flat-square)](https://github.com/lucidphp/resource/tree/develop) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/resource/blob/develop/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/resource/develop.svg?style=flat-square)](https://travis-ci.org/lucidphp/resource) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/resource/develop.svg?style=flat-square)](https://coveralls.io/r/lucidphp/resource) -[![HHVM](https://img.shields.io/hhvm/lucid/resource/dev-develop.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/resource) - -## Requirements - -``` -php >= 5.6 -``` - -## Installation - -You may install `lucid/resource` with composer. - -```bash -$ composer require lucid/resource:dev-develop -``` - -## Locating resources -```php -use Lucid\Resource\Locator; - -$locator = new Loacor(['/path/to/dirA', '/path/to/dirB']); - -foreach ($locator->locate('config.php') as $resource) { - $resource->... // do stuff -} - -``` -## Resources -### File Resources - -```php -use Lucid\Resource\FileResource; - -$res = new FileResource($file); -$res->getResource(); // returns "/path/file" - -$res->isValid($time) // if not mofified since $time -``` - -### Object Resources - -```php -use Lucid\Resource\ObjectResource; -use Acme\MyObject; - -$res = new ObjectResource(new MyObject); -$res->getResource(); // returns "/path/to/Acme/MyObject.php - -$res->isValid($time) // if not mofified since $time -``` - -### Resource Collections -```php -use Lucid\Resource\Collection; -use Lucid\Resource\FileResource; -use Lucid\Resource\ObjectResource; - -$resources = new Collection(); - -$resources->addResource(new ObjectResource($onject)); -$resources->addResource(new FileResource($file)); - -$resources->addObjectResource($object); -$resources->addFileResource($file); - -$resources->all(); // [ObjectResource $resource, FileResource $resource, ... ] - -$resources->isValid(time()); // bool -``` diff --git a/lucid/resource/composer.json b/lucid/resource/composer.json deleted file mode 100644 index df909cb..0000000 --- a/lucid/resource/composer.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "lucid/resource", - "license": "MIT", - "description": "Resource locator", - "keywords": ["loading", "locator"], - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require-dev": { - "php":">=5.6" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev" - }, - "autoload": { - "psr-4": { - "Lucid\\Resource\\":"src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Resource\\Tests\\":"tests/" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/resource/phpunit.xml.dist b/lucid/resource/phpunit.xml.dist deleted file mode 100644 index e85812b..0000000 --- a/lucid/resource/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/resource/src/AbstractResource.php b/lucid/resource/src/AbstractResource.php deleted file mode 100644 index 47077fd..0000000 --- a/lucid/resource/src/AbstractResource.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -/** - * @class AbstractResource - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -abstract class AbstractResource implements ResourceInterface -{ - /** @var string */ - protected $resource; - - /** - * {@inheritdoc} - */ - public function getResource() - { - return $this->resource; - } - - /** - * {@inheritdoc} - */ - public function serialize() - { - return serialize([ - 'resource' => $this->getResource() - ]); - } - - /** - * {@inheritdoc} - */ - public function __toString() - { - return $this->getResource(); - } - - /** - * {@inheritdoc} - */ - public function unserialize($data) - { - $data = unserialize($data); - - $this->resource = $data['resource']; - } - - /** - * {@inheritdoc} - */ - public function isLocal() - { - return is_file($this->getResource()) && stream_is_local($this->getResource()); - } - - /** - * {@inheritdoc} - */ - public function isValid($now) - { - try { - return filemtime($this) <= $now; - } catch (\Exception $e) { - } - - return false; - } -} diff --git a/lucid/resource/src/Cache/ResourceCacheInterface.php b/lucid/resource/src/Cache/ResourceCacheInterface.php deleted file mode 100644 index f28baa5..0000000 --- a/lucid/resource/src/Cache/ResourceCacheInterface.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Cache; - -use Lucid\Resource\ResourceInterface; - -/** - * @class ResourceCacheInterface - * - * @package Lucid\Resource\Cache - * @version $Id$ - * @author iwyg - */ -interface ResourceCacheInterface -{ - /** - * setResource - * - * @param ResourceInterface $resource - * - * @return void - */ - public function setResource(ResourceInterface $resource); - - /** - * getResource - * - * @return ResourceInterface - */ - public function getResource(); - - /** - * isValid - * - * @return bool - */ - public function isValid(); -} diff --git a/lucid/resource/src/Collection.php b/lucid/resource/src/Collection.php deleted file mode 100644 index 49e6944..0000000 --- a/lucid/resource/src/Collection.php +++ /dev/null @@ -1,143 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -use IteratorAggregate; - -/** - * @class Collection - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class Collection implements CollectionInterface -{ - /** @var int */ - private $current = 0; - - /** @var ResourceInterface[] */ - private $resources; - - /** - * Constructor. - * - * @param array $resources - */ - public function __construct(array $resources = []) - { - $this->setResources($resources); - } - - /** - * setResources - * - * @param array $resources - * - * @return void - */ - public function setResources(array $resources) - { - $this->resources = []; - - foreach ($resources as $resource) { - $this->addResource($resource); - } - } - - /** - * {@inheritdoc} - */ - public function addResource(ResourceInterface $resource) - { - $this->resources[] = $resource; - } - - - /** - * {@inheritdoc} - */ - public function addFileResource($file) - { - $this->addResource(new FileResource($file)); - } - - /** - * {@inheritdoc} - */ - public function addObjectResource($object) - { - $this->addResource(new ObjectResource($object)); - } - - /** - * {@inheritdoc} - */ - public function all() - { - return $this->resources; - } - - /** - * {@inheritdoc} - */ - public function isValid($timestamp) - { - foreach ($this->resources as $resource) { - if (!$resource->isValid($timestamp)) { - return false; - } - } - - return true; - } - - /** - * {@inheritdoc} - */ - public function current() - { - return $this->resources[$this->current]; - } - - /** - * {@inheritdoc} - */ - public function key() - { - return $this->current; - } - - /** - * {@inheritdoc} - */ - public function next() - { - $this->current++; - } - - /** - * {@inheritdoc} - */ - public function rewind() - { - $this->current = 0; - } - - /** - * {@inheritdoc} - */ - public function valid() - { - return $this->current < count($this->resources); - } -} diff --git a/lucid/resource/src/CollectionInterface.php b/lucid/resource/src/CollectionInterface.php deleted file mode 100644 index c9e6f79..0000000 --- a/lucid/resource/src/CollectionInterface.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -use Iterator; - -/** - * @interface CollectionInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface CollectionInterface extends Iterator -{ - /** - * Adds a resource. - * - * @param ResourceInterface $resource - * - * @return void - */ - public function addResource(ResourceInterface $resource); - - /** - * Adds a file resource. - * - * @param string $file - * - * @return void - */ - public function addFileResource($file); - - /** - * Adds a object resource. - * - * @param object $object - * - * @return void - */ - public function addObjectResource($object); - - /** - * Gets all resources as array. - * - * @return ResourceInterface[] - */ - public function all(); - - /** - * Checks if all resources are still valid. - * - * @param int $timestamp - * - * @return bool - */ - public function isValid($timestamp); -} diff --git a/lucid/resource/src/Exception/LoaderException.php b/lucid/resource/src/Exception/LoaderException.php deleted file mode 100644 index 36fa139..0000000 --- a/lucid/resource/src/Exception/LoaderException.php +++ /dev/null @@ -1,36 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Exception; - -/** - * @class LoaderException - * - * @package Lucid\Resource\Exception - * @version $Id$ - * @author iwyg - */ -class LoaderException extends \Exception -{ - /** - * Returns a LoaderException instance with predefined error message. - * - * @param mixed $resource - * - * @return LoaderException - */ - public static function missingLoader($resource) - { - return new self( - sprintf('No loader found for resource "%s".', is_string($resource) ? $resource : gettype($resource)) - ); - } -} diff --git a/lucid/resource/src/FileResource.php b/lucid/resource/src/FileResource.php deleted file mode 100644 index 8acf17f..0000000 --- a/lucid/resource/src/FileResource.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -/** - * @class FileResource - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class FileResource extends AbstractResource -{ - /** - * Constructor. - * - * @param mixed $file - * - * @return void - */ - public function __construct($file) - { - $this->resource = $file; - } -} diff --git a/lucid/resource/src/Loader/AbstractFileLoader.php b/lucid/resource/src/Loader/AbstractFileLoader.php deleted file mode 100644 index 88f1c02..0000000 --- a/lucid/resource/src/Loader/AbstractFileLoader.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -use Lucid\Resource\LocatorInterface; -use Lucid\Resource\Loader\AbstractLoader; - -/** - * @class AbstractFileLoader - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -abstract class AbstractFileLoader extends AbstractLoader -{ - /** @var LocatorInterface */ - private $locator; - - /** - * Constructor. - * - * @param LocatorInterface $locator - */ - public function __construct(LocatorInterface $locator) - { - $this->locator = $locator; - } - - /** - * {@inhertidoc} - */ - final public function supports($resource) - { - return is_string($resource) - && in_array(pathinfo(strtolower($resource), PATHINFO_EXTENSION), $this->getExtensions()); - } - - /** - * findResourceOrigin - * - * @access protected - * @return CollectionInterface - */ - final protected function findResource($resource, $any = self::LOAD_ONE) - { - return $this->locator->locate($resource, $any); - } - - /** - * Returns the supported extensions - * - * @return array - */ - abstract protected function getExtensions(); -} diff --git a/lucid/resource/src/Loader/AbstractLoader.php b/lucid/resource/src/Loader/AbstractLoader.php deleted file mode 100644 index cd765f4..0000000 --- a/lucid/resource/src/Loader/AbstractLoader.php +++ /dev/null @@ -1,157 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -use SplObjectStorage; -use Lucid\Resource\ResourceInterface; -use Lucid\Resource\Exception\LoaderException; - -/** - * @class AbstractLoader - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -abstract class AbstractLoader implements LoaderInterface -{ - /** @var SplObjectStorage */ - private $listeners; - - /** @var ResourceInterface */ - private $resolver; - - /** - * {@inheritdoc} - */ - public function load($resource, $any = self::LOAD_ONE) - { - foreach ($this->findResource($resource, $any) as $ret) { - $this->loadResource($ret); - } - } - - /** - * {@inheritdoc} - * @throws LoaderException - */ - public function import($resource) - { - if ($this->supports($resource)) { - return $this->load($resource); - } - - if (null === $res = $this->getResolver()) { - throw LoaderException::missingLoader($resource); - } - - try { - $loader = $res->resolve($resource); - } catch (LoaderException $e) { - throw new LoaderException($e->getMessage(), $e->getCode(), $e); - } - - $loader->load($resource); - } - - /** - * {@inheritdoc} - */ - public function addListener(ListenerInterface $listener) - { - $this->getListeners()->attach($listener); - } - - /** - * {@inheritdoc} - */ - public function removeListener(ListenerInterface $listener) - { - $this->getListeners()->detach($listener); - } - - /** - * {@inheritdoc} - */ - public function setResolver(ResolverInterface $resolver) - { - $this->resolver = $resolver; - } - - /** - * {@inheritdoc} - */ - public function getResolver() - { - return $this->resolver; - } - - /** - * Calls 'onLoaded' on all listeners. - * - * @return void - */ - protected function notify($resource) - { - foreach ($this->getListeners() as $listener) { - $listener->onLoaded($resource); - } - } - - /** - * findResource - * - * @param mixed $resource - * @param mixed $any - * - * @return \Traversable - */ - abstract protected function findResource($resource, $any = self::LOAD_ONE); - - /** - * doLoad - * - * @param mixed $resource - * - * @return void - */ - abstract protected function doLoad($resource); - - /** - * Loads a resource. - * - * @param mixed $resource - * - * @return void - */ - protected function loadResource(ResourceInterface $resource) - { - $res = $this->doLoad($resource); - $this->notify($resource); - - return $res; - } - - /** - * getObservers - * - * @return \SplObjectStorage - */ - private function getListeners() - { - if (null === $this->listeners) { - $this->listeners = new SplObjectStorage; - } - - return $this->listeners; - } -} diff --git a/lucid/resource/src/Loader/ChainedLoader.php b/lucid/resource/src/Loader/ChainedLoader.php deleted file mode 100644 index cd4b977..0000000 --- a/lucid/resource/src/Loader/ChainedLoader.php +++ /dev/null @@ -1,139 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -use Lucid\Resource\Exception\LoaderException; - -/** - * @class ChainedLoader - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class ChainedLoader implements LoaderInterface -{ - /** - * loaders - * - * @var ResolverInterface - */ - private $resolver; - - /** - * Constructor. - * - * @param array $loaders - */ - public function __construct(ResolverInterface $resolver) - { - $this->setResolver($resolver); - } - - /** - * Resolve a loader. - * - * @param mixed $resource - * - * @return LoaderInterface - */ - public function resolve($resource) - { - foreach ($this->loaders() as $loader) { - if ($loader->supports($resource)) { - return $loader; - } - } - - throw new LoaderException('No matching loader found.'); - } - - /** - * {@inheritdoc} - */ - public function load($resource) - { - return $this->resolve($resource)->load($resource); - } - - /** - * {@inheritdoc} - */ - public function import($resource) - { - return $this->load($resource); - } - - /** - * {@inheritdoc} - */ - public function supports($resource) - { - foreach ($this->loaders() as $loader) { - if ($loader->supports($resource)) { - return true; - } - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function addListener(ListenerInterface $listener) - { - foreach ($this->loaders() as $loader) { - $loader->addListener($listener); - } - } - - /** - * {@inheritdoc} - */ - public function removeListener(ListenerInterface $listener) - { - foreach ($this->loaders() as $loader) { - $loader->removeListener($listener); - } - } - - /** - * {@inheritdoc} - */ - public function setResolver(ResolverInterface $resolver) - { - $this->resolver = $resolver; - - foreach ($this->loaders() as $loader) { - $loader->setResolver($resolver); - } - } - - /** - * {@inheritdoc} - */ - public function getResolver() - { - return $this->resolver; - } - - /** - * loaders - * - * @return array `LoaderInterface[]` - */ - private function loaders() - { - return $this->resolver->all(); - } -} diff --git a/lucid/resource/src/Loader/ListenerInterface.php b/lucid/resource/src/Loader/ListenerInterface.php deleted file mode 100644 index 7c6406a..0000000 --- a/lucid/resource/src/Loader/ListenerInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -use Lucid\Resource\ResourceInterface; - -/** - * @interface ListenerInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface ListenerInterface -{ - /** - * Method to be called if a resources got loaded. - * - * @return void - */ - public function onLoaded($resource); -} diff --git a/lucid/resource/src/Loader/LoaderInterface.php b/lucid/resource/src/Loader/LoaderInterface.php deleted file mode 100644 index 9a086ec..0000000 --- a/lucid/resource/src/Loader/LoaderInterface.php +++ /dev/null @@ -1,77 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -/** - * @class LoaderInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface LoaderInterface -{ - /** @var bool */ - const LOAD_ALL = true; - - /** @var bool */ - const LOAD_ONE = false; - - /** - * load - * - * @param mixed $resource - * - * @return mixed - */ - public function load($resource); - - /** - * import - * - * @param mixed $resource - * - * @return void - */ - public function import($resource); - - /** - * supports - * - * @param mixed $resource - * - * @return boolean - */ - public function supports($resource); - - /** - * addListener - * - * @param ListenerInterface $listener - * - * @return void - */ - public function addListener(ListenerInterface $listener); - - /** - * removeListener - * - * @param ListenerInterface $listener - * - * @return void - */ - public function removeListener(ListenerInterface $listener); - - public function setResolver(ResolverInterface $resolver); - - public function getResolver(); -} diff --git a/lucid/resource/src/Loader/Resolver.php b/lucid/resource/src/Loader/Resolver.php deleted file mode 100644 index d642d41..0000000 --- a/lucid/resource/src/Loader/Resolver.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -use Lucid\Resource\Exception\LoaderException; - -/** - * @class Resolver - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class Resolver implements ResolverInterface -{ - /** @var array `LoaderInterface[]` */ - private $loaders; - - /** - * Constructor. - * - * @param array $loaders `LoaderInterface[]` - */ - public function __construct(array $loaders = []) - { - $this->loaders = []; - - foreach ($loaders as $loader) { - $this->addLoader($loader); - } - } - - /** - * {@inheritdoc} - */ - public function addLoader(LoaderInterface $loader) - { - $loader->setResolver($this); - $this->loaders[] = $loader; - } - - /** - * {@inheritdoc} - */ - public function resolve($resource) - { - foreach ($this->loaders as $loader) { - if ($loader->supports($resource)) { - return $loader; - } - } - - throw LoaderException::missingLoader($resource); - } - - /** - * {@inheritdoc} - */ - public function all() - { - return $this->loaders; - } -} diff --git a/lucid/resource/src/Loader/ResolverInterface.php b/lucid/resource/src/Loader/ResolverInterface.php deleted file mode 100644 index f7aa1ee..0000000 --- a/lucid/resource/src/Loader/ResolverInterface.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Loader; - -/** - * @interface ResolverInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface ResolverInterface -{ - /** - * Adds a loader. - * - * @param LoaderInterface $loader - * - * @return void - */ - public function addLoader(LoaderInterface $loader); - - /** - * Resolves a loader for a given resource - * - * @param mixed $resource - * - * @return \Lucid\Resource\LoaderInterface - */ - public function resolve($resource); - - /** - * Returns an array of loaders. - * - * @return array `LoaderInterface[]` - */ - public function all(); -} diff --git a/lucid/resource/src/Locator.php b/lucid/resource/src/Locator.php deleted file mode 100644 index a3b08dd..0000000 --- a/lucid/resource/src/Locator.php +++ /dev/null @@ -1,151 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -use InvalidArgumentException; -use Lucid\Resource\Loader\LoaderInterface; - -/** - * @class Locator - * @see LocatorInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class Locator implements LocatorInterface -{ - /** @var array */ - private $paths; - - /** @var string */ - private $cwd; - - /** - * Constructor. - * - * @param array $paths - * @param string $cwd - */ - public function __construct(array $paths = [], $cwd = null) - { - $this->setPaths($paths); - $this->setRootPath($cwd ?: getcwd()); - } - - /** - * {@inheritdoc} - */ - public function locate($file, $collection = LoaderInterface::LOAD_ONE) - { - if (!is_dir($this->cwd)) { - throw new InvalidArgumentException(sprintf('%s is not a directory.', $this->cwd)); - } - - $resources = []; - - foreach ($this->paths as $path) { - if (1 === $this->locateResource($path, $file, $collection, $resources)) { - break; - } - } - - return new Collection($resources); - } - - /** - * {@inheritdoc} - */ - public function setRootPath($root) - { - $this->cwd = $root; - } - - /** - * {@inheritdoc} - */ - public function addPath($path) - { - if (in_array($path, $this->paths)) { - return; - } - - $this->paths[] = $path; - } - - /** - * {@inheritdoc} - */ - public function addPaths(array $paths) - { - foreach ($paths as $path) { - $this->addPath($path); - } - } - - /** - * {@inheritdoc} - */ - public function setPaths(array $paths) - { - $this->paths = []; - - foreach ($paths as $path) { - $this->addPath($path); - } - } - - /** - * locateResource - * - * @param string $path - * @param string $file - * @param bool $collect - * @param array $collection - * - * @return int - */ - protected function locateResource($path, $file, $collect = LoaderInterface::LOAD_ONE, array &$collection = []) - { - if (false === ($dir = $this->expandPath($path)) || !is_dir($dir)) { - return; - } - - if (!file_exists($resource = $dir . DIRECTORY_SEPARATOR . $file)) { - return; - } - - $collection[] = new FileResource($resource); - - if (LoaderInterface::LOAD_ONE === $collect) { - return 1; - } - - return 0; - } - - /** - * Expands a path to full path. - * - * @param string $path - * - * @return string or false if path is invalid. - */ - private function expandPath($path) - { - if (0 === strspn($path, '\\/', 0, 1) || null === parse_url($path, PHP_URL_PATH)) { - $path = $this->cwd.DIRECTORY_SEPARATOR.$path; - } - - return realpath($path); - } -} diff --git a/lucid/resource/src/LocatorInterface.php b/lucid/resource/src/LocatorInterface.php deleted file mode 100644 index ced99e4..0000000 --- a/lucid/resource/src/LocatorInterface.php +++ /dev/null @@ -1,69 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -/** - * @interface LocatorInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface LocatorInterface -{ - /** - * Locate a resource file - * - * @param string $name typically the filename - * @param bool $collect weather to collection all files within the given - * paths or return the first match. - * - * @return string|array - */ - public function locate($name, $collect = false); - - /** - * Adds a resource path. - * - * @param string $path a resource path. - * - * @return void - */ - public function addPath($path); - - /** - * Adds resource paths. - * - * @param array $paths a collection of resource paths. - * - * @return void - */ - public function addPaths(array $paths); - - /** - * Sets resource paths. - * - * @param array $paths a collection of resource paths. - * - * @return void - */ - public function setPaths(array $paths); - - /** - * Set the location root path. - * - * @param string $root - * - * @return void - */ - public function setRootPath($root); -} diff --git a/lucid/resource/src/ObjectResource.php b/lucid/resource/src/ObjectResource.php deleted file mode 100644 index 54c0c6c..0000000 --- a/lucid/resource/src/ObjectResource.php +++ /dev/null @@ -1,62 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -use LogicException; -use ReflectionObject; -use InvalidArgumentException; - -/** - * @class ObjectResource - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class ObjectResource extends AbstractResource -{ - /** - * Constructor. - * - * @param object $object - */ - public function __construct($object) - { - $this->resource = $this->getObjectResource($object); - } - - /** - * getObjectResource - * - * @param mixed $object - * - * @return string - */ - private function getObjectResource($object) - { - if (!is_object($object)) { - throw new InvalidArgumentException( - sprintf('%s expects first argument to be object, instead saw %s', get_class($this), gettype($object)) - ); - } - - $reflection = new ReflectionObject($object); - - if ($reflection->isInternal()) { - throw new LogicException( - sprintf('Cannot use internal class "%s" as resource.', $reflection->getName()) - ); - } - - return $reflection->getFileName(); - } -} diff --git a/lucid/resource/src/ResourceInterface.php b/lucid/resource/src/ResourceInterface.php deleted file mode 100644 index ec66c5c..0000000 --- a/lucid/resource/src/ResourceInterface.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource; - -use Serializable; - -/** - * @interface ResourceInterface - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -interface ResourceInterface extends Serializable -{ - /** - * Gets the resource path. - * - * @return string - */ - public function getResource(); - - /** - * Check if the resource is local. - * - * @return bool - */ - public function isLocal(); - - /** - * Checks if the resource is still valid. - * - * @param int $time timestamp to test against. - * - * @return bool - */ - public function isValid($time); - - /** - * Returns the resource path as string. - * - * @return string - */ - public function __toString(); -} diff --git a/lucid/resource/tests/CollectionTest.php b/lucid/resource/tests/CollectionTest.php deleted file mode 100644 index e9b7b7d..0000000 --- a/lucid/resource/tests/CollectionTest.php +++ /dev/null @@ -1,137 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests; - -use Lucid\Resource\Collection; - -/** - * @class CollectionTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class CollectionTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\CollectionInterface', new Collection); - $this->assertInstanceOf('Traversable', new Collection); - } - - /** @test */ - public function itShouldReturnCollectionAsArray() - { - $resources = [ - $r1 = $this->mockResource(), - $r2 = $this->mockResource(), - $r3 = $this->mockResource(), - ]; - - $collection = new Collection($resources); - - $this->assertSame($resources, $collection->all()); - } - - /** @test */ - public function itShouldBeTraversable() - { - $resources = [ - $r1 = $this->mockResource(), - $r2 = $this->mockResource(), - $r3 = $this->mockResource(), - ]; - - $c = new Collection($resources); - - $ret = []; - foreach ($c as $i => $res) { - $this->assertSame($i, $c->key()); - $ret[] = $res; - } - - $this->assertSame($resources, $ret); - } - - /** @test */ - public function itShouldTestValidity() - { - $time = time(); - - $resources = [ - $r1 = $this->mockResource(), - $r2 = $this->mockResource() - ]; - - $r2->method('isValid')->with($time)->willReturn(false); - $r1->method('isValid')->with($time)->willReturn(true); - - $collection = new Collection($resources); - - $this->assertFalse($collection->isValid($time)); - - $resources = [ - $r1 = $this->mockResource(), - $r2 = $this->mockResource() - ]; - - $r2->method('isValid')->with($time)->willReturn(true); - $r1->method('isValid')->with($time)->willReturn(true); - - $collection = new Collection($resources); - $this->assertTrue($collection->isValid($time)); - } - - /** @test */ - public function itSouldBeAbleToAddResources() - { - $res = $this->mockResource(); - - $collection = new Collection; - $collection->addResource($res); - - $this->assertSame([$res], $collection->all()); - } - - /** @test */ - public function itShouldBeAbleToAddFilePaths() - { - $collection = new Collection; - - $collection->addFileResource(__FILE__); - - $res = $collection->all(); - - $this->assertArrayHasKey(0, $res); - $this->assertInstanceOf('Lucid\Resource\FileResource', $res[0]); - } - - /** @test */ - public function itShouldBeAbleToAddObjectResources() - { - $collection = new Collection; - $collection->addObjectResource($this); - - $res = $collection->all(); - - $this->assertArrayHasKey(0, $res); - $this->assertInstanceOf('Lucid\Resource\ObjectResource', $res[0]); - } - - private function mockResource() - { - return $this->getMockbuilder('Lucid\Resource\ResourceInterface') - ->disableOriginalConstructor()-> - getMock(); - } -} diff --git a/lucid/resource/tests/FileResourceTest.php b/lucid/resource/tests/FileResourceTest.php deleted file mode 100644 index f482cdc..0000000 --- a/lucid/resource/tests/FileResourceTest.php +++ /dev/null @@ -1,90 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests; - -use Lucid\Resource\FileResource; - -/** - * @class FileResourceTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class FileResourceTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Resource\ResourceInterface', new FileResource('')); - } - - /** @test */ - public function itShouldReturnResourcePathAsString() - { - $resource = new FileResource(__FILE__); - - $this->assertSame(__FILE__, $resource->getResource()); - } - - /** @test */ - public function itShouldBeLocalAResource() - { - $resource = new FileResource(__FILE__); - - $this->assertTrue($resource->isLocal()); - } - - /** @test */ - public function itShouldNotBeLocalAResource() - { - $resource = new FileResource('https://example.com/files/resource.txt'); - - $this->assertFalse($resource->isLocal()); - } - - /** @test */ - public function itShouldBeAValidResource() - { - $resource = new FileResource(__FILE__); - - $this->assertTrue($resource->isValid(time())); - $this->assertFalse($resource->isValid(0)); - } - - /** @test */ - public function itShouldNotBeValidIfResourceIsMissing() - { - $resource = new FileResource(__FILE__.'.inc'); - $this->assertFalse($resource->isValid(time())); - } - - /** @test */ - public function itShouldNotBeAValidResource() - { - $resource = new FileResource(__FILE__); - $time = filemtime(__FILE__) - 1; - - $this->assertFalse($resource->isValid($time)); - } - - /** @test */ - public function itShouldBeSerializable() - { - $resource = new FileResource(__FILE__); - - $data = serialize($resource); - $ret = unserialize($data); - - $this->assertSame(get_class($resource), get_class($ret)); - } -} diff --git a/lucid/resource/tests/Fixures/loc_a/file.txt b/lucid/resource/tests/Fixures/loc_a/file.txt deleted file mode 100644 index e69de29..0000000 diff --git a/lucid/resource/tests/Fixures/loc_b/file.txt b/lucid/resource/tests/Fixures/loc_b/file.txt deleted file mode 100644 index e69de29..0000000 diff --git a/lucid/resource/tests/Loader/AbstractFileLoaderTest.php b/lucid/resource/tests/Loader/AbstractFileLoaderTest.php deleted file mode 100644 index bb18abf..0000000 --- a/lucid/resource/tests/Loader/AbstractFileLoaderTest.php +++ /dev/null @@ -1,163 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests\Loader; - -use Lucid\Resource\Tests\Stubs\PhpFileLoader; -use Lucid\Resource\Loader\AbstractFileLoader; -use Lucid\Resource\Exception\LoaderException; - -/** - * @class AbstractFileLoaderTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class AbstractFileLoaderTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\Loader\LoaderInterface', $this->newLoader()); - } - - /** @test */ - public function itShouldSupportGivenFiles() - { - $loader = $this->newLoader(); - $loader->method('getExtensions')->willReturn(['php']); - - $this->assertTrue($loader->supports(__FILE__)); - } - - /** @test */ - public function itShouldAddAndGetResolver() - { - $loader = $this->newLoader(); - $res = $this->mockResolver(); - $loader->setResolver($res); - - $this->assertTrue($res === $loader->getResolver()); - } - - /** @test */ - public function itShouldCallResolverOnImport() - { - $loader = $this->newLoader(null, ['getResolver']); - $loader->method('getExtensions')->willReturn([]); - $loader->expects($this->once())->method('getResolver')->willReturn($res = $this->mockResolver()); - $res->expects($this->once())->method('resolve')->with('resource')->willReturn($ld = $this->mockLoader()); - $ld->expects($this->once())->method('load')->with('resource'); - - $loader->import('resource'); - } - - /** @test */ - public function itShouldThrowOnImportIfLoaderIsNotResolved() - { - $loader = $this->newLoader(null, ['getResolver']); - $loader->method('getExtensions')->willReturn([]); - $loader->expects($this->once())->method('getResolver')->willReturn($res = $this->mockResolver()); - $exc = null; - $res->expects($this->once())->method('resolve')->with('resource')->willReturnCallback(function () use (&$exc) { - $exc = new LoaderException; - throw $exc; - }); - - try { - $loader->import('resource'); - } catch (LoaderException $e) { - $this->assertSame($exc, $e->getPrevious()); - } - } - - /** @test */ - public function itShouldThrowLoaderExceptionIfResolverIsMissing() - { - $loader = $this->newLoader(null, ['getResolver']); - $loader->method('getExtensions')->willReturn([]); - $loader->expects($this->once())->method('getResolver')->willReturn(null); - - try { - $loader->import('resource'); - } catch (LoaderException $e) { - $this->assertSame('No loader found for resource "resource".', $e->getMessage()); - } catch (\Exception $e) { - $this->fail(); - } - } - - /** @test */ - public function itShouldCallLoadOnImport() - { - $loader = $this->newLoader(); - $loader->method('getExtensions')->willReturn(['txt']); - $loader->expects($this->once())->method('load'); - - $loader->import('resource.txt'); - } - - /** @test */ - public function itIsExpectedThat() - { - $loader = new PhpFileLoader($locator = $this->mockLocator()); - - $called = false; - $locator->method('locate')->with(basename(__FILE__))->willReturnCallBack(function () use (&$called) { - $called = true; - $c = $this->mockCollection(); - $c->method('all')->willReturn([]); - - return $c; - }); - - $loader->load(basename(__FILE__)); - - $this->assertTrue($called); - } - - protected function mockCollection() - { - return $this->getMockbuilder('Lucid\Resource\CollectionInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function mockLocator() - { - return $this->getMockbuilder('Lucid\Resource\LocatorInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function mockResolver() - { - return $this->getMockbuilder('Lucid\Resource\Loader\ResolverInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function mockLoader() - { - return $this->getMockbuilder('Lucid\Resource\Loader\LoaderInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - protected function newLoader($locator = null, array $methods = []) - { - return $this->getMockBuilder(AbstractFileLoader::class) - ->setMethods(array_merge(['getExtensions', 'load', 'supports', 'doLoad'], $methods)) - ->setConstructorArgs([$locator ?: $this->mockLocator()]) - ->getMock(); - } -} diff --git a/lucid/resource/tests/Loader/AbstractLoaderTest.php b/lucid/resource/tests/Loader/AbstractLoaderTest.php deleted file mode 100644 index 40718dc..0000000 --- a/lucid/resource/tests/Loader/AbstractLoaderTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests\Loader; - -use Lucid\Resource\Loader\AbstractLoader; - -/** - * @class AbstractLoaderTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class AbstractLoaderTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\Loader\LoaderInterface', $this->getLoader()); - } - - /** @test */ - public function itShouldAddAndRemoveListeners() - { - $loader = $this->getLoader(); - $lst = $this->mockListener(); - - $loader->addListener($lst); - $loader->expects($this->exactly(2))->method('findResource')->willReturn([$res = $this->mockResource()]); - $loader->expects($this->exactly(2))->method('doLoad'); - - $lst->expects($this->once())->method('onLoaded')->with($res); - - $loader->load('resource'); - $loader->removeListener($lst); - $loader->load('resource'); - } - - private function mockListener() - { - return $this->getMockbuilder('Lucid\Resource\Loader\ListenerInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - private function mockResource() - { - return $this->getMockbuilder('Lucid\Resource\ResourceInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - private function getLoader(array $methods = []) - { - $methods = array_merge(['supports', 'doLoad', 'findResource'], $methods); - return $this->getMockbuilder(AbstractLoader::class) - ->setMethods($methods) - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/resource/tests/Loader/ChainedLoaderTest.php b/lucid/resource/tests/Loader/ChainedLoaderTest.php deleted file mode 100644 index 61d57e9..0000000 --- a/lucid/resource/tests/Loader/ChainedLoaderTest.php +++ /dev/null @@ -1,164 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests\Loader; - -use Lucid\Resource\Loader\ChainedLoader; - -/** - * @class ChainedLoaderTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class ChainedLoaderTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\Loader\LoaderInterface', new ChainedLoader($this->mockResolver())); - } - - /** @test */ - public function itShouldGetResolver() - { - $loader = new ChainedLoader($res = $this->mockResolver()); - $this->assertTrue($res === $loader->getResolver()); - } - - /** @test */ - public function itShouldOnlyResolveLoadersFromResolver() - { - $loader = new ChainedLoader($this->mockResolver()); - $ld = $this->mockLoader(); - $ld->expects($this->once())->method('supports'); - - $loader->setResolver($res = $this->mockResolver([$ld])); - $this->assertTrue($res === $loader->getResolver()); - - $loader->supports('resource'); - } - - /** @test */ - public function itShouldNotSupportResource() - { - $loader = new ChainedLoader($this->mockResolver()); - - $this->assertFalse($loader->supports('resource')); - } - - /** @test */ - public function itShouldResolveLoaderAndCallSupportOnLoaders() - { - $loaderA = $this->mockLoader(); - $loaderB = $this->mockLoader(); - - $type = 'php'; - - $loaderA->expects($this->any())->method('supports')->with($type)->willReturn(false); - $loaderB->expects($this->any())->method('supports')->with($type)->willReturn(true); - - $loader = new ChainedLoader($this->mockResolver([$loaderA, $loaderB])); - - $this->assertTrue($loader->supports($type)); - - $this->assertTrue($loaderB == $loader->resolve($type)); - } - - /** @test */ - public function itShouldReturnFirstSupportedLoader() - { - $loaderA = $this->mockLoader(); - $loaderB = $this->mockLoader(); - - $type = 'php'; - - $loaderA->expects($this->once())->method('supports')->with($type)->willReturn(true); - $loaderB->method('supports')->with($type)->willReturnCallback(function () { - $this->fail('Loader should not be called.'); - return false; - }); - - $loader = new ChainedLoader($this->mockResolver([$loaderA, $loaderB])); - - $this->assertTrue($loader->supports($type)); - } - - /** @test */ - public function itShouldCallLoadOnLoaderWhenImportingResources() - { - - $loaderA = $this->mockLoader(); - $loaderB = $this->mockLoader(); - - $type = 'file'; - - $loaderA->expects($this->any())->method('supports')->with($type)->willReturn(true); - $loaderB->expects($this->any())->method('supports')->with($type)->willReturn(false); - - $loaderA->expects($this->exactly(2))->method('load')->with($type); - - $loader = new ChainedLoader($this->mockResolver([$loaderA, $loaderB])); - - $loader->import($type); - $loader->load($type); - } - - /** @test */ - public function itShouldThrowLoaderExceptionIfLoaderIsNotResolveable() - { - $loader = new ChainedLoader($this->mockResolver()); - - try { - $loader->load('resource'); - } catch (\Lucid\Resource\Exception\LoaderException $e) { - $this->assertEquals('No matching loader found.', $e->getMessage()); - } - - } - - /** @test */ - public function itShouldAddAndRemoveListener() - { - $ld = $this->mockLoader(); - $res = $this->mockResolver([$ld]); - - $listener = $this->getMockbuilder('Lucid\Resource\Loader\ListenerInterface') - ->disableOriginalConstructor()->getMock(); - - $ld->expects($this->once())->method('addListener')->with($listener); - $ld->expects($this->once())->method('removeListener')->with($listener); - - $loader = new ChainedLoader($res); - - $loader->addListener($listener); - $loader->removeListener($listener); - } - - private function mockLoader() - { - return $this->getMockbuilder('Lucid\Resource\Loader\LoaderInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - private function mockResolver(array $loaders = []) - { - $resolver = $this->getMockbuilder('Lucid\Resource\Loader\ResolverInterface') - ->disableOriginalConstructor() - ->getMock(); - - $resolver->method('all')->willReturn($loaders); - - return $resolver; - } -} diff --git a/lucid/resource/tests/Loader/ResolverTest.php b/lucid/resource/tests/Loader/ResolverTest.php deleted file mode 100644 index 6fdf756..0000000 --- a/lucid/resource/tests/Loader/ResolverTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests\Loader; - -use Lucid\Resource\Loader\Resolver; -use Lucid\Resource\Exception\LoaderException; - -/** - * @class ResolverTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class ResolverTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\Loader\ResolverInterface', new Resolver); - } - - /** @test */ - public function itShouldAddLoaders() - { - $res = null; - $loader = $this->mockLoader(); - $loader->expects($this->once())->method('setResolver'); - - $res = new Resolver([$loader]); - - $this->assertSame([$loader], $res->all()); - } - - /** @test */ - public function itShouldResolveResources() - { - $res = new Resolver(); - $res->addLoader($loader = $this->mockLoader()); - - $loader->method('supports')->with(__FILE__)->willReturn(true); - - $res->resolve(__FILE__); - - $res = new Resolver(); - $res->addLoader($loader = $this->mockLoader()); - - $loader->method('supports')->willReturn(false); - - try { - $res->resolve('nope'); - } catch (LoaderException $e) { - $this->assertSame('No loader found for resource "nope".', $e->getMessage()); - return; - } - - $this->fail(); - } - - private function mockLoader() - { - return $this->getMockbuilder('Lucid\Resource\Loader\LoaderInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/resource/tests/LocatorTest.php b/lucid/resource/tests/LocatorTest.php deleted file mode 100644 index 2866467..0000000 --- a/lucid/resource/tests/LocatorTest.php +++ /dev/null @@ -1,128 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests; - -use Lucid\Resource\Locator; - -/** - * @class LocatorTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class LocatorTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\LocatorInterface', new Locator); - } - - /** @test */ - public function itShouldLocateFiles() - { - $paths = [__DIR__]; - $file = basename(__FILE__); - - $locator = new Locator($paths); - - $this->assertInstanceOf('Lucid\Resource\CollectionInterface', $res = $locator->locate($file)); - - $resources = $res->all(); - $this->assertSame(__FILE__, (string)$resources[0]); - } - - /** @test */ - public function itShouldLocateMultipleFiles() - { - $base = dirname(__FILE__).DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR.'loc_'; - - $locator = new Locator([$baseA = $base.'a', $baseB = $base.'b']); - - $res = $locator->locate('file.txt', true); - - $this->assertArrayHasKey(0, $res->all()); - $this->assertArrayHasKey(1, $res->all()); - - $res = $res->all(); - - $this->assertSame($baseA.DIRECTORY_SEPARATOR.'file.txt', (string)$res[0]); - $this->assertSame($baseB.DIRECTORY_SEPARATOR.'file.txt', (string)$res[1]); - } - - /** @test */ - public function itShouldAddPathsToPatharray() - { - $base = dirname(__FILE__).DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR.'loc_'; - - $locator = new Locator([$baseA = $base.'a']); - - $locator->addPaths([$baseB = $base.'b', $base.'a']); - $res = $locator->locate('file.txt', true); - - $this->assertArrayHasKey(0, $res->all()); - $this->assertArrayHasKey(1, $res->all()); - - $res = $res->all(); - - $this->assertSame($baseA.DIRECTORY_SEPARATOR.'file.txt', (string)$res[0]); - $this->assertSame($baseB.DIRECTORY_SEPARATOR.'file.txt', (string)$res[1]); - } - - /** @test */ - public function itShouldReturnEmptyCollectionIfPathIsNotADirectoryOrFileDoesNotExist() - { - $base = dirname(__FILE__).DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR.'loc_'; - $locator = new Locator(['/not/a/path', $base.'a']); - - $this->assertSame([], $locator->locate('foo')->all()); - } - - /** @test */ - public function itShouldThrowIfRootpathIsInvalid() - { - $locator = new Locator(['bar/foo']); - $locator->setRootPath('/not/a/path'); - try { - $locator->locate('text.txt'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } - - $this->fail('Foo'); - } - - /** @test */ - public function itShouldExpandRelativePaths() - { - $locator = new Locator; - $locator->setRootPath(__DIR__.DIRECTORY_SEPARATOR.'Fixures'); - $locator->addPath('loc_a'); - - $res = $locator->locate('file.txt'); - - $this->assertArrayHasKey(0, $res->all()); - } - - /** @test */ - public function itShouldReturnCollection() - { - $paths = [__DIR__]; - $file = basename(__FILE__); - - $locator = new Locator($paths); - - $this->assertInstanceOf('Lucid\Resource\CollectionInterface', $locator->locate($file, true)); - } -} diff --git a/lucid/resource/tests/ObjectResourceTest.php b/lucid/resource/tests/ObjectResourceTest.php deleted file mode 100644 index be9b88b..0000000 --- a/lucid/resource/tests/ObjectResourceTest.php +++ /dev/null @@ -1,98 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests; - -use Lucid\Resource\ObjectResource; - -/** - * @class ObjectResourceTest - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class ObjectResourceTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Resource\ResourceInterface', new ObjectResource($this)); - } - - /** @test */ - public function itShouldGetObjectFilePath() - { - $resource = new ObjectResource($this); - - $this->assertSame(__FILE__, $resource->getResource()); - } - - /** @test */ - public function itShouldBeLocal() - { - $resource = new ObjectResource($this); - - $this->assertTrue($resource->isLocal()); - - $resource = new ObjectResource($obj = $this->getMock('ObjResourceMock')); - $this->assertFalse($resource->isLocal()); - } - - /** @test */ - public function itShouldBeValid() - { - $resource = new ObjectResource($this); - $this->assertTrue($resource->isValid(time())); - } - - /** @test */ - public function itShouldBeInvalid() - { - $resource = new ObjectResource($this); - $this->assertFalse($resource->isValid(filemtime(__FILE__) - 1)); - } - - /** @test */ - public function itShouldBeSerializable() - { - $resource = new ObjectResource($obj = $this); - - $data = serialize($resource); - $ret = unserialize($data); - - $this->assertSame(get_class($resource), get_class($ret)); - } - - /** @test */ - public function itShouldThrowIfNotAnObject() - { - try { - $resource = new ObjectResource('not an object.'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } - $this->fail('InvalidArgumentException should be thrown.'); - } - - /** @test */ - public function itShouldThrowIfInternalObject() - { - try { - $resource = new ObjectResource(new \stdClass); - } catch (\LogicException $e) { - $this->assertTrue(true); - return; - } - $this->fail('LogicException should be thrown.'); - } -} diff --git a/lucid/resource/tests/Stubs/PhpFileLoader.php b/lucid/resource/tests/Stubs/PhpFileLoader.php deleted file mode 100644 index 905a466..0000000 --- a/lucid/resource/tests/Stubs/PhpFileLoader.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Resource\Tests\Stubs; - -use Lucid\Resource\Loader\AbstractFileLoader; - -/** - * @class PhpFileLoader - * - * @package Lucid\Resource - * @version $Id$ - * @author iwyg - */ -class PhpFileLoader extends AbstractFileLoader -{ - protected function doLoad($resource) - { - } - - protected function getExtensions() - { - return ['php']; - } -} diff --git a/lucid/signal/.coveralls.yml b/lucid/signal/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/signal/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/signal/.phpunit b/lucid/signal/.phpunit deleted file mode 100755 index 379530d..0000000 --- a/lucid/signal/.phpunit +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then - ./vendor/bin/phpunit --verbose; -else - ./vendor/bin/phpunit --verbose --coverage-text --coverage-clover /tmp/coverage/coverage.xml -fi diff --git a/lucid/signal/.travis.yml b/lucid/signal/.travis.yml deleted file mode 100644 index 3d97e8c..0000000 --- a/lucid/signal/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/signal/README.md b/lucid/signal/README.md deleted file mode 100644 index b171495..0000000 --- a/lucid/signal/README.md +++ /dev/null @@ -1,108 +0,0 @@ -# Event Dispatcher Library - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/signal/tree/master) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/signal/blob/master/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/signal/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/signal) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/signal/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/signal) -[![HHVM](https://img.shields.io/hhvm/lucid/signal/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/signal) - -## Requirements - -``` -php >= 5.6 -``` - -## Installation - -You may install `lucid/signal` with composer. - -```bash -$ composer require lucid/signal --save -``` - -## Usage - -```php -addHandler('my_event', function (EventInterface $event) { - // do something -}); -``` - -### Event Handlers - -Eventhandlers can be any callable but must accept an instance of `EventInterface` -as their first argument. - -Using handlers the implement the `HandlerInterface` will automatically call the `handleEvent` method on the handler if the event is dispatched. - -```php -addHandler('my_event', $handler); - -``` - -`MyHandler::handleEvent` will now be called when `my_event` is fired. - -### Event Delegation - -Events are fired subsequentially unless all handlers where adressed or until -the Event object is being stopped. You can stop the eventdelegation in your -handler by calling `$event->stop()`. - -### Custom Events - -Event objects can be referred to message objects. You can easily create your -custom message objects by implementing the `EventInterface` interface or -extending the `Event` base class. - -```php -message = $message; - } - - public function getMessage() - { - return $this->message; - } -} -``` diff --git a/lucid/signal/composer.json b/lucid/signal/composer.json deleted file mode 100644 index 64b47c0..0000000 --- a/lucid/signal/composer.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "lucid/signal", - "license": "MIT", - "description": "Event Dispatcher Library", - "keywords": ["events", "eventing"], - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require-dev": { - "php":">=5.6" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev", - "container-interop/container-interop": "~1.1.0" - }, - "autoload": { - "psr-4": { - "Lucid\\Signal\\":"src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Signal\\Tests\\":"tests/" - } - }, - "suggest": { - "container-interop/container-interop": "For using the ContainerAwareEventDispatcher goodness." - }, - "minimum-stability": "dev" -} diff --git a/lucid/signal/phpunit.xml.dist b/lucid/signal/phpunit.xml.dist deleted file mode 100644 index 22e19e1..0000000 --- a/lucid/signal/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/signal/src/.coveralls.yml b/lucid/signal/src/.coveralls.yml deleted file mode 100644 index 1f545eb..0000000 --- a/lucid/signal/src/.coveralls.yml +++ /dev/null @@ -1,3 +0,0 @@ -src_dir: '' -coverage_clover: report/clover/coverage.xml -json_path: report/coveralls-upload.json diff --git a/lucid/signal/src/.travis.yml b/lucid/signal/src/.travis.yml deleted file mode 100644 index 43890ea..0000000 --- a/lucid/signal/src/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php -php: - - 5.4 - - 5.5 - - 5.6 - - hhvm - - hhvm-nightly -matrix: - allow_failures: - - php: hhvm - - php: hhvm-nightly - fast_finish: true -before_script: - - composer self-update - - composer install --prefer-source --no-interaction -script: - - vendor/bin/phpunit -after_script: - - php vendor/bin/coveralls -v -notififation: - on_success: never - on_failure: always - diff --git a/lucid/signal/src/ChainedEvent.php b/lucid/signal/src/ChainedEvent.php deleted file mode 100644 index 5db3366..0000000 --- a/lucid/signal/src/ChainedEvent.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class ChainedEvent - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class ChainedEvent extends Event implements ChainedEventInterface -{ - /** @var EventDispatcherInterface */ - private $dispatcher; - - /** - * {@inheritdoc} - */ - final public function setDispatcher(EventDispatcherInterface $dispatcher) - { - $this->dispatcher = $dispatcher; - } - - /** - * {@inheritdoc} - */ - final public function getDispatcher() - { - return $this->dispatcher; - } -} diff --git a/lucid/signal/src/ChainedEventInterface.php b/lucid/signal/src/ChainedEventInterface.php deleted file mode 100644 index e0b5dd6..0000000 --- a/lucid/signal/src/ChainedEventInterface.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @interface ChainedEventInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface ChainedEventInterface extends EventInterface -{ - /** - * setDispatcher - * - * @param EventDispatcherInterface $dispatcher - * - * @return void - */ - public function setDispatcher(EventDispatcherInterface $dispatcher); - - /** - * getDispatcher - * - * @return EventDispatcherInterface|null - */ - public function getDispatcher(); -} diff --git a/lucid/signal/src/ContainerAwareDispatcher.php b/lucid/signal/src/ContainerAwareDispatcher.php deleted file mode 100644 index ec9eb2b..0000000 --- a/lucid/signal/src/ContainerAwareDispatcher.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -use Interop\Container\ContainerInterface; - -/** - * @class ContainerAwareDispatcher - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class ContainerAwareDispatcher extends EventDispatcher -{ - /** @var string */ - const M_SEPARATOR = '@'; - - /** @var ContainerInterface */ - private $container; - - /** - * Constructor. - * - * @param ContainerInterface $container - */ - public function __construct(ContainerInterface $container) - { - $this->container = $container; - } - - /** - * {@inheritdoc} - */ - protected function getHandler($handler) - { - if (is_string($handler)) { - if ($this->hasAttachedMethod($handler)) { - list($id, ) = explode(self::M_SEPARATOR, $handler); - } else { - $id = $handler; - } - - if ($this->container->has($id)) { - return $handler; - } - } - - return parent::getHandler($handler); - } - - /** - * hasAttachedMethod - * - * @param string $string - * - * @return bool - */ - private function hasAttachedMethod($string) - { - return 1 === substr_count($string, self::M_SEPARATOR) && 0 !== strpos($string, self::M_SEPARATOR); - } -} diff --git a/lucid/signal/src/Event.php b/lucid/signal/src/Event.php deleted file mode 100644 index e79a987..0000000 --- a/lucid/signal/src/Event.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class Event - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class Event implements EventInterface -{ - use EventTrait; - - /** - * Constructor. - * - * @param string $name - */ - public function __construct($name = null) - { - $this->originalName = new EventName($this, (string)$name); - } -} diff --git a/lucid/signal/src/EventDispatcher.php b/lucid/signal/src/EventDispatcher.php deleted file mode 100644 index 981f2b6..0000000 --- a/lucid/signal/src/EventDispatcher.php +++ /dev/null @@ -1,205 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -use RuntimeException; -use InvalidArgumentException; - -/** - * @class EventDispatcher - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class EventDispatcher implements EventDispatcherInterface -{ - /** @var array */ - private $handlers = []; - - /** - * {@inheritdoc} - */ - public function addHandler($events, $handler, $priority = PriorityInterface::PRIORITY_NORMAL) - { - foreach ((array)$events as $event) { - $this->getHandlerQueue($event)->add($this->getHandler($handler), $priority); - } - } - - /** - * {@inheritdoc} - */ - public function removeHandler($events, $handler = null) - { - foreach ((array)$events as $event) { - if (null === $handler) { - unset($this->handlers[$event]); - } elseif ($this->hasEvent($event)) { - $this->getHandlerQueue($event)->remove($this->getHandler($handler)); - } - } - } - - /** - * {@inheritdoc} - */ - public function addSubscriber(SubscriberInterface $subscriber) - { - foreach ($subscriber->getSubscriptions() as $name => $methods) { - if (is_array($methods) && 2 === count($methods) && is_int($methods[1])) { - $this->addHandler($name, [$subscriber, $methods[0]], $methods[1]); - - continue; - } - - foreach ((array)($methods) as $method) { - list($method, $prio) = array_pad((array)$method, 2, 0); - $this->addHandler($name, [$subscriber, $method], $prio); - } - } - } - - /** - * {@inheritdoc} - */ - public function removeSubscriber(SubscriberInterface $subscriber) - { - foreach ($subscriber->getSubscriptions() as $name => $methods) { - if (is_array($methods) && 2 === count($methods) && is_int($methods[1])) { - $this->removeHandler($name, [$subscriber, $methods[0]]); - - continue; - } - - foreach ((array)($methods) as $method) { - list($method, ) = array_pad((array)$method, 2, 0); - $this->removeHandler($name, [$subscriber, $method]); - } - } - } - - /** - * {@inheritdoc} - */ - public function dispatchEvents(array $events) - { - foreach ($events as $event) { - $this->dispatchEvent($event); - } - } - - /** - * {@inheritdoc} - */ - public function dispatchEvent(EventInterface $event) - { - $this->dispatch((string)(new EventName($event)), $event); - } - - /** - * {@inheritdoc} - */ - public function dispatch($eventName, EventInterface $event = null) - { - $event = $event ?: new Event; - - if ($event instanceof ChainedEventInterface) { - $event->setDispatcher($this); - } - - foreach ((array)$eventName as $name) { - if (!$this->hasEvent($name)) { - continue; - } - - $event->setName($name); - - foreach ($this->handlers[$name]->all() as $handler) { - if ($event->isStopped()) { - break; - } - - call_user_func($handler, $event); - } - } - } - - /** - * {@inheritdoc} - */ - public function getHandlers($events = null) - { - $handlers = []; - - if (null === $events) { - foreach ($this->handlers as $event => $queue) { - $handlers = array_merge($handlers, $queue->toArray()); - } - } - - foreach ((array)$events as $event) { - if ($this->hasEvent($event)) { - $handlers = array_merge($handlers, $this->handlers[$event]->toArray()); - } - } - - return $handlers; - } - - /** - * {@inheritdoc} - */ - public function hasEvent($event) - { - return array_key_exists($event, $this->handlers); - } - - /** - * getHandler - * - * @param $handler - * @throws InvalidArgumentException - * - * @return callable - */ - protected function getHandler($handler) - { - if ($handler instanceof HandlerInterface) { - return [$handler, 'handleEvent']; - } - - if (is_callable($handler)) { - return $handler; - } - - throw new InvalidArgumentException( - sprintf('Invalid handler "%s".', is_string($handler) ? $handler : gettype($handler)) - ); - } - - /** - * getHandlerQueue - * - * @param mixed $event - * - * @return PriorityInterface - */ - private function getHandlerQueue($event) - { - if (!isset($this->handlers[$event])) { - $this->handlers[$event] = new Priority; - } - - return $this->handlers[$event]; - } -} diff --git a/lucid/signal/src/EventDispatcherInterface.php b/lucid/signal/src/EventDispatcherInterface.php deleted file mode 100644 index 77a40a2..0000000 --- a/lucid/signal/src/EventDispatcherInterface.php +++ /dev/null @@ -1,107 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @interface EventDispatcherInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface EventDispatcherInterface -{ - /** - * Register a handler for one or more events. - * - * @param mixed $events event names. - * @param mixed $handler the event handler - * @param int $priority dispatch priority - * - * @return void - */ - public function addHandler($events, $handler, $priority = PriorityInterface::PRIORITY_NORMAL); - - /** - * Remove one or all handers for given events - * - * @param mixed $events Event name as string or array of event names. - * @param mixed $handler callable handler or handler as string declaration - * - * @return void - */ - public function removeHandler($events, $handler = null); - - /** - * Register a subscriber. - * - * @param \Lucid\Signal\SubscriberInterface $subscriber - * - * @return void - */ - public function addSubscriber(SubscriberInterface $subscriber); - - /** - * Remove a subscriber. - * - * @param SubscriberInterface $subscriber - * - * @return void - */ - public function removeSubscriber(SubscriberInterface $subscriber); - - /** - * Check if a event has been registered. - * - * @param string $event - * - * @return boolean - */ - public function hasEvent($event); - - /** - * Dispatches an array of Event objects. - * - * @param array $events - * - * @return void - */ - public function dispatchEvents(array $events); - - /** - * Dispatches an event for a single Event object. - * - * @param EventInterface $event - * - * @return void - */ - public function dispatchEvent(EventInterface $event); - - /** - * Dispatch one or multple events. - * - * @param mixed $eventName - * @param \Lucid\Signal\EventInterface $event - * - * @return void - */ - public function dispatch($eventName, EventInterface $event = null); - - /** - * Get the handler for one or more events. - * - * @param string $event - * - * @return array - */ - public function getHandlers($event = null); -} diff --git a/lucid/signal/src/EventInterface.php b/lucid/signal/src/EventInterface.php deleted file mode 100644 index df29e2d..0000000 --- a/lucid/signal/src/EventInterface.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class EventInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface EventInterface -{ - /** - * Stop event delegation for this event. - * - * @return void - */ - public function stop(); - - /** - * Check if event delegation is stopped for this event. - * - * @return boolean - */ - public function isStopped(); - - /** - * Set the name of the event. - * - * @param string $name - * - * @return void - */ - public function setName($name); - - /** - * Get the event name - * - * @return EventName - */ - public function getName(); - - /** - * Get the event name - * - * @return EventName - */ - public function getOriginalName(); -} diff --git a/lucid/signal/src/EventName.php b/lucid/signal/src/EventName.php deleted file mode 100644 index e9bd4f8..0000000 --- a/lucid/signal/src/EventName.php +++ /dev/null @@ -1,91 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class EventName - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class EventName -{ - /** @var string */ - private $name; - - /** @var EventInterface */ - private $event; - - /** - * Construct. - * - * @param EventInterface $event - * @name string $name - */ - public function __construct(EventInterface $event, $name = null) - { - $this->event = $event; - $this->name = $name; - } - - /** - * Get the event name. - * - * @return string - */ - public function getName() - { - if (!$this->isEmpty($this->name)) { - return $this->name; - } - - foreach (['getName', 'getOriginalName'] as $fn) { - $name = call_user_func([$this->event, $fn]); - - if ($this->isEmpty($name) || $name === $this) { - continue; - } - - if ($name instanceof self) { - return $name = $name->getName(); - } - } - - return $this->name = $this->parseEventName(); - } - - /** - * @see EventName#getName() - */ - public function __toString() - { - return (string)$this->getName(); - } - - /** - * Parses an event object into a readable event name. - * - * @return string - */ - private function parseEventName() - { - $name = basename(strtr(get_class($this->event), ['\\' => '/'])); - - return strtolower(preg_replace('#[A-Z]#', '.$0', lcfirst($name))); - } - - private function isEmpty($name) - { - return null === $name || empty($name); - } -} diff --git a/lucid/signal/src/EventTrait.php b/lucid/signal/src/EventTrait.php deleted file mode 100644 index c81b818..0000000 --- a/lucid/signal/src/EventTrait.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @trait EventTrait - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -trait EventTrait -{ - /** @var EventName */ - private $originalName; - - /** @var EventName */ - private $name; - - /** @var bool */ - private $isStopped = false; - - /** - * {@inheritdoc} - */ - public function __clone() - { - $this->isStopped = false; - } - - /** - * {@inheritdoc} - */ - final public function stop() - { - $this->isStopped = true; - } - - /** - * {@inheritdoc} - */ - final public function isStopped() - { - return $this->isStopped; - } - - /** - * {@inheritdoc} - */ - final public function setName($name) - { - $this->name = new EventName($this, $name); - } - - /** - * {@inheritdoc} - */ - final public function getName() - { - return $this->name ?: $this->originalName; - } - - /** - * {@inheritdoc} - */ - final public function getOriginalName() - { - return $this->originalName; - } -} diff --git a/lucid/signal/src/HandlerInterface.php b/lucid/signal/src/HandlerInterface.php deleted file mode 100644 index 1244417..0000000 --- a/lucid/signal/src/HandlerInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class HandlerInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface HandlerInterface -{ - /** - * Will be called on a specific event. - * - * @param EventInterface $event the event that's been dispatched. - * - * @return void - */ - public function handleEvent(EventInterface $event); -} diff --git a/lucid/signal/src/Priority.php b/lucid/signal/src/Priority.php deleted file mode 100644 index 42dc5b0..0000000 --- a/lucid/signal/src/Priority.php +++ /dev/null @@ -1,149 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -use RuntimeException; - -/** - * @class Priority - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class Priority implements PriorityInterface -{ - /** @var array */ - private $lut = []; - - /** @var array */ - private $handlers = []; - - /** @var bool */ - private $sorted = false; - - /** - * {@inheritdoc} - */ - public function add($handler, $priority) - { - $this->sorted = false; - $this->handlers[$priority][] = $handler; - $this->lut[$this->getHandlerString($handler)][] = $priority; - } - - /** - * {@inheritdoc} - */ - public function remove($handler) - { - $str = $this->getHandlerString($handler); - - if (!isset($this->lut[$str])) { - return false; - } - - // remove handler from lut and handlers array - foreach ($this->lut[$str] as $i => $prio) { - unset($this->handlers[$prio]); - unset($this->lut[$str][$i]); - } - - return true; - } - - /** - * {@inheritdoc} - */ - public function all() - { - $this->sort(); - - foreach ($this->handlers as $handlers) { - foreach ($handlers as $handler) { - yield $handler; - } - } - } - - /** - * {@inheritdoc} - */ - public function flush() - { - $this->sort(); - - foreach ($this->handlers as $handlers) { - foreach ($handlers as $handler) { - if ($this->remove($handler)) { - yield $handler; - } - } - } - } - - /** - * toArray - * - * @return array - */ - public function toArray() - { - $handlers = []; - - foreach ($this->all() as $handler) { - $handlers[] = $handler; - } - - return $handlers; - } - - /** - * getHandlerString - * - * @param mixed $handler - * @throws RuntimeException - * - * @return string - */ - private function getHandlerString($handler) - { - if (is_string($handler)) { - return $handler; - } - - if (is_object($handler) || $handler instanceof \Closure) { - return spl_object_hash($handler); - } - - if (is_array($handler) && is_callable($handler)) { - return $this->getHandlerString($handler[0]) . '@' . $handler[1]; - } - - throw new RuntimeException('Can\'t convert handler to string.'); - } - - /** - * sort - * - * @return void - */ - private function sort() - { - if ($this->sorted) { - return; - } - - krsort($this->handlers); - $this->sorted = true; - } -} diff --git a/lucid/signal/src/PriorityInterface.php b/lucid/signal/src/PriorityInterface.php deleted file mode 100644 index a231f97..0000000 --- a/lucid/signal/src/PriorityInterface.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @interface PriorityInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface PriorityInterface -{ - /** - * Default priority level. - * @var int - */ - const PRIORITY_NORMAL = 0; - - /** - * High priority level. - * @var int - */ - const PRIORITY_HIGH = 1000; - - /** - * Low priority level. - * @var int - */ - const PRIORITY_LOW = -1000; - - /** - * Adds a thing to the pool. - * - * @param mixed $handler - * @param int $priority - * - * @return void - */ - public function add($thing, $priority); - - /** - * Removes a thing. - * - * @param mixed $thing - * - * @return bool - */ - public function remove($thing); - - /** - * Returns all things ordered by priority - * by returning an Iterator. - * - * @return \Iterator - */ - public function all(); - - /** - * Like `all()`, but removes all things. - * - * @return void - */ - public function flush(); -} diff --git a/lucid/signal/src/SubscriberInterface.php b/lucid/signal/src/SubscriberInterface.php deleted file mode 100644 index bd23602..0000000 --- a/lucid/signal/src/SubscriberInterface.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class SubscriberInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface SubscriberInterface -{ - /** - * Get event subscriptions. - * - * @return array Array of event subscriptions - */ - public function getSubscriptions(); -} diff --git a/lucid/signal/src/Subscription.php b/lucid/signal/src/Subscription.php deleted file mode 100644 index e8313da..0000000 --- a/lucid/signal/src/Subscription.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @class Subscription - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class Subscription implements SubscriptionInterface -{ - /** @var array */ - private $subscriptions; - - /** - * Constructor. - * - * @param array $subscriptions - */ - public function __construct(array $subscriptions) - { - $this->subscriptions = $subscriptions; - } - - /** - * {@inheritdoc} - */ - public function get() - { - foreach ($this->subscriptions as $e => $sub) { - yield $e => $sub; - } - } -} diff --git a/lucid/signal/src/SubscriptionInterface.php b/lucid/signal/src/SubscriptionInterface.php deleted file mode 100644 index 3d3541c..0000000 --- a/lucid/signal/src/SubscriptionInterface.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal; - -/** - * @interface SubscriptionInterface - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -interface SubscriptionInterface -{ - /** - * Returns an iteratable list of subscriptions. - * - * @return \Iterator - */ - public function get(); -} diff --git a/lucid/signal/tests/ChainedEventTest.php b/lucid/signal/tests/ChainedEventTest.php deleted file mode 100644 index 64298c7..0000000 --- a/lucid/signal/tests/ChainedEventTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\ChainedEvent; - -/** - * @class ChainedEventTest - * - * @package Lucid\Signal\Tests - * @version $Id$ - * @author iwyg - */ -class ChainedEventTest extends EventTest -{ - /** @test */ - public function aDispatcherShouldBeSettable() - { - $event = $this->newEvent(); - - $d = $this->getMockBuilder('Lucid\Signal\EventDispatcherInterface') - ->disableOriginalConstructor() - ->getMock(); - $event->setDispatcher($d); - - $this->assertTrue($d === $event->getDispatcher()); - } - - protected function newEvent() - { - return new ChainedEvent; - } -} diff --git a/lucid/signal/tests/ContainerAwareDispatcherTest.php b/lucid/signal/tests/ContainerAwareDispatcherTest.php deleted file mode 100644 index 30a254e..0000000 --- a/lucid/signal/tests/ContainerAwareDispatcherTest.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\ContainerAwareDispatcher; - -/** - * @class ContainerAwareDispatcherTest - * - * @package Lucid\Signal\Tests - * @version $Id$ - * @author iwyg - */ -class ContainerAwareDispatcherTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf( - 'Lucid\Signal\EventDispatcherInterface', - new ContainerAwareDispatcher($this->mockContainer()) - ); - } - - /** @test */ - public function itShouldAcceptServiceSignature() - { - $container = $this->mockContainer(); - $container->method('has')->with('myservice')->willReturn(true); - - $dispatcher = new ContainerAwareDispatcher($container); - $dispatcher->addHandler('myevent', 'myservice@handleEvent'); - - $this->assertTrue($dispatcher->hasEvent('myevent')); - } - - /** @test */ - public function itShouldAcceptServiceSignatureWithoutAttachedMethod() - { - $container = $this->mockContainer(); - $container->method('has')->with('myservice')->willReturn(true); - - $dispatcher = new ContainerAwareDispatcher($container); - $dispatcher->addHandler('myevent', 'myservice'); - - $this->assertTrue($dispatcher->hasEvent('myevent')); - } - - /** @test */ - public function itShouldNotAcceptServiceSignatureIfInvalid() - { - $container = $this->mockContainer(); - $container->method('has')->with('myservice')->willReturn(false); - - $dispatcher = new ContainerAwareDispatcher($container); - try { - $dispatcher->addHandler('myevent', 'myservice@handleMethod'); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('Invalid handler "myservice@handleMethod".', $e->getMessage()); - - return; - } - - $this->fail(); - } - - private function mockContainer() - { - return $this->getMockbuilder('Interop\Container\ContainerInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/signal/tests/EventDispatcherTest.php b/lucid/signal/tests/EventDispatcherTest.php deleted file mode 100644 index 183cf86..0000000 --- a/lucid/signal/tests/EventDispatcherTest.php +++ /dev/null @@ -1,260 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\Event; -use Lucid\Signal\EventDispatcher; -use Lucid\Signal\Tests\Stubs\SimpleSubscriber; - -/** - * @class EventDispatcherTest - * - * @package Lucid\Signal\Tests - * @version $Id$ - * @author iwyg - */ -class EventDispatcherTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Signal\EventDispatcher', new EventDispatcher); - } - - /** @test */ - public function itShouldAllowInvokableHandlers() - { - $invoked = false; - - $handler = $this->getMock('InvokableHandler', ['__invoke']); - $handler->method('__invoke')->willReturnCallback(function () use (&$invoked) { - $invoked = true; - }); - - $events = new EventDispatcher; - $events->addHandler('event', $handler); - - $events->dispatch('event'); - - $this->assertTrue($invoked); - } - - /** @test */ - public function itShouldAddHandlers() - { - $handle = false; - $events = new EventDispatcher; - - $events->addHandler('event', $handler = $this->mockHandler()); - - $handler->method('handleEvent')->willReturnCallback(function () use (&$handle) { - $handle = true; - }); - - $events->dispatch('event'); - - $this->assertTrue($handle); - } - - /** @test */ - public function itShouldDispatchEventHandlersInOrder() - { - $order = []; - $events = new EventDispatcher; - - $events->addHandler('event', $handlerA = $this->mockHandler(), 1); - $events->addHandler('event', $handlerB = $this->mockHandler(), 10); - $events->addHandler('event', $handlerC = $this->mockHandler(), 0); - - $handlerA->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'A'; - }); - $handlerB->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'B'; - }); - $handlerC->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'C'; - }); - - $events->dispatch('event'); - - $this->assertSame(['B', 'A', 'C'], $order); - } - - /** @test */ - public function itShouldDispatchEvents() - { - $event = new Event('my_event'); - - $event->setName('my_event'); - $events = new EventDispatcher; - $events->addHandler('my_event', $handler = $this->mockHandler(), 1); - - $invoked = false; - $handler->method('handleEvent')->willReturnCallback(function () use (&$invoked) { - $invoked = true; - }); - - $events->dispatchEvents([$event]); - - $this->assertTrue($invoked); - } - - /** @test */ - public function itShouldGetHandlers() - { - $events = new EventDispatcher; - - $events->addHandler('event', $a = $this->mockHandler(), 1); - $events->addHandler('event', $b = $this->mockHandler(), 10); - $events->addHandler('myevent', $c = $this->mockHandler(), 100); - - $this->assertSame([[$b, 'handleEvent'], [$a, 'handleEvent']], $events->getHandlers('event')); - $this->assertSame([[$b, 'handleEvent'], [$a, 'handleEvent'], [$c, 'handleEvent']], $events->getHandlers()); - } - - /** @test */ - public function itShouldAddSubscribers() - { - $events = new EventDispatcher; - - $sj = new \stdClass; - - $sub = new SimpleSubscriber($sj); - - $events->addSubscriber($sub); - - $this->assertSame([[$sub, 'onA']], $events->getHandlers('eventA')); - $this->assertSame([[$sub, 'onB']], $events->getHandlers('eventB')); - } - - /** @test */ - public function itShouldRemoveSubscribers() - { - $events = new EventDispatcher; - - $events = new EventDispatcher; - - $sj = new \stdClass; - - $sub = new SimpleSubscriber($sj); - - $events->addSubscriber($sub); - $events->removeSubscriber($sub); - - $this->assertSame([], $events->getHandlers('eventA')); - $this->assertSame([], $events->getHandlers('eventB')); - } - - /** @test */ - public function itShouldTrowOnInvalidHandler() - { - $events = new EventDispatcher; - try { - $events->addHandler('event', 'foo@bar'); - } catch (\InvalidArgumentException $e) { - $this->assertSame('Invalid handler "foo@bar".', $e->getMessage()); - return; - } - - $this->fail('Test should throw InvalidArgumentException.'); - } - - /** @test */ - public function itShouldInvokeHandlerMethod() - { - $called = false; - - $handler = $this->getMock('Lucid\Signal\HandlerInterface'); - $handler->method('handleEvent')->will($this->returnCallback(function () use (&$called) { - $called = true; - })); - - $events = new EventDispatcher; - $events->addHandler('event', $handler); - $events->dispatch('event'); - - $this->assertTrue($called); - } - - /** @test */ - public function itShouldRemoveHandler() - { - $order = []; - $events = new EventDispatcher; - - $events->addHandler('event', $handlerA = $this->mockHandler(), 1); - $events->addHandler('event', $handlerB = $this->mockHandler(), 10); - $events->addHandler('event', $handlerC = $this->mockHandler(), 20); - - $handlerA->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'A'; - }); - $handlerB->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'B'; - }); - $handlerC->method('handleEvent')->willReturnCallback(function () use (&$order) { - $order[] = 'C'; - }); - - $events->removeHandler('event', $handlerA); - - $events->dispatch('event'); - - $this->assertSame(['C', 'B'], $order); - - $order = []; - $events->removeHandler('event'); - $events->dispatch('event'); - - $this->assertSame([], $order); - } - - /** @test */ - public function itShouldStopDispatchingIfEventIsStopped() - { - $event = new Event('IAmStopped'); - - $events = new EventDispatcher; - - $events->addHandler('IAmStopped', function ($event) { - $event->stop(); - }); - - $events->addHandler('IAmStopped', function ($event) { - $this->fail('Handler should never been called.'); - }); - - $events->dispatchEvent($event); - $this->assertTrue(true); - } - - /** @test */ - public function itShouldSetDispatcherOnChainedEvents() - { - $event = $this->getMockbuilder('Lucid\Signal\ChainedEventInterface') - ->disableOriginalConstructor() - ->getMock(); - $event->expects($this->once())->method('setDispatcher'); - $event->method('getName')->willReturn('chained_event'); - - $events = new EventDispatcher; - $events->dispatchEvent($event); - } - - private function mockHandler() - { - return $this->getMockbuilder('Lucid\Signal\HandlerInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/signal/tests/EventNameTest.php b/lucid/signal/tests/EventNameTest.php deleted file mode 100644 index fec6e55..0000000 --- a/lucid/signal/tests/EventNameTest.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\Event; -use Lucid\Signal\EventName; - -/** - * @class EventNameTest - * - * @package Lucid\Signal\Tests - * @version $Id$ - * @author iwyg - */ -class EventNameTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldParseEventName() - { - $name = new EventName($e = new Event); - - $this->assertSame('event', (string)$name); - - $name = new EventName($e = new Event('tata')); - - $this->assertSame('tata', (string)$name); - - $event = $this->getMockbuilder('Lucid\Signal\EventInterface') - ->disableOriginalConstructor() - ->setMockClassName('MyGoofyEvent') - ->getMock(); - - $name = new EventName($event, null); - - $event->method('getOriginalName')->willReturn($name); - - $this->assertSame('my.goofy.event', (string)$name); - } - - /** @test */ - public function itShouldNotParseEventNameIfNameExists() - { - $event = new Event; - $event->setName('my_event'); - $name = new EventName($event); - - $this->assertSame('my_event', (string)$name); - } -} diff --git a/lucid/signal/tests/EventTest.php b/lucid/signal/tests/EventTest.php deleted file mode 100644 index bd890fe..0000000 --- a/lucid/signal/tests/EventTest.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\Event; - -/** - * @class EventTest - * - * @package Lucid\Signal\Tests - * @version $Id$ - * @author iwyg - */ -class EventTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeStoppable() - { - $event = $this->newEvent(); - - $this->assertFalse($event->isStopped()); - - $event->stop(); - $this->assertTrue($event->isStopped()); - - $clone = clone $event; - $this->assertFalse($clone->isStopped()); - } - - /** @test */ - public function aNameShouldBeSettable() - { - $event = $this->newEvent(); - $this->assertInstanceOf('Lucid\Signal\EventName', $event->getName()); - - $event->setName('event'); - $this->assertSame('event', (string)$event->getName()); - } - - protected function newEvent() - { - return new Event; - } -} diff --git a/lucid/signal/tests/PriorityTest.php b/lucid/signal/tests/PriorityTest.php deleted file mode 100644 index 3d39407..0000000 --- a/lucid/signal/tests/PriorityTest.php +++ /dev/null @@ -1,122 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests; - -use Lucid\Signal\Priority; - -/** - * @class PriorityTest - * - * @package Lucid\Signal - * @version $Id$ - * @author iwyg - */ -class PriorityTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Signal\PriorityInterface', new Priority); - } - - /** @test */ - public function allMustAlwaysReturnIterator() - { - $pri = new Priority; - $this->assertInstanceOf('\Iterator', $pri->all()); - } - - /** @test */ - public function flushMustAlwaysReturnIterator() - { - $pri = new Priority; - $this->assertInstanceOf('\Iterator', $pri->flush()); - } - - /** @test */ - public function flushNeedsToFlushHandlers() - { - $pri = new Priority; - - $pri->add('handlerA', 1); - $pri->add('handlerC', 20); - $pri->add('handlerB', 10); - - foreach ($pri->flush() as $handler) { - } - - $this->assertEquals([], $pri->toArray()); - } - - /** @test */ - public function itShouldThrowExceptionOnInvalidHandlers() - { - $pri = new Priority; - try { - $pri->add(['not_a_handler'], 0); - } catch (\RuntimeException $e) { - $this->assertSame('Can\'t convert handler to string.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldAlwaysReturnAnIteratable() - { - $pri = new Priority; - $this->assertInstanceof('\Iterator', $pri->all()); - } - - - /** @test */ - public function itShouldRemoveHandler() - { - $pri = new Priority; - - $pri->add('handlerA', 1); - $pri->add('handlerC', 20); - $pri->add('handlerB', 10); - - $pri->remove('handlerA'); - - $this->assertSame(['handlerC', 'handlerB'], $pri->toArray()); - - $pri->remove('c'); - $pri->remove('handlerC'); - - $this->assertSame(['handlerB'], $pri->toArray()); - } - - /** @test */ - public function itShouldAllHandlersInCorrectOrder() - { - $handlerA = 'handlerA'; - $handlerB = 'handlerB'; - $handlerC = 'handlerC'; - - $pri = new Priority; - $pri->add($handlerA, -10); - $pri->add($handlerB, 10); - $pri->add($handlerC, 0); - - $handlers = []; - - foreach ($pri->all() as $handler) { - $handlers[] = $handler; - } - - $this->assertSame(['handlerB', 'handlerC', 'handlerA'], $handlers); - } -} diff --git a/lucid/signal/tests/Stubs/SimpleSubscriber.php b/lucid/signal/tests/Stubs/SimpleSubscriber.php deleted file mode 100644 index e42fd89..0000000 --- a/lucid/signal/tests/Stubs/SimpleSubscriber.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Signal\Tests\Stubs; - -use Lucid\Signal\SubscriberInterface; - -/** - * @class SimpleSubscriber - * - * @package Lucid\Signal\Tests\Stubs - * @version $Id$ - * @author iwyg - */ -class SimpleSubscriber implements SubscriberInterface -{ - public $subject; - public function __construct(\stdClass $subj) - { - $this->subject = $subj; - } - - public function getSubscriptions() - { - return [ - 'eventA' => 'onA', - 'eventB' => 'onB' - ]; - } - - public function onA() - { - $this->subject->first = 'A'; - } - - public function onB() - { - $this->subject->second = 'B'; - } -} diff --git a/lucid/signal/tests/SubscriptionTest.php b/lucid/signal/tests/SubscriptionTest.php deleted file mode 100644 index f77a5dc..0000000 --- a/lucid/signal/tests/SubscriptionTest.php +++ /dev/null @@ -1,28 +0,0 @@ -assertInstanceOf('Lucid\Signal\SubscriptionInterface', new Subscription([])); - } - - /** @test */ - public function itIsExpectedThat() - { - $subs = [ - 'eventA' => 'onA', - 'eventB' => 'onB' - ]; - - $subscription = new Subscription($subs); - - foreach ($subscription->get() as $event => $s) { - } - } -} diff --git a/lucid/template/.coveralls.yml b/lucid/template/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/template/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/template/.phpunit b/lucid/template/.phpunit deleted file mode 100755 index 379530d..0000000 --- a/lucid/template/.phpunit +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then - ./vendor/bin/phpunit --verbose; -else - ./vendor/bin/phpunit --verbose --coverage-text --coverage-clover /tmp/coverage/coverage.xml -fi diff --git a/lucid/template/.travis.yml b/lucid/template/.travis.yml deleted file mode 100644 index 9321fd2..0000000 --- a/lucid/template/.travis.yml +++ /dev/null @@ -1,38 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - allow_failures: - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* --ignore=tests/Fixures/* src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/template/LICENSE.md b/lucid/template/LICENSE.md deleted file mode 100644 index f014f7d..0000000 --- a/lucid/template/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -Copyright (c) 2014-2016 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/template/README.md b/lucid/template/README.md deleted file mode 100644 index 3f27b30..0000000 --- a/lucid/template/README.md +++ /dev/null @@ -1,126 +0,0 @@ -# Templating library - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/template/tree/master) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/template/blob/master/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/template/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/template) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/template/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/template) -[![HHVM](https://img.shields.io/hhvm/lucid/template/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/template) - -An extendable templating library for php. - -## Requirements - -``` -php >= 5.6 -``` - -## Installation -```bash -$ composer require lucid/template -``` - -## Getting started - -```php -render('partials/content.php', ['title' => 'Hello World!']); - -``` - -## Partials - -### Inserts - -```php - - - -
- $view->insert('partials/footer.php'); - $view->insert('partials/content.php'); - $view->insert('partials/footer.php'); -
- - - -``` - -### Extending existing templates - -The templates - -`partials/content.php`: - -```php - -extend('master.php') ?> -section('content') ?> -

Extended content

-endsection() ?> - -``` - - -`master.php`: - -```php - - - - - <?= $title ?> - - -
- section('content') ?> -

The default content.

- endsection() ?> -
- - -``` - -### Sections - -### Tempalte Listeners - -Adding template listeners can be usefull if you want to add data to a specific -template. This data my be derieved from any resource you may want (e.g. DB, -Container, etc). - -```php -addListener('head.php', new RenderHeadListener($headerData)); -``` - -Your listener may look something like this - -```php -data = $data; - } - - public function onRender(TemplateDataInterface $data) - { - // add header data to `$data` - } -} -``` diff --git a/lucid/template/composer.json b/lucid/template/composer.json deleted file mode 100644 index 5e96cc6..0000000 --- a/lucid/template/composer.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "lucid/template", - "license": "MIT", - "description": "Templating library", - "keywords": ["templating"], - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require": { - "php":">=5.6" - }, - "require-dev": { - "psr/log": "^1.0", - "phpunit/phpunit": "5.2.*@dev" - }, - "autoload": { - "psr-4": { - "Lucid\\Template\\":"src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Template\\Tests\\":"tests/" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/template/phpunit.xml.dist b/lucid/template/phpunit.xml.dist deleted file mode 100644 index 663f328..0000000 --- a/lucid/template/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/template/src/.coveralls.yml b/lucid/template/src/.coveralls.yml deleted file mode 100644 index 1f545eb..0000000 --- a/lucid/template/src/.coveralls.yml +++ /dev/null @@ -1,3 +0,0 @@ -src_dir: '' -coverage_clover: report/clover/coverage.xml -json_path: report/coveralls-upload.json diff --git a/lucid/template/src/.travis.yml b/lucid/template/src/.travis.yml deleted file mode 100644 index 43890ea..0000000 --- a/lucid/template/src/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: php -php: - - 5.4 - - 5.5 - - 5.6 - - hhvm - - hhvm-nightly -matrix: - allow_failures: - - php: hhvm - - php: hhvm-nightly - fast_finish: true -before_script: - - composer self-update - - composer install --prefer-source --no-interaction -script: - - vendor/bin/phpunit -after_script: - - php vendor/bin/coveralls -v -notififation: - on_success: never - on_failure: always - diff --git a/lucid/template/src/AbstractPhpEngine.php b/lucid/template/src/AbstractPhpEngine.php deleted file mode 100644 index 89378c9..0000000 --- a/lucid/template/src/AbstractPhpEngine.php +++ /dev/null @@ -1,233 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -use Lucid\Template\Loader\LoaderInterface; -use Lucid\Template\Resource\ResourceInterface; - -/** - * @class AbstractEngine - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -abstract class AbstractPhpEngine implements EngineInterface, DisplayInterface, PhpRenderInterface -{ - /** - * loader - * - * @var LoaderInterface - */ - private $loader; - - /** - * identity - * - * @var TemplateIdentityInterface - */ - private $identity; - - /** - * itentities - * - * @var array - */ - private $itentities = []; - - /** - * loaded - * - * @var array - */ - private $loaded = []; - - /** - * types - * - * @var mixed - */ - private $types = []; - - /** - * view - * - * @var mixed - */ - private $view; - - /** - * Constructor. - * - * @param LoaderInterface $loader - * @param TemplateIdentity $identity - */ - public function __construct(LoaderInterface $loader, IdentityParserInterface $parser = null) - { - $this->loader = $loader; - $this->identity = $parser ?: new IdentityParser; - } - - /** - * {@inheritdoc} - */ - public function setManager(ViewManagerInterface $view) - { - $this->view = $view; - } - - /** - * {@inheritdoc} - */ - public function getManager() - { - return $this->view; - } - - /** - * {@inheritdoc} - */ - public function supports($template) - { - return in_array($this->findIdentity($template)->getType(), $this->getTypes()); - } - - /** - * addType - * - * @param mixed $type - * - * @return void - */ - public function addType($type) - { - $this->types[] = $type; - } - - /** - * {@inheritdoc} - */ - public function exists($template) - { - try { - return (bool)$this->loadTemplate($template); - } catch (\Exception $e) { - } - - return false; - } - - /** - * Try to load the given template - * - * @param mixed $template - * - * @return void - */ - public function loadTemplate($template) - { - if (!$this->isLoaded($identity = $this->findIdentity($template))) { - if (!$this->supports($identity)) { - throw new \InvalidArgumentException(sprintf('Unsupported template "%s".', $identity->getName())); - } - - $this->setLoaded($identity, $this->getLoader()->load($identity)); - } - - return $this->getLoaded($identity); - } - - abstract protected function getParameters($template, array $parameters); - - /** - * isLoaded - * - * @param TemplateInterface $template - * - * @return boolean - */ - protected function isLoaded(IdentityInterface $template) - { - return isset($this->loaded[$template->getName()]); - } - - /** - * getLoaded - * - * @param TemplateInterface $template - * - * @return TemplateInterface - */ - protected function getLoaded(IdentityInterface $identity) - { - return $this->loaded[$identity->getName()]; - } - - /** - * setLoaded - * - * @param TemplateInterface $template - * - * @return void - */ - protected function setLoaded(IdentityInterface $identity, ResourceInterface $resource) - { - $this->loaded[$identity->getName()] = $resource; - } - - /** - * getLoader - * - * @return void - */ - protected function getLoader() - { - return $this->loader; - } - - /** - * findIdentity - * - * @param mixed $template - * - * @return TemplateInterface - */ - protected function findIdentity($template) - { - if (!isset($this->identities[(string)$template])) { - $this->identities[(string)$template] = $this->getIdentity()->identify($template); - } - - return $this->identities[(string)$template]; - } - - /** - * getTypes - * - * @return array - */ - protected function getTypes() - { - return $this->types; - } - - /** - * getIdentity - * - * - * @return void - */ - protected function getIdentity() - { - return $this->identity; - } -} diff --git a/lucid/template/src/Data/Data.php b/lucid/template/src/Data/Data.php deleted file mode 100644 index 6067fc7..0000000 --- a/lucid/template/src/Data/Data.php +++ /dev/null @@ -1,122 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Data; - -use Lucid\Template\ViewManagerInterface; - -/** - * @class Parameters - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -final class Data implements TemplateDataInterface -{ - /** - * data - * - * @var array - */ - private $data; - - /** - * replace - * - * @var boolean - */ - private $replace; - - /** - * Constructor. - */ - public function __construct() - { - $this->data = []; - $this->replace = false; - } - - /** - * get - * - * @param mixed $key - * - * @return mixed - */ - public function get($key) - { - return isset($this->data[$key]) ? $this->data[$key] : null; - } - - /** - * all - * - * @param array $parameters - * - * @return array - */ - public function all(array $parameters = []) - { - if ($this->replace) { - return $this->data; - } - - return array_merge($parameters, $this->data); - } - - /** - * set - * - * @param array $data - * - * @return void - */ - public function set(array $data) - { - $this->data = []; - $this->replace = false; - - foreach ($data as $key => $value) { - $this->add($key, $value); - } - } - - /** - * replace - * - * @param array $data - * - * @return void - */ - public function replace(array $data) - { - $this->set($data); - $this->replace = true; - } - - /** - * addData - * - * @param mixed $key - * @param mixed $value - * - * @return void - */ - public function add($key, $value) - { - if (is_numeric($key)) { - throw new \InvalidArgumentException('Key must not be a number.'); - } - - $this->data[$key] = $value; - } -} diff --git a/lucid/template/src/Data/TemplateDataInterface.php b/lucid/template/src/Data/TemplateDataInterface.php deleted file mode 100644 index b4a62ea..0000000 --- a/lucid/template/src/Data/TemplateDataInterface.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Data; - -/** - * @interface TemplateDataInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface TemplateDataInterface -{ - /** - * addData - * - * @param array $data - * - * @return void - */ - public function add($key, $value); - - /** - * setData - * - * @param array $data - * - * @return void - */ - public function set(array $data); - - /** - * replace - * - * @param array $data - * - * @return void - */ - public function replace(array $data); - - /** - * getData - * - * @return array - */ - public function all(array $parameters = []); -} diff --git a/lucid/template/src/DelegatingEngine.php b/lucid/template/src/DelegatingEngine.php deleted file mode 100644 index e52b8e4..0000000 --- a/lucid/template/src/DelegatingEngine.php +++ /dev/null @@ -1,138 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @class DelegatingEngine - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class DelegatingEngine implements EngineInterface, DisplayInterface -{ - /** - * engines - * - * @var EngineInterface[] - */ - private $engines; - - /** - * Constructor. - * - * @param array $engines - */ - public function __construct(array $engines = []) - { - $this->setEngines($engines); - } - - /** - * Sets the render engines. - * - * @param array $engines - * - * @return void - */ - public function setEngines(array $engines) - { - $this->engines = []; - - foreach ($engines as $engine) { - $this->addEngine($engine); - } - } - - /** - * Adds a template Engine. - * - * @param EngineInterface $engine - * - * @return void - */ - public function addEngine(EngineInterface $engine) - { - $this->engines[] = $engine; - } - - /** - * {@inheritdoc} - */ - public function render($template, array $parameters = []) - { - return $this->getEngine($template)->render($template, $parameters); - } - - /** - * {@inheritdoc} - */ - public function display($template, array $parameters = []) - { - if (($engine = $this->getEngine($template)) instanceof DisplayInterface) { - $engine->display($template, $parameters); - } else { - echo $engine->render($template, $parameters); - } - } - - /** - * {@inheritdoc} - */ - public function supports($template) - { - if ($engine = $this->resolveEngine($template)) { - return true; - } - - return false; - } - - /** - * {@inheritdoc} - */ - public function exists($template) - { - if ($engine = $this->resolveEngine($template)) { - return $engine->exists($template); - } - - return false; - } - - /** - * resolveEngine - * - * @param mixed $template - * - * @return EngineInterface|bool false if no engine is false; - */ - public function resolveEngine($template) - { - foreach ($this->engines as $engine) { - if ($engine->supports($template)) { - return $engine; - } - } - - return false; - } - - private function getEngine($template) - { - if (!$engine = $this->resolveEngine($template)) { - throw new \InvalidArgumentException(sprintf('No suitable engine found for template %s.', $template)); - } - - return $engine; - } -} diff --git a/lucid/template/src/DisplayInterface.php b/lucid/template/src/DisplayInterface.php deleted file mode 100644 index f4dd17a..0000000 --- a/lucid/template/src/DisplayInterface.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface DisplayEngineInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface DisplayInterface -{ - /** - * Should directly output the contents of a rendered template. - * - * @param mixed $template - * @param array $parameters - * - * @return void - */ - public function display($template, array $parameters = []); -} diff --git a/lucid/template/src/Engine.php b/lucid/template/src/Engine.php deleted file mode 100644 index 004e9f8..0000000 --- a/lucid/template/src/Engine.php +++ /dev/null @@ -1,528 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -use SplStack; -use Exception; -use ErrorException; -use RuntimeException; -use Lucid\Template\Loader\LoaderInterface; -use Lucid\Template\Resource\FileResource; -use Lucid\Template\Resource\StringResource; -use Lucid\Template\Resource\ResourceInterface; -use Lucid\Template\IdentityParserInterface as Parser; -use Lucid\Template\Exception\LoaderException; -use Lucid\Template\Exception\RenderException; -use Lucid\Template\Exception\TemplateException; -use Lucid\Template\Extension\FunctionInterface; -use Lucid\Template\Extension\ExtensionInterface; - -/** - * @class Engine - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class Engine extends AbstractPhpEngine implements ViewAwareInterface -{ - /** @var string */ - const SUPPORT_TYPE = 'php'; - - /** @var string */ - protected $encoding; - - /** @var array */ - protected $functions; - - /** @var array */ - protected $globals; - - /** @var array */ - public $sections; - - /** @var array */ - protected $parents; - - /** @var array */ - protected $proxy; - - /** @var \SplStack */ - protected $stack; - - /** @var callable */ - protected $errHandler; - - /** @var \Closure */ - protected $errFunc; - - /** - * Constructor. - * - * @param LoaderInterface $loader - * @param TemplateIdentityInterface $identity - * @param array $helpers - */ - public function __construct(LoaderInterface $loader, Parser $parser = null, $enc = 'UTF-8') - { - $this->globals = []; - $this->sections = []; - $this->functions = []; - $this->setEncoding($enc); - $this->stack = new SplStack; - - parent::__construct($loader, $parser); - $this->addType(self::SUPPORT_TYPE); - } - - /** - * Sets the template encoding, - * - * @param string $enc the encoding type, e.g. UTF-8 - * - * @return void - */ - public function setEncoding($enc) - { - $this->encoding = $enc; - } - - /** - * Sets a set of "global" data - * - * Global variables are accessible by all templates during the rendering - * cycle. - * - * @param array $globals a assocoiative array of vaiable key/value pairs - * - * @return void - */ - public function setGlobals(array $globals) - { - $this->globals = $globals; - } - - /** - * Adds a global key/value pair to the globale template variables. - * - * @param string $key the variable name - * @param mixed $parameter the variable value - * - * @return void - */ - public function addGlobal($key, $parameter) - { - $this->globals[$key] = $parameter; - } - - /** - * Gets all the global data. - * - * @return array - */ - public function getGlobals() - { - return $this->globals; - } - - /** - * Registers a template extension. - * - * @param ExtensionInterface $extension - * - * @return void - */ - public function registerExtension(ExtensionInterface $extension) - { - $extension->setEngine($this); - - array_map([$this, 'registerFunction'], $extension->functions()); - } - - /** - * Registers a function on the template engine. - * - * @param string $alias - * @param callable $callable - * - * @return void - */ - public function registerFunction(FunctionInterface $fn) - { - $this->functions[$fn->getName()] = $fn; - } - - - /** - * Removes a template extension. - * - * @param ExtensionInterface $extension - * - * @return void - */ - public function removeExtension(ExtensionInterface $extension) - { - foreach ($extension->functions() as $func) { - unset($this->functions[$func->getName()]); - } - } - - /** - * {@inheritdoc} - * - * @throws RenderException - */ - public function render($template, array $parameters = []) - { - $resource = $this->loadTemplate($template); - - $this->stack->push([ - $resource, - $this->mergeShared($this->getParameters($template, $parameters)) - ]); - - $hash = $resource->getHash(); - - if (isset($this->parents[$hash])) { - throw new RenderException(sprintf('Circular reference in %s.', $template)); - } - - unset($this->parents[$hash]); - - $this->startErrorHandling(); - - $content = $this->doRender(); - - if (isset($this->parents[$hash])) { - $content = $this->render($this->parents[$hash], $parameters); - } - - $this->stopErrorHandling(); - - $this->stack->pop(); - - return $content; - } - - /** - * {@inheritdoc} - */ - public function display($template, array $parameters = []) - { - echo $this->render($template, $parameters); - } - - /** - * Inserts a template at the given position. - * - * @param mixed $template - * @param array $replacements template parameters to be merged with existing ones. - * - * @return string the rendered template as string - */ - public function insert($template, array $replacements = []) - { - list($resource, $params) = $this->getCurrent(); - - return $this->render($template, array_merge($params, $replacements)); - } - - /** - * Extends a given template. - * - * @param mixed $template - * - * @return void - */ - public function extend($template) - { - list($resource, ) = $this->getCurrent(); - - //if (isset($this->parents[$hash = $resource->getHash()])) { - //throw new RenderException('Circular reference.'); - //} - - $this->parents[$resource->getHash()] = $template; - } - - /** - * Starts a template section at the current postion. - * - * @param string $name - * - * @return void - */ - public function section($name) - { - if (isset($this->sections[$name])) { - $section = $this->sections[$name]; - unset($this->sections[$name]); - } else { - $section = new Section($name); - } - - $this->sections[$name] = $section; - - $section->start(); - } - - /** - * Ends a previuosly started section. - * - * @return void - */ - public function endsection() - { - $section = $this->getLastSection(); - $section->stop(); - - if ($this->hasParent()) { - return; - } - - $content = $section->getContent(0); - $section->reset(); - - return $content; - } - - /** - * execute - * - * @return void - */ - public function func(...$args) - { - $fns = explode('|', array_shift($args)); - $res = null; - - foreach (array_reverse($fns) as $fnc) { - if (!isset($this->functions[$fnc])) { - throw new RuntimeException(sprintf('Template function "%s" does not exist.', $fnc)); - } - - $res = $this->functions[$fnc]->call($args); - - if (!$this->functions[$fnc]->getOption('is_safe_html')) { - $res = $this->escape($res); - } - - $args = [$res]; - } - - return $res; - } - - /** - * Escape a string - * - * @param mixed $string - * - * @return void - */ - public function escape($string) - { - return htmlspecialchars($string, ENT_COMPAT, $this->getEncoding()); - } - - /** - * hasParent - * - * @return boolean - */ - protected function hasParent() - { - list($resource, ) = $this->getCurrent(); - - return isset($this->parents[$resource->getHash()]); - } - - /** - * getParameters - * - * @param mixed $template - * @param array $parameters - * - * @return array - */ - protected function getParameters($template, array $parameters) - { - if (null !== ($view = $this->getManager())) { - $view->notifyListeners($name = $this->getIdentity()->identify($template)->getName()); - - if ($data = $view->flushData($name)) { - $parameters = $data->all($parameters); - } - } - - return $parameters; - } - - /** - * getCurrent - * - * @return array - */ - protected function getCurrent() - { - return $this->stack->top(); - } - - /** - * getLastSection - * - * @return Section - */ - protected function getLastSection() - { - if (0 === count($this->sections)) { - throw new RenderException('Cannot end a section. You must start a section first.'); - } - - $keys = array_keys($this->sections); - $key = array_pop($keys); - - return $this->sections[$key]; - } - - /** - * doRender - * - * - * @return void - */ - protected function doRender() - { - list($resource, $parameters) = $this->getCurrent(); - ob_start(); - try { - if ($resource instanceof FileResource) { - $this->displayFile($resource, $parameters); - } elseif ($resource instanceof StringResource) { - $this->displayString($resource, $parameters); - } - } catch (Exception $e) { - ob_end_clean(); - - if ($e instanceof TemplateException) { - throw $e; - } - - throw new RenderException($e->getMessage(), $e, $e->getCode()); - } - - return ob_get_clean(); - } - - /** - * displayFile - * - * @param FileResource $resource - * @param array $parameters - * - * @return void - */ - protected function displayFile(FileResource $resource, array $parameters) - { - extract($parameters, EXTR_SKIP); - include $resource->getResource(); - } - - /** - * displayString - * - * @param StringResource $resource - * @param array $parameters - * - * @return void - */ - protected function displayString(StringResource $resource, array $parameters) - { - extract($parameters, EXTR_SKIP); - eval('; ?>' . $resource->getContents() . 'proxy ? $this->proxy = new RenderEngineDecorator($this) : $this->proxy; - } - - /** - * getEncoding - * - * @return void - */ - protected function getEncoding() - { - return $this->encoding ?: 'UTF-8'; - } - - /** - * mergeShared - * - * @param array $params - * - * @return array - */ - protected function mergeShared(array $params) - { - $proxy = $this->getProxy(); - - return array_merge($this->globals, $params, ['view' => $proxy, 'func' => [$this, 'func']]); - } - - /** - * Starts error handling. - * - * @return void - */ - protected function startErrorHandling() - { - $this->errHandler = set_error_handler($this->getErrFunc()); - } - - /** - * Get the error handler. - * - * @return \Closure - */ - protected function getErrFunc() - { - if (null === $this->errFunc) { - $this->errFunc = function ($errno, $errstr, $errfile, $errline) { - $this->stopErrorHandling(); - throw new ErrorException($errstr, 0, $errno, $errfile, $errline); - }; - } - - return $this->errFunc; - } - - /** - * Stops the errorhandler. - * - * @return void - */ - protected function stopErrorHandling() - { - if (null !== $this->errHandler) { - restore_error_handler(); - } - - $this->errHandler = null; - } -} diff --git a/lucid/template/src/EngineInterface.php b/lucid/template/src/EngineInterface.php deleted file mode 100644 index 6efadfe..0000000 --- a/lucid/template/src/EngineInterface.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface EngineInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface EngineInterface extends RenderInterface -{ - /** - * supports - * - * @param string $type - * - * @return boolean - */ - public function supports($template); - - /** - * exists - * - * @param mixed $template - * - * @return void - */ - public function exists($template); -} diff --git a/lucid/template/src/Exception/LoaderException.php b/lucid/template/src/Exception/LoaderException.php deleted file mode 100644 index d173a2e..0000000 --- a/lucid/template/src/Exception/LoaderException.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Exception; - -use Lucid\Template\IdentityInterface; - -/** - * @class LoaderException - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class LoaderException extends \InvalidArgumentException implements TemplateException -{ - /** - * Constructor. - * - * @param mixed $message - * @param \Exception $prevException - * @param int $code - */ - public function __construct($message, \Exception $prevException = null, $code = 0) - { - parent::__construct($message, $code, $prevException); - } - - public static function templateNotFound(IdentityInterface $template) - { - return new self(sprintf('Template "%s" not found.', $template->getName())); - } -} diff --git a/lucid/template/src/Exception/RenderException.php b/lucid/template/src/Exception/RenderException.php deleted file mode 100644 index 280b677..0000000 --- a/lucid/template/src/Exception/RenderException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Exception; - -/** - * @class RenderException - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class RenderException extends \RuntimeException implements TemplateException -{ - /** - * Constructor. - * - * @param mixed $message - * @param \Exception $prevException - * @param int $code - */ - public function __construct($message, \Exception $prevException = null, $code = 0) - { - parent::__construct($message, $code, $prevException); - } - - public static function invalidParameter($param) - { - return new self(sprintf('Invalid parameter "%s".', $param)); - } -} diff --git a/lucid/template/src/Exception/TemplateException.php b/lucid/template/src/Exception/TemplateException.php deleted file mode 100644 index 3971245..0000000 --- a/lucid/template/src/Exception/TemplateException.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Exception; - -/** - * @interface TemplateException - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface TemplateException -{ -} diff --git a/lucid/template/src/Extension/AbstractExtension.php b/lucid/template/src/Extension/AbstractExtension.php deleted file mode 100644 index 4d27918..0000000 --- a/lucid/template/src/Extension/AbstractExtension.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -use Lucid\Template\EngineInterface; - -/** - * @class AbstractExtension - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -abstract class AbstractExtension implements ExtensionInterface -{ - private $engine; - - /** - * {@inheritdoc} - */ - public function setEngine(EngineInterface $engine) - { - $this->engine = $engine; - } - - /** - * {@inheritdoc} - */ - public function getEngine() - { - return $this->engine; - } -} diff --git a/lucid/template/src/Extension/ExtensionInterface.php b/lucid/template/src/Extension/ExtensionInterface.php deleted file mode 100644 index ce7a12c..0000000 --- a/lucid/template/src/Extension/ExtensionInterface.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -use Lucid\Template\EngineInterface; - -/** - * @class ExtensionInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface ExtensionInterface -{ - /** - * Register functions. - * - * @param array $functions - * - * @return array - */ - public function functions(); - - /** - * Sets the template engine. - * - * @param EngineInterface $engine - * - * @return void - */ - public function setEngine(EngineInterface $engine); - - /** - * Get the template engine. - * - * @return EngineInterface - */ - public function getEngine(); -} diff --git a/lucid/template/src/Extension/FunctionInterface.php b/lucid/template/src/Extension/FunctionInterface.php deleted file mode 100644 index 3154b81..0000000 --- a/lucid/template/src/Extension/FunctionInterface.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -/** - * @interface FunctionInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface FunctionInterface -{ - - /** - * Calls the callable with given arguments - * - * @param array $arguments - * - * @return mixed - */ - public function call(array $arguments = []); - - /** - * Get the funtion name. - * - * @return string - */ - public function getName(); - - /** - * Get the function callable. - * - * @return callable - */ - public function getCallable(); - - /** - * Get the options. - * - * @return array - */ - public function getOptions(); - - /** - * getOption - * - * @return void - */ - public function getOption($option); - - /** - * Returns the function alias. - * - * @return void - */ - public function __toString(); - - /** - * Calls the callable. - * - * @return mixed - */ - public function __invoke(); -} diff --git a/lucid/template/src/Extension/PhpEngineExtension.php b/lucid/template/src/Extension/PhpEngineExtension.php deleted file mode 100644 index b199ae7..0000000 --- a/lucid/template/src/Extension/PhpEngineExtension.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -use Lucid\Template\EngineInterface; -use Lucid\Template\PhpRenderInterface; - -/** - * @class PhpEngineExtension - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class PhpEngineExtension extends AbstractExtension -{ - public function functions() - { - $engine = $this->getEngine(); - return [ - new TemplateFunction('section', [$engine, 'section']), - new TemplateFunction('endsection', [$engine, 'endsection']), - new TemplateFunction('insert', [$engine, 'insert']), - new TemplateFunction('extend', [$engine, 'extend']), - new TemplateFunction('escape', [$engine, 'escape']), - new TemplateFunction('func', [$engine, 'func']), - ]; - } - - /** - * {@inheritdoc} - */ - public function setEngine(EngineInterface $engine) - { - if (!$engine instanceof PhpRenderInterface) { - throw new \InvalidArgumentException; - } - - parent::setEngine($engine); - } -} diff --git a/lucid/template/src/Extension/RoutingExtension.php b/lucid/template/src/Extension/RoutingExtension.php deleted file mode 100644 index 576d173..0000000 --- a/lucid/template/src/Extension/RoutingExtension.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -/** - * @class RoutingExtension - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class RoutingExtension extends AbstractExtension -{ - private $router; - - public function __construct(RouterInterface $router) - { - $this->router = $router; - } - - /** - * {@inheritdoc} - */ - public function functions() - { - return [ - new TemplateFunction('call_route', [$this, 'callRoute']) - ]; - } - - /** - * callRoute - * - * @param mixed $name - * @param array $parameters - * @param array $options - * - * @return string - */ - public function callRoute($name, array $parameters = [], array $options = []) - { - try { - $content = $this->router->dispatchRoute($name, $parameters, $options); - } catch (\Exception $e) { - return sprintf('

%s

', $e->getMessage()); - } - - return is_string($content) ? $content : ''; - } -} diff --git a/lucid/template/src/Extension/TemplateFunction.php b/lucid/template/src/Extension/TemplateFunction.php deleted file mode 100644 index b89d3f0..0000000 --- a/lucid/template/src/Extension/TemplateFunction.php +++ /dev/null @@ -1,121 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Extension; - -/** - * @class TemplateFunction - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class TemplateFunction implements FunctionInterface -{ - /** @var string */ - private $name; - - /** @var callable */ - private $callable; - - /** @var array */ - private $options; - - /** - * Constructor. - * - * @param string $name - * @param callable $callable - * @param array $options - */ - public function __construct($name, callable $callable, array $options = []) - { - $this->name = (string)$name; - $this->callable = $callable; - $this->setOptions($options); - } - - /** - * {@inheritdoc} - */ - public function call(array $arguments = []) - { - return call_user_func_array($this->getCallable(), $arguments); - } - - /** - * getOption - * - * @param string $option - * - * @return mixed - */ - public function getOption($option) - { - if (isset($this->options[$option])) { - return $this->options[$option]; - } - } - - /** - * {@inheritdoc} - */ - public function getName() - { - return $this->name; - } - - /** - * {@inheritdoc} - */ - public function getCallable() - { - return $this->callable; - } - - /** - * {@inheritdoc} - */ - public function getOptions() - { - return $this->options; - } - - /** - * {@inheritdoc} - */ - public function __toString() - { - return $this->name; - } - - /** - * {@inheritdoc} - */ - public function __invoke(...$args) - { - return $this->call($args); - } - - /** - * setOptions - * - * @param array $options - * - * @return void - */ - private function setOptions(array $options) - { - $this->options = array_merge([ - 'is_safe_html' => false - ], $options); - } -} diff --git a/lucid/template/src/Identity.php b/lucid/template/src/Identity.php deleted file mode 100644 index d746546..0000000 --- a/lucid/template/src/Identity.php +++ /dev/null @@ -1,77 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -use Lucid\Template\Resource\FileResource; -use Lucid\Template\Resource\ResourceInterface; - -/** - * @class Identity - * @see IdentityInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class Identity implements IdentityInterface -{ - /** - * name - * - * @var string - */ - protected $name; - - /** - * type - * - * @var string - */ - protected $type; - - /** - * Constructor. - * - * @param string $name - * @param string $type - * @param string $path - */ - public function __construct($name, $type) - { - $this->name = $name; - $this->type = $type; - } - - /** - * {@inheritdoc} - */ - public function getName() - { - return $this->name; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return $this->type; - } - - /** - * {@inheritdoc} - */ - public function __toString() - { - return $this->name; - } -} diff --git a/lucid/template/src/IdentityInterface.php b/lucid/template/src/IdentityInterface.php deleted file mode 100644 index b23c9af..0000000 --- a/lucid/template/src/IdentityInterface.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -use Lucid\Template\Resource\ResourceInterface; - -/** - * @class TemplateInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface IdentityInterface -{ - /** - * Get the template name - * - * @return string - */ - public function getName(); - - /** - * Get the tempalte type - * - * @return string - */ - public function getType(); - - /** - * __toString - * - * @return string - */ - public function __toString(); -} diff --git a/lucid/template/src/IdentityParser.php b/lucid/template/src/IdentityParser.php deleted file mode 100644 index a1f062e..0000000 --- a/lucid/template/src/IdentityParser.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @class IdentityParser - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class IdentityParser implements IdentityParserInterface -{ - /** - * pool - * - * @var array - */ - private $pool = []; - - /** - * {@inheritdoc} - */ - public function identify($template) - { - if ($template instanceof IdentityInterface) { - return $template; - } - - $name = (string)$template; - - if (!isset($this->pool[$name])) { - $type = null; - - if (false !== ($pos = strrpos($name, '.'))) { - $type = substr($name, 1 + $pos); - } - - $this->pool[$name] = new Identity($name, $type); - } - - return $this->pool[$name]; - } -} diff --git a/lucid/template/src/IdentityParserInterface.php b/lucid/template/src/IdentityParserInterface.php deleted file mode 100644 index f2247e1..0000000 --- a/lucid/template/src/IdentityParserInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface TemplateIdentityInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface IdentityParserInterface -{ - /** - * identify - * - * @param mixed $template - * - * @return TemplateInterface - */ - public function identify($template); -} diff --git a/lucid/template/src/Listener/ListenerInterface.php b/lucid/template/src/Listener/ListenerInterface.php deleted file mode 100644 index 1ab540b..0000000 --- a/lucid/template/src/Listener/ListenerInterface.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Listener; - -use Lucid\Template\Data\TemplateDataInterface; - -/** - * @interface ListenerInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface ListenerInterface -{ - /** - * onRender - * - * @param View $view - * - * @return void - */ - public function onRender(TemplateDataInterface $data); -} diff --git a/lucid/template/src/Loader/FilesystemLoader.php b/lucid/template/src/Loader/FilesystemLoader.php deleted file mode 100644 index 1624d4a..0000000 --- a/lucid/template/src/Loader/FilesystemLoader.php +++ /dev/null @@ -1,88 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Loader; - -use Lucid\Template\Template; -use Lucid\Template\IdentityInterface; -use Lucid\Template\Resource\FileResource; -use Lucid\Template\Exception\LoaderException; - -/** - * @class FilesystemLoader - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -final class FilesystemLoader implements LoaderInterface -{ - private $paths; - private $loaded; - - /** - * Constructor. - * - * @param string|array $paths include paths to look for templates. - */ - public function __construct($paths) - { - $this->paths = is_array($paths) ? $paths : explode(',', $paths); - $this->loaded = []; - } - - /** - * {@inheritdoc} - */ - public function load(IdentityInterface $template) - { - if (isset($this->loaded[$template->getName()])) { - return $this->loaded[$template->getName()]; - } - - if (!$realpath = $this->findTemplateInPaths($template->getName())) { - throw LoaderException::templateNotFound($template); - } - - return $this->loaded[$template->getName()] = new FileResource($realpath); - } - - /** - * {@inheritdoc} - */ - public function isValid(IdentityInterface $template, $now) - { - try { - return filemtime($this->load($template)->getResource()) < $now; - } catch (LoaderException $e) { - } - - return false; - } - - /** - * findTemplateInPaths - * - * @param string $template - * - * @return string|boolean - */ - private function findTemplateInPaths($template) - { - foreach ($this->paths as $path) { - if (file_exists($realpath = $path . DIRECTORY_SEPARATOR . ltrim($template, DIRECTORY_SEPARATOR))) { - return $realpath; - } - } - - return false; - } -} diff --git a/lucid/template/src/Loader/LoaderInterface.php b/lucid/template/src/Loader/LoaderInterface.php deleted file mode 100644 index 6580c8a..0000000 --- a/lucid/template/src/Loader/LoaderInterface.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Loader; - -use Lucid\Template\IdentityInterface; - -/** - * @interface LoaderInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface LoaderInterface -{ - /** - * Load a template. - * - * @param IdentityInterface $template - * - * @return Lucid\Template\Resource\ResourceInterface - */ - public function load(IdentityInterface $template); - - /** - * isValid - * - * @param IdentityInterface $template - * - * @return void - */ - public function isValid(IdentityInterface $template, $now); -} diff --git a/lucid/template/src/Loader/LoggerAwareLoader.php b/lucid/template/src/Loader/LoggerAwareLoader.php deleted file mode 100644 index f153bfc..0000000 --- a/lucid/template/src/Loader/LoggerAwareLoader.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Loader; - -use Psr\Log\LoggerInterface; -use Lucid\Template\IdentityInterface; - -/** - * @class LoggerAwareLoader - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -final class LoggerAwareLoader implements LoaderInterface -{ - /** - * logger - * - * @var LoggerInterface - */ - private $logger; - - /** - * loader - * - * @var LoaderInterface - */ - private $loader; - - /** - * Constructor. - * - * @param LoaderInterface $loader - * @param LoggerInterface $logger - */ - public function __construct(LoaderInterface $loader, LoggerInterface $logger) - { - $this->loader = $loader; - $this->logger = $logger; - } - - /** - * {@inheritdoc} - */ - public function load(IdentityInterface $template) - { - if (!$res = $this->loader->load($template)) { - $this->logger->error('Error loading template "%s".', [(string)$template]); - return; - } - - $this->logger->info('Loaded template "%s".', [(string)$template]); - - return $res; - } - - /** - * {@inheritdoc} - */ - public function isValid(IdentityInterface $template, $now) - { - return $this->loader->isValid($template, $now); - } -} diff --git a/lucid/template/src/PhpRenderInterface.php b/lucid/template/src/PhpRenderInterface.php deleted file mode 100644 index 433ff06..0000000 --- a/lucid/template/src/PhpRenderInterface.php +++ /dev/null @@ -1,73 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface PhpEngineInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface PhpRenderInterface -{ - /** - * extend - * - * @param mixed $template - * - * @return void - */ - public function extend($template); - - /** - * insert - * - * @param mixed $template - * @param array $replacement - * - * @return void - */ - public function insert($template, array $vars = []); - - /** - * escape - * - * @param mixed $string - * - * @return void - */ - public function escape($string); - - /** - * section - * - * @param mixed $template - * - * @return void - */ - public function section($template); - - /** - * endsection - * - * @return void - */ - public function endsection(); - - /** - * func - * - * @return void - */ - public function func(...$args); -} diff --git a/lucid/template/src/RenderEngineDecorator.php b/lucid/template/src/RenderEngineDecorator.php deleted file mode 100644 index 2c9dbff..0000000 --- a/lucid/template/src/RenderEngineDecorator.php +++ /dev/null @@ -1,77 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @class RenderEngineProxy - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class RenderEngineDecorator implements PhpRenderInterface -{ - private $engine; - - public function __construct(PhpRenderInterface $engine) - { - $this->engine = $engine; - } - - /** - * {@inheritdoc} - */ - public function extend($template, array $vars = []) - { - return $this->engine->extend($template, $vars); - } - - /** - * {@inheritdoc} - */ - public function section($template) - { - return $this->engine->section($template); - } - - /** - * {@inheritdoc} - */ - public function endsection() - { - return $this->engine->endsection(); - } - - /** - * {@inheritdoc} - */ - public function insert($template, array $replacements = []) - { - return $this->engine->insert($template, $replacements); - } - - /** - * {@inheritdoc} - */ - public function func(...$args) - { - return call_user_func_array([$this->engine, 'func'], $args); - } - - /** - * {@inheritdoc} - */ - public function escape($string) - { - return $this->engine->escape($string); - } -} diff --git a/lucid/template/src/RenderInterface.php b/lucid/template/src/RenderInterface.php deleted file mode 100644 index abd7983..0000000 --- a/lucid/template/src/RenderInterface.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface RenderInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface RenderInterface -{ - /** - * Renders a given template - * - * @param mixed $template the template - * @param array $parameters the parameters - * - * @return string - */ - public function render($template, array $parameters = []); -} diff --git a/lucid/template/src/Resource/AbstractResource.php b/lucid/template/src/Resource/AbstractResource.php deleted file mode 100644 index 8c3209b..0000000 --- a/lucid/template/src/Resource/AbstractResource.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Resource; - -/** - * @class AbstractResource - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -abstract class AbstractResource implements ResourceInterface -{ - /** - * hash - * - * @var string - */ - protected $hash; - - public function getHash() - { - return $this->hash ?: $this->hash = hash('sha256', serialize($this)); - } -} diff --git a/lucid/template/src/Resource/FileResource.php b/lucid/template/src/Resource/FileResource.php deleted file mode 100644 index 7dd6a1f..0000000 --- a/lucid/template/src/Resource/FileResource.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Resource; - -/** - * @class FileResource - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class FileResource extends AbstractResource -{ - /** - * resource - * - * @var string - */ - private $file; - - /** - * Constructor. - * - * @param string $file - */ - public function __construct($file) - { - $this->file = $file; - } - - /** - * {@inheritdoc} - */ - public function getResource() - { - return $this->file; - } - - /** - * {@inheritdoc} - */ - public function getContents() - { - return file_get_contents($this->file); - } -} diff --git a/lucid/template/src/Resource/ResourceInterface.php b/lucid/template/src/Resource/ResourceInterface.php deleted file mode 100644 index 8570265..0000000 --- a/lucid/template/src/Resource/ResourceInterface.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Resource; - -/** - * @class ResourceInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface ResourceInterface -{ - public function getResource(); - - public function getContents(); - - /** - * getHash - * - * @return void - */ - public function getHash(); -} diff --git a/lucid/template/src/Resource/StringResource.php b/lucid/template/src/Resource/StringResource.php deleted file mode 100644 index 201a32a..0000000 --- a/lucid/template/src/Resource/StringResource.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Resource; - -/** - * @class StringResource - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class StringResource extends AbstractResource -{ - private $content; - - /** - * Constructor. - * - * @param string $string - */ - public function __construct($string) - { - $this->content = $string; - } - - /** - * {@inheritdoc} - */ - public function getResource() - { - return null; - } - - /** - * {@inheritdoc} - */ - public function getContents() - { - return $this->content; - } -} diff --git a/lucid/template/src/Section.php b/lucid/template/src/Section.php deleted file mode 100644 index 4f2bb2b..0000000 --- a/lucid/template/src/Section.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @class Section - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class Section -{ - /** - * Interal content buffer. - * - * @var array array of strings. - */ - private $cbuff = []; - - /** - * Start section buffer. - * - * @return void - */ - public function start() - { - ob_start(); - ob_implicit_flush(0); - } - - /** - * Stop section buffer. - * - * @return void - */ - public function stop() - { - $this->cbuff[] = ob_get_clean(); - } - - /** - * Reset the contents buffer; - * - * @return void - */ - public function reset() - { - $this->cbuff = []; - } - - /** - * Get the contents. - * - * @param int|null $index - * @param boolean $raw - * - * @throws \OutOfBoundsException if the given indenx is invalid. - * @return string|array - */ - public function getContent($index = null, $raw = false) - { - if (null === $index) { - return (bool)$raw ? $this->cbuff : join('', $this->cbuff); - } - - if (isset($this->cbuff[(int)$index])) { - return $this->cbuff[(int)$index]; - } - - throw new \OutOfBoundsException(sprintf('No content at buffer %d or empty buffer.', (int)$index)); - } -} diff --git a/lucid/template/src/Traits/ViewAwareTrait.php b/lucid/template/src/Traits/ViewAwareTrait.php deleted file mode 100644 index 237b192..0000000 --- a/lucid/template/src/Traits/ViewAwareTrait.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Traits; - -use Lucid\Template\ViewManagerInterface; - -/** - * @trait ViewAwareTrait - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -trait ViewAwareTrait -{ - private $viewManager; - - /** - * setView - * - * @param ViewManagerInterface $view - * - * @return void - */ - public function setManager(ViewManagerInterface $view) - { - $this->viewManager = $view; - } - - /** - * setView - * - * @param ViewManagerInterface $view - * - * @return void - */ - public function getManager() - { - return $this->viewManager; - } -} diff --git a/lucid/template/src/View.php b/lucid/template/src/View.php deleted file mode 100644 index 3339633..0000000 --- a/lucid/template/src/View.php +++ /dev/null @@ -1,238 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -use Lucid\Template\Listener\ListenerInterface; -use Lucid\Template\Data\TemplateDataInterface; -use Lucid\Template\Data\Data; - -/** - * @class View - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class View implements ViewManagerInterface -{ - /** - * Render engine - * - * @var EngineInterface - */ - private $engine; - - /** - * Render listeners - * - * @var ListenerInterface[] - */ - private $listeners; - - /** - * Global parameters - * - * @var array - */ - private $parameters; - - /** - * Temporary data. - * - * @var array - */ - private $data; - - /** - * Constructor. - * - * @param EngineInterface $engine - * @param array $listeners - */ - public function __construct(EngineInterface $engine, array $listeners = []) - { - $this->engine = $engine; - $this->data = []; - $this->parameters = []; - - $this->setListeners($listeners); - } - - /** - * Set render listeners. - * - * @param array $listeners - * - * @return void - */ - public function setListeners(array $listeners) - { - $this->listeners = []; - - foreach ($listeners as $name => $listener) { - $this->addListener($name, $listener); - } - } - - /** - * Add a render listener. - * - * @param string $name - * @param ListenerInterface $listener - * - * @return void - */ - public function addListener($name, ListenerInterface $listener) - { - $this->listeners[(string)$name] = $listener; - } - - /** - * {@inheritdoc} - */ - public function flushData($name) - { - if (!isset($this->data[$name])) { - return false; - } - - $data = $this->data[$name]; - - unset($this->data[$name]); - - return $data; - } - - /** - * Set global parameters. - * - * @param array $parameters - * - * @return void - */ - public function setGlobals(array $parameters) - { - $this->parameters = $parameters; - } - - /** - * Add global parameters. - * - * @param array $parameters - * - * @return void - */ - public function addGlobals(array $parameters) - { - $this->parameters = array_merge((array)$this->parameters, $parameters); - } - - /** - * Add a global parameter. - * - * @param string $key - * @param mixed $parameter - * - * @return void - */ - public function addGlobal($key, $value) - { - $this->parameters[$key] = $value; - } - - /** - * {@inheritdoc} - */ - public function notifyListeners($name) - { - if (!isset($this->listeners[$name])) { - return; - } - - $this->listeners[$name]->onRender($this->data[$name] = new Data($this)); - } - - /** - * {@inheritdoc} - */ - public function render($template, array $parameters = []) - { - return $this->getEngineForTemplate($template)->render($template, $this->getParameters($parameters)); - } - - /** - * {@inheritdoc} - */ - public function display($template, array $parameters = []) - { - if (($engine = $this->getEngineForTemplate($template)) instanceof DisplayInterface) { - $engine->display($template, $this->getParameters($parameters)); - } else { - echo $engine->render($template, $this->getParameters($parameters)); - } - } - - /** - * {@inheritdoc} - */ - public function supports($template) - { - return $this->engine->supports($template); - } - - /** - * {@inheritdoc} - * - * @throws \InvalidArgumentException if no engine is found for the - * given template. - */ - public function getEngineForTemplate($template) - { - if ($this->engine instanceof DelegatingEngine) { - return $this->prepareEngine($this->engine->resolveEngine($template)); - } - - if ($this->engine->supports($template)) { - return $this->prepareEngine($this->engine); - } - - throw new \InvalidArgumentException(sprintf('No engine found for template "%s".', (string)$template)); - } - - /** - * getParameters - * - * @param array $parameters - * - * @return void - */ - protected function getParameters(array $parameters) - { - return empty($parameters) ? $this->parameters : array_merge($this->parameters, $parameters); - } - - /** - * prepareEngine - * - * @param EngineInterface $engine - * - * @return EngineInterface - */ - protected function prepareEngine(EngineInterface $engine) - { - if ($engine instanceof ViewAwareInterface && $this !== $engine->getManager()) { - $engine->setManager($this); - } - - return $engine; - } -} diff --git a/lucid/template/src/ViewAwareInterface.php b/lucid/template/src/ViewAwareInterface.php deleted file mode 100644 index 0c9dfa9..0000000 --- a/lucid/template/src/ViewAwareInterface.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface ViewAwareInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface ViewAwareInterface -{ - /** - * setManager - * - * @param ViewManagerInterface $view - * - * @return void - */ - public function setManager(ViewManagerInterface $view); - - /** - * getManager - * - * @return Lucid\Template\ViewManagerInterface - */ - public function getManager(); -} diff --git a/lucid/template/src/ViewManagerInterface.php b/lucid/template/src/ViewManagerInterface.php deleted file mode 100644 index 67c7b20..0000000 --- a/lucid/template/src/ViewManagerInterface.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template; - -/** - * @interface ViewManagerInterface - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface ViewManagerInterface extends RenderInterface, DisplayInterface -{ - /** - * supports - * - * @param mixed $template - * - * @return boolean - */ - public function supports($template); - - /** - * getEngineForTemplate - * - * @param mixed $template - * - * @return EngineInteface|null - */ - public function getEngineForTemplate($template); - - /** - * notifyListeners - * - * @param string $name - * - * @return void - */ - public function notifyListeners($name); - - /** - * Flushes the data added by a listener. - * - * @param string $name - * - * @return \Lucid\Template\Data\TemplateDataInterface - */ - public function flushData($name); -} diff --git a/lucid/template/src/composer.json b/lucid/template/src/composer.json deleted file mode 100644 index 08428a1..0000000 --- a/lucid/template/src/composer.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "lucid/template", - "description": "Templating library for native php templates", - "license": "MIT", - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require-dev": { - "phpunit/phpunit": "4.4.*", - "satooshi/php-coveralls": "dev-master" - }, - "autoload": { - "psr-4": { - "Lucid\\Module\\Template\\":"" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/template/tests/Data/DataTest.php b/lucid/template/tests/Data/DataTest.php deleted file mode 100644 index 0c70d1b..0000000 --- a/lucid/template/tests/Data/DataTest.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Data; - -use Lucid\Template\Data\Data; - -/** - * @class DataTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class DataTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\Data\TemplateDataInterface', new Data); - } - - /** @test */ - public function dataShouldBeSettable() - { - $data = new Data; - $data->set(['foo' => 'bar']); - - $this->assertSame('bar', $data->get('foo')); - } - - /** @test */ - public function itShouldMergeData() - { - $data = new Data; - $data->set(['foo' => 'bar']); - - $this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $data->all(['bar' => 'baz'])); - } - - /** @test */ - public function itShouldNotMergeDataIfReplaced() - { - $data = new Data; - $data->replace(['foo' => 'bar']); - - $this->assertEquals(['foo' => 'bar'], $data->all(['bar' => 'baz'])); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function keysMustNotBeNumeric() - { - $data = new Data; - $data->add(0, 'bar'); - } -} diff --git a/lucid/template/tests/DelegatingEngineTest.php b/lucid/template/tests/DelegatingEngineTest.php deleted file mode 100644 index f6b1d50..0000000 --- a/lucid/template/tests/DelegatingEngineTest.php +++ /dev/null @@ -1,142 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\DelegatingEngine; - -/** - * @class DelegatingEngineTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class DelegatingEngineTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $engine = new DelegatingEngine(); - - $this->assertInstanceof('Lucid\Template\EngineInterface', $engine); - $this->assertInstanceof('Lucid\Template\DisplayInterface', $engine); - } - - /** @test */ - public function itShouldResolveEngine() - { - $engines = [ - $a = $this->mockEngine(), - $b = $this->mockEngine(), - ]; - - $a->method('supports')->with('template')->willReturn(false); - $b->method('supports')->with('template')->willReturn(true); - - $engine = new DelegatingEngine($engines); - - $this->assertTrue($engine->supports('template')); - $this->assertTrue($b === $engine->resolveEngine('template')); - } - - /** @test */ - public function itShouldSuportTemplateTypes() - { - $engines = [ - $a = $this->mockEngine(), - ]; - - $map = [ - ['template.php', true], - ['template.html', false] - ]; - - $a->method('supports')->will($this->returnValueMap($map)); - $a->method('exists')->will($this->returnValueMap($map)); - - $engine = new DelegatingEngine($engines); - - $this->assertTrue($engine->supports('template.php')); - $this->assertFalse($engine->supports('template.html')); - - $this->assertTrue($engine->exists('template.php')); - $this->assertFalse($engine->exists('template.html')); - } - - /** @test */ - public function itShouldDelegateRenderToEngine() - { - $a = $this->mockEngine(); - $a->method('supports')->with('template')->willReturn(true); - $a->expects($this->once())->method('render'); - - $engine = new DelegatingEngine([$a]); - - $engine->render('template'); - } - - /** @test */ - public function itShouldDelegateDisplayToEngine() - { - $a = $this->mockEngine(); - $a->method('supports')->with('template')->willReturn(true); - $a->expects($this->once())->method('render'); - - $engine = new DelegatingEngine([$a]); - - $engine->display('template'); - - $a = $this->getMockBuilder('Lucid\Template\Engine')->disableOriginalConstructor()->getMock(); - $a->method('supports')->with('template')->willReturn(true); - $a->expects($this->once())->method('display'); - - $engine = new DelegatingEngine([$a]); - - $engine->display('template'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowExceptionOnRenderIfNoEngineIsFoundForAGivenTamplate() - { - $a = $this->mockEngine(); - $a->method('supports')->with('template')->willReturn(false); - - $engine = new DelegatingEngine([$a]); - $engine->render('template'); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowExceptionOnDisplayIfNoEngineIsFoundForAGivenTamplate() - { - $a = $this->mockEngine(); - $a->method('supports')->with('template')->willReturn(false); - - $engine = new DelegatingEngine([$a]); - $engine->display('template'); - } - - protected function mockEngine() - { - return $this->getMock('Lucid\Template\EngineInterface'); - } - - protected function newEngine(array $engines = []) - { - return new DelegatingEngine($engines); - } -} diff --git a/lucid/template/tests/EngineTest.php b/lucid/template/tests/EngineTest.php deleted file mode 100644 index 9445630..0000000 --- a/lucid/template/tests/EngineTest.php +++ /dev/null @@ -1,270 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\Engine; -use Lucid\Template\Data\Data; -use Lucid\Template\Loader\FilesystemLoader; -use Lucid\Template\Resource\StringResource; - -/** - * @class EngineTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class EngineTest extends \PHPUnit_Framework_TestCase -{ - protected $loader; - - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\EngineInterface', new Engine($this->mockLoader())); - } - - /** @test */ - public function itShouldTellIfTemplateExists() - { - $engine = $this->newEngine(); - - $map = [ - ['template.php', true], - ['template.html', false], - ]; - - $this->loader->expects($this->any()) - ->method('supports')->will($this->returnValueMap($map)); - - $this->loader->expects($this->once()) - ->method('load')->with('template.php')->willReturn($this->mockResource()); - - $this->assertTrue($engine->exists('template.php')); - $this->assertFalse($engine->exists('template.html')); - } - - /** @test */ - public function renderShouldReturnString() - { - $engine = $this->newEngine(); - - $this->loader->method('load')->willReturn($this->mockResource()); - - $this->assertInternalType('string', $engine->render('template.php')); - } - - /** @test */ - public function itShouldSupportItsType() - { - $engine = $this->newEngine(); - - $this->assertTrue($engine->supports('template.php')); - $this->assertFalse($engine->supports('html')); - } - - /** @test */ - public function itShouldDisplayTemplate() - { - $engine = $this->newEngine(); - - $this->loader->method('load')->willReturn($this->mockResource()); - ob_start(); - $engine->display('template.php'); - $content = ob_get_contents(); - ob_end_clean(); - - $this->assertInternalType('string', $content); - } - - /** @test */ - public function itShouldRegisterGlobalData() - { - $engine = $this->newEngine(); - $engine->setGlobals(['foo' => 'bar']); - - $this->assertInternalType('array', $engine->getGlobals()); - $this->assertArrayHasKey('foo', $engine->getGlobals()); - $engine->addGlobal('bar', 'baz'); - $this->assertArrayHasKey('bar', $engine->getGlobals()); - } - - /** @test */ - public function itShouldHandleTemplateErrors() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - - try { - $engine->render('error.php'); - } catch (\Lucid\Template\Exception\RenderException $e) { - $this->assertSame('Undefined variable: dontexist', $e->getMessage()); - } - } - - /** @test */ - public function itShouldHandleCircularReferencesOnExtends() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - try { - $engine->render('partials/extend.1.php'); - } catch (\Lucid\Template\Exception\RenderException $e) { - $this->assertSame('Circular reference in partials/extend.1.php.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldThrowExceptionOnFaultySections() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - try { - $engine->render('index.1.php'); - } catch (\Lucid\Template\Exception\RenderException $e) { - $this->assertSame('Cannot end a section. You must start a section first.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldRegisterExtension() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - - $func = $this->getMock('Lucid\Template\Extension\FunctionInterface'); - $func->method('getName')->willReturn('my_func'); - $ext = $this->getMock('Lucid\Template\Extension\ExtensionInterface'); - $ext->method('functions')->willReturn([$func]); - - $func->expects($this->once())->method('call'); - - $engine->registerExtension($ext); - - $engine->render('func.0.php'); - } - - /** @test */ - public function itShouldUnregisterExtension() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - - $func = $this->getMock('Lucid\Template\Extension\FunctionInterface'); - $func->method('getName')->willReturn('my_func'); - $ext = $this->getMock('Lucid\Template\Extension\ExtensionInterface'); - $ext->method('functions')->willReturn([$func]); - - $func->expects($this->once())->method('call'); - - $engine->registerExtension($ext); - - $engine->render('func.0.php'); - - $engine->removeExtension($ext); - - try { - $engine->render('func.0.php'); - } catch (\RuntimeException $e) { - $this->assertTrue(true); - - return; - } - - $this->fail(); - } - - /** - * @test - * @expectedException \RuntimeException - */ - public function callingUnknowenFuncShouldThrowException() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - $engine->render('func.1.php'); - } - - /** @test */ - public function viewShouldBeSettable() - { - $engine = $this->newEngine(); - - $engine->setManager($view = $this->getMock('Lucid\Template\ViewManagerInterface')); - $this->assertTrue($view === $engine->getManager()); - } - - /** @test */ - public function itShouldRenderStrings() - { - $id = $this->getMockbuilder('Lucid\Template\IdentityInterface')->disableOriginalConstructor()->getMock(); - $id->method('__toString')->willReturnCallback(function () { - return 'my string'; - }); - $id->method('getType')->willReturn('string'); - - $idp = $this->getMockbuilder('Lucid\Template\IdentityParserInterface')->disableOriginalConstructor()->getMock(); - - $idp->method('identify')->willReturnCallback(function (...$args) use ($id) { - return $id; - }); - - $engine = new Engine($loader = $this->mockLoader(), $idp); - $engine->addType('string'); - - $loader->method('load')->willReturn(new StringResource('my string')); - - $engine->render('my string'); - } - - /** @test */ - public function itShouldNotifyListener() - { - $data = new Data; - $data->set(['foo' => 'bar']); - $engine = new Engine($loader = $this->mockLoader()); - $loader->method('load')->willReturn($this->mockResource()); - $engine->setManager($view = $this->mockViewManager()); - - $view->expects($this->once())->method('notifyListeners')->with('template.php'); - $view->expects($this->once())->method('flushData')->with('template.php')->willReturn($data); - - $engine->render('template.php'); - } - - protected function newEngine() - { - $engine = new Engine($this->loader = $this->mockLoader()); - - return $engine; - } - - protected function mockViewManager() - { - return $this->getMockbuilder('Lucid\Template\ViewManagerInterface')->disableOriginalConstructor()->getMock(); - } - - - protected function mockLoader() - { - return $this->getMockbuilder('Lucid\Template\Loader\LoaderInterface')->disableOriginalConstructor()->getMock(); - } - - protected function mockResource() - { - return $this->getMockbuilder('Lucid\Template\Resource\ResourceInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/template/tests/Extension/PhpEngineExtensionTest.php b/lucid/template/tests/Extension/PhpEngineExtensionTest.php deleted file mode 100644 index 8a6f686..0000000 --- a/lucid/template/tests/Extension/PhpEngineExtensionTest.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Extension; - -use Lucid\Template\Extension\PhpEngineExtension; - -/** - * @class PhpEngineExtensionTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class PhpEngineExtensionTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf('Lucid\Template\Extension\ExtensionInterface', new PhpEngineExtension); - } - - /** @test */ - public function itShouldReturnEngine() - { - $ext = new PhpEngineExtension; - $this->assertNull($ext->getEngine()); - - $ext->setEngine($engine = $this->mockEngine()); - - $this->assertTrue($engine === $ext->getEngine()); - } - - /** @test */ - public function itShouldThrowIfEngineIsNotSupported() - { - $ext = new PhpEngineExtension; - $engine = $this->getMockbuilder('Lucid\Template\EngineInterface') - ->disableOriginalConstructor() - ->getMock(); - - try { - $ext->setEngine($engine); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldExportPhpRenderInterfaceMethods() - { - $ext = new PhpEngineExtension; - $ext->setEngine($this->mockEngine()); - - $ref = new \ReflectionClass('Lucid\Template\PhpRenderInterface'); - $methods = array_map(function ($m) { - return $m->getName(); - }, $ref->getMethods()); - - $this->assertTrue(count($methods) === count($ext->functions())); - - foreach ($ext->functions() as $func) { - $this->assertTrue(in_array($func->getName(), $methods)); - } - } - - private function mockEngine() - { - return $this->getMockbuilder('Lucid\Template\Engine') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/template/tests/Fixures/view/content.php b/lucid/template/tests/Fixures/view/content.php deleted file mode 100644 index 6844f70..0000000 --- a/lucid/template/tests/Fixures/view/content.php +++ /dev/null @@ -1,5 +0,0 @@ -extend('master.php')?> - -section('content')?> - -
Content
diff --git a/lucid/template/tests/Fixures/view/error.php b/lucid/template/tests/Fixures/view/error.php deleted file mode 100644 index 200411d..0000000 --- a/lucid/template/tests/Fixures/view/error.php +++ /dev/null @@ -1 +0,0 @@ -
diff --git a/lucid/template/tests/Fixures/view/func.0.php b/lucid/template/tests/Fixures/view/func.0.php deleted file mode 100644 index 07646c4..0000000 --- a/lucid/template/tests/Fixures/view/func.0.php +++ /dev/null @@ -1,3 +0,0 @@ -
- -
diff --git a/lucid/template/tests/Fixures/view/func.1.php b/lucid/template/tests/Fixures/view/func.1.php deleted file mode 100644 index 568a479..0000000 --- a/lucid/template/tests/Fixures/view/func.1.php +++ /dev/null @@ -1,3 +0,0 @@ -
- -
diff --git a/lucid/template/tests/Fixures/view/include.0.php b/lucid/template/tests/Fixures/view/include.0.php deleted file mode 100644 index 3587f8a..0000000 --- a/lucid/template/tests/Fixures/view/include.0.php +++ /dev/null @@ -1,10 +0,0 @@ - - - - - title - - - insert("partials/include.0.php") ?> - - diff --git a/lucid/template/tests/Fixures/view/include.1.php b/lucid/template/tests/Fixures/view/include.1.php deleted file mode 100644 index 448e9d0..0000000 --- a/lucid/template/tests/Fixures/view/include.1.php +++ /dev/null @@ -1,10 +0,0 @@ - - - - - title - - - insert("partials/include.1.php", ["name" => "World"]) ?> - - diff --git a/lucid/template/tests/Fixures/view/index.0.php b/lucid/template/tests/Fixures/view/index.0.php deleted file mode 100644 index 31cc910..0000000 --- a/lucid/template/tests/Fixures/view/index.0.php +++ /dev/null @@ -1,11 +0,0 @@ - - - - - title - - - section('content') ?> - endsection() ?> - - diff --git a/lucid/template/tests/Fixures/view/index.1.php b/lucid/template/tests/Fixures/view/index.1.php deleted file mode 100644 index 97e746a..0000000 --- a/lucid/template/tests/Fixures/view/index.1.php +++ /dev/null @@ -1,10 +0,0 @@ - - - - - title - - - endsection() ?> - - diff --git a/lucid/template/tests/Fixures/view/index.2.php b/lucid/template/tests/Fixures/view/index.2.php deleted file mode 100644 index 1f0038c..0000000 --- a/lucid/template/tests/Fixures/view/index.2.php +++ /dev/null @@ -1,12 +0,0 @@ - - - - - title - - - section('content') ?> - - endsection() ?> - - diff --git a/lucid/template/tests/Fixures/view/index.php b/lucid/template/tests/Fixures/view/index.php deleted file mode 100644 index 1aaaab1..0000000 --- a/lucid/template/tests/Fixures/view/index.php +++ /dev/null @@ -1,9 +0,0 @@ - - - - - <?=$title?> - - - - diff --git a/lucid/template/tests/Fixures/view/partials/extend.0.php b/lucid/template/tests/Fixures/view/partials/extend.0.php deleted file mode 100644 index d313079..0000000 --- a/lucid/template/tests/Fixures/view/partials/extend.0.php +++ /dev/null @@ -1,5 +0,0 @@ -extend('index.0.php') ?> -section('content') ?> -
some content
-endsection() ?> - diff --git a/lucid/template/tests/Fixures/view/partials/extend.1.php b/lucid/template/tests/Fixures/view/partials/extend.1.php deleted file mode 100644 index f2e2431..0000000 --- a/lucid/template/tests/Fixures/view/partials/extend.1.php +++ /dev/null @@ -1,2 +0,0 @@ -extend('partials/extend.1.php') ?> - diff --git a/lucid/template/tests/Fixures/view/partials/extend.2.php b/lucid/template/tests/Fixures/view/partials/extend.2.php deleted file mode 100644 index edb3a8c..0000000 --- a/lucid/template/tests/Fixures/view/partials/extend.2.php +++ /dev/null @@ -1,4 +0,0 @@ -extend('index.2.php', ['default' => 'foo']) ?> -section('content') ?> -endsection() ?> - diff --git a/lucid/template/tests/Fixures/view/partials/footer.php b/lucid/template/tests/Fixures/view/partials/footer.php deleted file mode 100644 index e69de29..0000000 diff --git a/lucid/template/tests/Fixures/view/partials/header.php b/lucid/template/tests/Fixures/view/partials/header.php deleted file mode 100644 index 0e5a14b..0000000 --- a/lucid/template/tests/Fixures/view/partials/header.php +++ /dev/null @@ -1 +0,0 @@ -
header
diff --git a/lucid/template/tests/Fixures/view/partials/include.0.php b/lucid/template/tests/Fixures/view/partials/include.0.php deleted file mode 100644 index ad83115..0000000 --- a/lucid/template/tests/Fixures/view/partials/include.0.php +++ /dev/null @@ -1 +0,0 @@ -
diff --git a/lucid/template/tests/Fixures/view/partials/include.1.php b/lucid/template/tests/Fixures/view/partials/include.1.php deleted file mode 100644 index d03997a..0000000 --- a/lucid/template/tests/Fixures/view/partials/include.1.php +++ /dev/null @@ -1 +0,0 @@ -
Hello !
diff --git a/lucid/template/tests/IdentityParserTest.php b/lucid/template/tests/IdentityParserTest.php deleted file mode 100644 index 6cce7b9..0000000 --- a/lucid/template/tests/IdentityParserTest.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\IdentityParser; - -/** - * @class TemplateTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class IdentityParserTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\IdentityParserInterface', new IdentityParser); - } - - /** @test */ - public function itShouldSetAttributes() - { - $parser = new IdentityParser; - - $this->assertInstanceOf('Lucid\Template\IdentityInterface', $idt = $parser->identify('template.php')); - $this->assertSame('php', $idt->getType()); - $this->assertSame('template.php', $idt->getName()); - - $idt = $parser->identify('template'); - $this->assertNull($idt->getType()); - $this->assertSame('template', $idt->getName()); - } - - /** @test */ - public function itShouldReturnGivenIdentities() - { - $parser = new IdentityParser; - $mock = $this->getMock('Lucid\Template\IdentityInterface'); - - $this->assertTrue($mock === $parser->identify($mock)); - } -} diff --git a/lucid/template/tests/IdentityTest.php b/lucid/template/tests/IdentityTest.php deleted file mode 100644 index 1d21364..0000000 --- a/lucid/template/tests/IdentityTest.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\Identity; - -/** - * @class TemplateTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class IdentityTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\IdentityInterface', new Identity('name', 'type')); - } - - /** @test */ - public function itShouldGetAttributes() - { - $template = new Identity('template.php', 'php'); - - $this->assertSame('template.php', $template->getName()); - $this->assertSame('php', $template->getType()); - } -} diff --git a/lucid/template/tests/Listener/ListenerTest.php b/lucid/template/tests/Listener/ListenerTest.php deleted file mode 100644 index eb0c143..0000000 --- a/lucid/template/tests/Listener/ListenerTest.php +++ /dev/null @@ -1,33 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Listener; - -use Lucid\Template\Tests\Stubs\Listener; - -class ListenerTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf( - 'Lucid\Template\Listener\ListenerInterface', - new Listener - ); - } - - public function mockListener() - { - return $this->getMockbuilder('Lucid\Template\Listener\ListenerInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/template/tests/Loader/FilesystemLoaderTest.php b/lucid/template/tests/Loader/FilesystemLoaderTest.php deleted file mode 100644 index 84f130b..0000000 --- a/lucid/template/tests/Loader/FilesystemLoaderTest.php +++ /dev/null @@ -1,100 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Loader; - -use Lucid\Template\Loader\FilesystemLoader; - -/** - * @class FilesystemLoaderTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class FilesystemLoaderTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof( - 'Lucid\Template\Loader\LoaderInterface', - new FilesystemLoader(__DIR__.'/../Fixures/view') - ); - } - - /** @test */ - public function itShouldLoadTemplates() - { - $id = $this->mockIdentity(); - $id->expects($this->any())->method('getName')->willReturn('index.php'); - - $loader = new FilesystemLoader(__DIR__.'/../Fixures/view'); - - $loader->load($id); - } - - /** @test */ - public function itShouldCacheResolvedResources() - { - $id = $this->mockIdentity(); - $id->expects($this->any())->method('getName')->willReturn('index.php'); - - $loader = new FilesystemLoader(__DIR__.'/../Fixures/view'); - - $res = $loader->load($id); - - $this->assertSame($res, $loader->load($id)); - } - - /** @test */ - public function itShouldValidateTemplateTimestamps() - { - $idA = $this->mockIdentity(); - $idA->expects($this->any())->method('getName')->willReturn('index.php'); - $idB = $this->mockIdentity(); - $idB->expects($this->any())->method('getName')->willReturn('index'); - $loader = new FilesystemLoader(__DIR__.'/../Fixures/view'); - - $this->assertTrue($loader->isValid($idA, time())); - $this->assertFalse($loader->isValid($idA, 1000)); - - $this->assertFalse($loader->isValid($idB, time())); - } - - /** @test */ - public function itShouldResolveResourceIfNameIsAbsolutePath() - { - $id = $this->mockIdentity(); - $id->expects($this->any())->method('getName')->willReturn(__DIR__.'/../Fixures/view/index.php'); - - $loader = new FilesystemLoader('/'); - - $this->assertInstanceof('Lucid\Template\Resource\FileResource', $loader->load($id)); - } - - /** - * @test - * @expectedException \Lucid\Template\Exception\LoaderException - */ - public function itShouldThrowExceptionIfPathCannotBeResolved() - { - $id = $this->mockIdentity(); - $id->expects($this->any())->method('getName')->willReturn('index'); - $loader = new FilesystemLoader(__DIR__.'/../Fixures/view'); - $loader->load($id); - } - - protected function mockIdentity() - { - return $this->getMock('Lucid\Template\IdentityInterface'); - } -} diff --git a/lucid/template/tests/Loader/LoggerAwareLoaderTest.php b/lucid/template/tests/Loader/LoggerAwareLoaderTest.php deleted file mode 100644 index cf7ca7a..0000000 --- a/lucid/template/tests/Loader/LoggerAwareLoaderTest.php +++ /dev/null @@ -1,90 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Loader; - -use Lucid\Template\Loader\LoggerAwareLoader as Loader; - -/** - * @class LoggerAwareLoaderTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class LoggerAwareLoaderTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceOf( - 'Lucid\Template\Loader\LoaderInterface', - new Loader($this->mockLoader(), $this->mockLogger()) - ); - } - - /** @test */ - public function itShouldLogLoadingATemplate() - { - $loader = new Loader($ml = $this->mockLoader(), $logger = $this->mockLogger()); - - $id = $this->mockId(); - $ml->expects($this->once())->method('load')->with($id)->willReturn($this->mockResource()); - $logger->expects($this->once())->method('info'); - $loader->load($id); - } - - /** @test */ - public function itShouldLogErrorLoadingATemplate() - { - $loader = new Loader($ml = $this->mockLoader(), $logger = $this->mockLogger()); - - $id = $this->mockId(); - $ml->expects($this->once())->method('load')->with($id)->willReturn(null); - $logger->expects($this->once())->method('error'); - $loader->load($id); - } - - /** @test */ - public function itShouldPassEncapsulatingMethods() - { - $loader = new Loader($ml = $this->mockLoader(), $this->mockLogger()); - $ml->expects($this->once())->method('isValid')->with($id = $this->mockId(), $now = time()); - $loader->isValid($id, $now); - } - - private function mockResource() - { - return $this->getMockbuilder('Lucid\Template\Resource\ResourceInterface') - ->disableOriginalConstructor() - ->getMock(); - } - private function mockId() - { - return $this->getMockbuilder('Lucid\Template\IdentityInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - private function mockLoader() - { - return $this->getMockbuilder('Lucid\Template\Loader\LoaderInterface') - ->disableOriginalConstructor() - ->getMock(); - } - - private function mockLogger() - { - return $this->getMockbuilder('Psr\Log\LoggerInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/template/tests/RenderEngineDecoratorTest.php b/lucid/template/tests/RenderEngineDecoratorTest.php deleted file mode 100644 index bdcfb2b..0000000 --- a/lucid/template/tests/RenderEngineDecoratorTest.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\RenderEngineDecorator; - -/** - * @class RenderEngineDecoratorTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class RenderEngineDecoratorTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof( - 'Lucid\Template\PhpRenderInterface', - new RenderEngineDecorator($this->mockEngine()) - ); - } - - /** - * @test - * @dataProvider methodProvider - */ - public function itShouldDelegateFunc($method, array $args) - { - $rd = new RenderEngineDecorator($engine = $this->mockEngine()); - $called = false; - - $engine->method('func')->willReturn(null); - $engine->method($method)->will($this->returnCallback(function (...$arguments) use (&$called, $args, $method) { - if ($args === $arguments) { - $called = true; - } else { - $this->fail('Arguments missmatch.'); - } - })); - - call_user_func_array([$rd, $method], $args); - - $this->assertTrue($called); - } - - public function methodProvider() - { - return [ - ['extend', ['template', []]], - ['insert', ['template', []]], - ['escape', ['string']], - ['section', ['name']], - ['endsection', []], - ['func', ['args']], - ]; - } - - public function mockEngine() - { - return $this->getMockBuilder('Lucid\Template\PhpRenderInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/template/tests/RenderTest.php b/lucid/template/tests/RenderTest.php deleted file mode 100644 index 1412127..0000000 --- a/lucid/template/tests/RenderTest.php +++ /dev/null @@ -1,109 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\View; -use Lucid\Template\Engine; -use Lucid\Template\Loader\FilesystemLoader; - -/** - * @class RenderTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class RenderTest extends \PHPUnit_Framework_TestCase -{ - - /** @test */ - public function itShouldRenderData() - { - $engine = $this->newEngine(); - $expected = << - - - - Hello - - - - -EOF; - $this->assertXmlStringEqualsXmlString($expected, $engine->render('index.php', ['title' => 'Hello'])); - } - - /** @test */ - public function itShouldRenderInserts() - { - $engine = $this->newEngine(); - $expected = << - - - - title - - -
- - -EOF; - $this->assertXmlStringEqualsXmlString($expected, $engine->render('include.0.php')); - } - - /** @test */ - public function itShouldRenderInsertsWithData() - { - $engine = $this->newEngine(); - $expected = << - - - - title - - -
Hello World!
- - -EOF; - $this->assertXmlStringEqualsXmlString($expected, $engine->render('include.1.php')); - } - - /** @test */ - public function itIsShouldExtendTemplates() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view/')); - $expected = << - - - - title - - -
some content
- - -EOF; - $this->assertXmlStringEqualsXmlString($expected, $engine->render('partials/extend.0.php')); - } - - protected function newEngine() - { - $engine = new Engine(new FilesystemLoader(__DIR__.'/Fixures/view')); - - return $engine; - } -} diff --git a/lucid/template/tests/Resource/FileResourceTest.php b/lucid/template/tests/Resource/FileResourceTest.php deleted file mode 100644 index 1d1e38e..0000000 --- a/lucid/template/tests/Resource/FileResourceTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Resource; - -use Lucid\Template\Resource\FileResource; - -/** - * @class FileResourceTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class FileResourceTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\Resource\ResourceInterface', new FileResource('file')); - } - - /** @test */ - public function itShouldGetPath() - { - $res = new FileResource('file'); - $this->assertSame('file', $res->getResource()); - } - - /** @test */ - public function itShouldContents() - { - $res = new FileResource(__FILE__); - $this->assertStringEqualsFile(__FILE__, $res->getContents()); - } -} diff --git a/lucid/template/tests/Resource/StringResourceTest.php b/lucid/template/tests/Resource/StringResourceTest.php deleted file mode 100644 index 512434f..0000000 --- a/lucid/template/tests/Resource/StringResourceTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Resource; - -use Lucid\Template\Resource\StringResource; - -/** - * @class FileResourceTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class StringResourceTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Template\Resource\ResourceInterface', new StringResource('string')); - } - - /** @test */ - public function itShouldGetPath() - { - $res = new StringResource('file'); - $this->assertSame(null, $res->getResource()); - } - - /** @test */ - public function itShouldContents() - { - $res = new StringResource('string'); - $this->assertSame('string', $res->getContents()); - } -} diff --git a/lucid/template/tests/SectionTest.php b/lucid/template/tests/SectionTest.php deleted file mode 100644 index 41c33da..0000000 --- a/lucid/template/tests/SectionTest.php +++ /dev/null @@ -1,98 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\Section; - -/** - * @class SectionTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class SectionTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldCaptureOuput() - { - $section = new Section; - - $section->start(); - echo 'Foo'; - $section->stop(); - - $this->assertSame('Foo', $section->getContent()); - } - - /** @test */ - public function itShouldResetBuffer() - { - $section = new Section; - - $section->start(); - echo 'Foo'; - $section->stop(); - - $this->assertSame('Foo', $section->getContent()); - - $section->reset(); - - $this->assertSame('', $section->getContent()); - } - - /** - * @test - * @expectedException \OutOfBoundsException - */ - public function itShouldThrowExceptionIfBufferIndexIsOutOfRange() - { - $section = new Section; - - $section->start(); - echo 'Foo'; - $section->stop(); - - $section->getContent(1); - } - - /** @test */ - public function itShouldGetContentAsArray() - { - $section = new Section; - - $section->start(); - echo 'Foo'; - $section->stop(); - $section->start(); - echo 'Bar'; - $section->stop(); - - $this->assertEquals(['Foo', 'Bar'], $section->getContent(null, true)); - } - - /** @test */ - public function itShouldGetContentBufferAtGivenIndex() - { - $section = new Section; - - $section->start(); - echo 'Foo'; - $section->stop(); - $section->start(); - echo 'Bar'; - $section->stop(); - - $this->assertSame('Foo', $section->getContent(0)); - $this->assertSame('Bar', $section->getContent(1)); - } -} diff --git a/lucid/template/tests/Stubs/Displayable.php b/lucid/template/tests/Stubs/Displayable.php deleted file mode 100644 index cfb6084..0000000 --- a/lucid/template/tests/Stubs/Displayable.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Stubs; - -use Lucid\Template\RenderInterface; -use Lucid\Template\DisplayInterface; -use Lucid\Template\EngineInterface; - -/** - * @interface Renderer - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -interface Displayable extends EngineInterface, DisplayInterface -{ -} diff --git a/lucid/template/tests/Stubs/Listener.php b/lucid/template/tests/Stubs/Listener.php deleted file mode 100644 index fc7bdd7..0000000 --- a/lucid/template/tests/Stubs/Listener.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests\Stubs; - -use Lucid\Template\Listener\ListenerInterface; -use Lucid\Template\Data\TemplateDataInterface; - -/** - * @class Listener - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class Listener implements ListenerInterface -{ - private $test; - - public function __construct(callable $callback = null) - { - $this->test = $callback; - } - - public function onRender(TemplateDataInterface $data) - { - if (null !== $this->test) { - call_user_func_array($this->test, func_get_args()); - } - } -} diff --git a/lucid/template/tests/ViewTest.php b/lucid/template/tests/ViewTest.php deleted file mode 100644 index caadf7e..0000000 --- a/lucid/template/tests/ViewTest.php +++ /dev/null @@ -1,197 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Template\Tests; - -use Lucid\Template\View; - -/** - * @class ViewTest - * - * @package Lucid\Template - * @version $Id$ - * @author iwyg - */ -class ViewTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldPassGlobals() - { - $engine = $this->mockEngine(); - $view = $this->newView($engine); - - $view->setGlobals(['foo' => 'bar', 'baz' => 'bam']); - $view->addGlobals(['bar' => 'zzz']); - $view->addGlobal('fake', 'name'); - - $params = ['name' => 'Carl']; - - $engine->method('supports')->with('template')->willReturn(true); - $engine->expects($this->once())->method('render')->will($this->returnCallback(function ($t, $p) { - $this->assertTrue(isset($p['foo'])); - $this->assertTrue(isset($p['bar'])); - $this->assertTrue(isset($p['fake'])); - $this->assertTrue(isset($p['baz'])); - $this->assertTrue(isset($p['name'])); - })); - - $view->render('template', $params); - } - - /** @test */ - public function itShouldSupportTemplate() - { - $engine = $this->mockEngine(); - - $map = [ - ['template.php', true], - ['template.twig', false] - ]; - - $engine->method('supports')->will($this->returnValueMap($map)); - $view = $this->newView($engine); - - $this->assertTrue($view->supports('template.php')); - $this->assertFalse($view->supports('template.twig')); - } - - /** @test */ - public function itShouldSetListeners() - { - $engine = $this->mockEngine(); - $view = $this->newView($engine); - - $called = false; - $listener = $this->getMock('Lucid\Template\Listener\ListenerInterface'); - $listener->method('onRender')->will($this->returnCallback(function () use (&$called) { - $called = true; - })); - - $view->setListeners(['template' => $listener]); - $view->notifyListeners('template'); - $this->assertTrue($called); - - $called = false; - $view->notifyListeners('new.template'); - $this->assertFalse($called); - } - - /** @test */ - public function itShouldBeAbleToFlushData() - { - $engine = $this->mockEngine(); - $view = $this->newView($engine); - - $listener = $this->getMock('Lucid\Template\Listener\ListenerInterface'); - $listener->method('onRender')->will($this->returnCallback(function ($data) { - $data->add('foo', 'bar'); - })); - - $view->addListener('template', $listener); - - $view->notifyListeners('template'); - $data = $view->flushData('template'); - $this->assertInstanceof('Lucid\Template\Data\TemplateDataInterface', $data); - $this->assertArrayHasKey('foo', $data->all()); - $this->assertFalse($view->flushData('template')); - } - - /** @test */ - public function itShouldCallDisplayOnDisplayInterfaces() - { - $engine = $this->getMock('Lucid\Template\Tests\Stubs\Displayable'); - $engine->method('supports')->with('template')->willReturn(true); - $view = $this->newView($engine); - - $engine->expects($this->once())->method('display')->will($this->returnCallback(function ($name) { - if ('template' !== $name) { - $this->fail(); - } - })); - - $view->display('template'); - } - - /** @test */ - public function itShouldCallRenderOnNoneDisplayInterfaces() - { - $view = $this->newView($engine = $this->mockEngine()); - $engine->method('supports')->with('template')->willReturn(true); - - $engine->expects($this->once())->method('render')->will($this->returnCallback(function (...$args) { - if ('template' !== $args[0]) { - $this->fail(); - } - })); - - $view->display('template'); - } - - /** @test */ - public function itShouldReturnItsEngine() - { - $view = $this->newView($engine = $this->mockEngine()); - $engine->method('supports')->with('template')->willReturn(true); - - $view->getEngineForTemplate('template'); - } - - /** @test */ - public function itShouldResolveEngineOnDelegatingEngine() - { - $engine = $this->getMockbuilder('Lucid\Template\DelegatingEngine') - ->disableOriginalConstructor()->getMock(); - $view = $this->newView($engine); - - $engine->expects($this->once())->method('resolveEngine')->with('template')->willReturn($this->mockEngine()); - $view->getEngineForTemplate('template'); - } - - /** @test */ - public function itShouldSetSelfOnViewAwareEngines() - { - $engine = $this->getMockbuilder('Lucid\Template\Engine') - ->disableOriginalConstructor()->getMock(); - $view = $this->newView($engine); - - $engine->expects($this->once())->method('supports')->with('template')->willReturn(true); - $engine->expects($this->once())->method('setManager')->with($view); - $view->getEngineForTemplate('template'); - } - - /** @test */ - public function itShouldThrowExceptionOnNoneSupportedTemplate() - { - $view = $this->newView($engine = $this->mockEngine()); - $engine->method('supports')->with('template.html')->willReturn(false); - - try { - $view->getEngineForTemplate('template.html'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } - - $this->fail(); - } - - protected function newView($engine = null) - { - return new View($engine ?: $this->mockEngine()); - } - - protected function mockEngine() - { - return $this->getMockBuilder('Lucid\Template\EngineInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/writer/.coveralls.yml b/lucid/writer/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/writer/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/writer/.travis.yml b/lucid/writer/.travis.yml deleted file mode 100644 index 07d4d2b..0000000 --- a/lucid/writer/.travis.yml +++ /dev/null @@ -1,36 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* --ignore=tests/Object/Fixures src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/writer/LICENSE.md b/lucid/writer/LICENSE.md deleted file mode 100644 index 27c4f83..0000000 --- a/lucid/writer/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright (c) 2013-2014 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/writer/README.md b/lucid/writer/README.md deleted file mode 100644 index 52996ab..0000000 --- a/lucid/writer/README.md +++ /dev/null @@ -1,419 +0,0 @@ -# Dumping strings with elegance - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/writer/tree/master) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/writer/blob/master/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/writer/master.svg?style=flat-square)](https://travis-ci.org/lucidphp/writer) -[![HHVM](https://img.shields.io/hhvm/lucid/writer/dev-master.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/writer) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/writer/master.svg?style=flat-square)](https://coveralls.io/r/lucidphp/writer) - -## Installation - -```bash -> composer require lucid/writer -``` - -## The Writer - -### Dumping strings - -Write a 2 line text block: - -```php -writeln('foo') - ->writeln('bar'); - -echo $writer->dump(); //"foo\nbar" -``` - -### Behavior - -By defatault, the Writer will remove trailing spaces at the end of each line. - -You may override this behavior by calling the `allowTrailingSpace()` -method. - -```php -allowTrailingSpace(true); // will now preserve trailing space chars for each line. - -``` - -### Indentation - -The default indentation level is 4 spaces. - -If you require a different level using spaces, you'll have to specify this on the. -constructor: - -```php -writeln('foo') - ->indent() - ->writeln('bar'); - -echo $writer->dump(); //"foo\n bar" -``` - -You may also change spaces to tabs using the `useTabs()` method. - -```php -useTabs(true); - -// … -``` - -### Output indentation - -Output indentation indents the whole block and is applied just before the -string is being dumped. The value passed to `setOutputIndentation(int $level)` -acts as a multiplyer. - -### API - -Fluent methods: - -- **`Lucid\Writer\Writer` writeln( `string|null $str` )** - -Adds a line. - -- **`Lucid\Writer\Writer` indent( `void` )** - -Adds an indentation. - -- **`Lucid\Writer\Writer` replaceln( string $str, int $index)** - -Replaces a line at a line index. - -- **`Lucid\Writer\Writer` removeln( `int $index` )** - -Removes a line at a line index. - -- **`Lucid\Writer\Writer` popln ( `void` )** - -Removes the last line. - -- **`Lucid\Writer\Writer` appendln ( `string $str` )** - -Appends a string to the last line. - -None fluent methods: - -- **`void` ignoreNull( `bool $ignore` )** -Don't add a line if `$str` in `Writer::writeln()` is `null`. Default is on. - -- **`void` allowTrailingSpace( `bool $space` )** -Allow/Disallow traling space chars. Default is off. - -- **`void` useTabs( `void` )** -Use Tabs for indentation instead of spaces. - -- **`void` setOutputIndentation( `int $level` )** -Sets the output indentation level of the whole text block. -The level value increments the indentation by one indent, e.g. `0` is no additional indentation, `1` is one indent, etc. -Default is `0`. - -- **`int` getOutputIndentation( `void` )** -Gets the output indentation level. (see `Writer::setOutputIndentation()`); - -## Class, Interface, and Trait Writers - -Dump PSR-2 compliant php source code. - -There're three object generators, `InterfaceWriter`, `ClassWriter`, and `TraitWriter`. -All object generators share a common API. - -### Shared API - -- **setParent( `string $parent` )** - -This is a one time operation. Once the parent is set, you cannot change it. -`$parent` name must be the FQN of the parent interface or class. - -- **addUseStatement( `string $use` )** - -Adds a use statement to the php document. Naming conflicts will automatically -be resolved, however you can set your own alias by declating the import like -this `\Acme\Foo as FooAlias`. By default `Acme\Lib\Foo` will become `LibFoo`, -or `AcmeLibFoo`, or `AcmeLibFooAlias`, and so on. -Note that the use statement is considered to be the FQN; - -- **getImportResolver( )** - -Will return an instance of `Lucid\Writer\Object\ImportResolver`. -This is useful if you need to know the aliases name of a imported string -(interface, trait, parent class or usestatement), e.g. - -```php -getImportResolver()->getAlias('Acme\MyClass') // e.g. AcmeMyClassAlias; -``` - -- **`void` addConstant( `Lucid\Writer\Object\Constant $constant` )** - -Adds a constant to the interface. - -- **`void` addMethod( `Lucid\Writer\Object\MethodInterface $method` )** - -Takes an object of type `Lucid\Writer\Object\MethodInterface` and adds it to -the object declaration. - -- **`Lucid\Writer\Object\DocBlock` getDoc( `void` )** - -Returns an instance of `Lucid\Writer\Object\DocBlock` that represents the -document level docblock. - -- **`Lucid\Writer\Object\DocBlock` getObjDoc( `void` )** - -Returns an instance of `Lucid\Writer\Object\DocBlock` that represents the -object level docblock. - -- **`void` noAutoGenerateTag( void )** - -By default, the objectwriter will add a timestamp to the document level -docblock. Use this if you wan't to deactivate this behavior. - -### InterfaceWriter - -Use this for autogenerating php interfaces. - -```php -generate()); - -``` -Results in: - -```php -generate()); - -``` - -Results in: - -```php -setParent('Acme\Lib\Bar'); -$cg->addProperty(new Property('foo', Property::IS_PRIVATE, 'string')); -$cg->addConstant(new Constant('T_ASW', '42', 'int')); -$cg->addMethod($method = new Method('__construct', Method::IS_PUBLIC, Method::T_VOID)); - -// declare method: -$method->setDescription('Constructor.') -$method->addArgument(new Argument('foo', Method::T_STRING, 'null')); -$method->setBody('$this->foo = $foo;'); - -// Add traits: -$cg->addTrait($foo = 'Acme\Lib\Traits\FooTrait'); -$cg->addTrait($bar = 'Acme\Lib\Traits\BarTrait'); -// resolve trait conflicts: -$cg->useTraitMethodAs($foo, 'getFoo', 'getFooStr', Method::IS_PRIVATE); -$cg->replaceTraitConflict($bar, $foo, 'getBar'); - -// modify the class doc. -$cg->getObjDoc() - ->setDescription('Some class.') - ->setLongDescription("Some more info on the class.\nSome more lines.") - ->addAnnotation('author', 'Thomas Appel '); - -echo $cg->generate(); - -``` -Results in - -```php - - */ -class Foo extends Bar -{ - use FooTrait, - BarTrait { - FooTrait::getFoo as private getFooStr; - BarTrait::getBar insteadof FooTrait; - } - - /** @var int */ - const T_ASW = 42; - - /** @var string */ - private $foo; - - /** - * Constructor. - * - * @param string $foo - */ - public function __construct($foo = null) - { - $this->foo = $foo; - } -} -``` - -### TraitWriter - -Behaves like the `ClassWriter` except there're no constants and interfaces. - -#### Notes - -Setting method bodies is up to you. However, if you rely on class base names -that have been imported you can utilize the import resolver to determine the -actual shortname that's used on the object writer. - -Also see the [Shared API](#shared-api) section. - -```php -getImportResolver()->getAlias('Acme\MyClass').';'; -$method->setBody($body); - -``` diff --git a/lucid/writer/composer.json b/lucid/writer/composer.json deleted file mode 100644 index cb18c8c..0000000 --- a/lucid/writer/composer.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "lucid/writer", - "license": "MIT", - "description": "object definition generators", - "require": { - "php": ">=5.6", - "lucid/common": "dev-master" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev" - }, - "license": "MIT", - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "autoload": { - "psr-4": { - "Lucid\\Writer\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Writer\\Tests\\": "tests/" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/writer/phpunit.xml.dist b/lucid/writer/phpunit.xml.dist deleted file mode 100644 index f0bbf62..0000000 --- a/lucid/writer/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/writer/src/File/JsonGenerator.php b/lucid/writer/src/File/JsonGenerator.php deleted file mode 100644 index fe12c72..0000000 --- a/lucid/writer/src/File/JsonGenerator.php +++ /dev/null @@ -1,88 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\File; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Common\Helper\Arr; -use Lucid\Writer\GeneratorInterface; - -/** - * @class JsonGenerator - * @package Selene\Module\Writer\Generator\File - * @version $Id$ - */ -class JsonGenerator implements GeneratorInterface -{ - use Stringable; - - /** - * content - * - * @var array - */ - private $payload; - - /** - * Constructor. - * - * @param array $contents - */ - public function __construct(array $payload = []) - { - $this->setContent($payload); - } - - /** - * setContent - * - * @param array $contents - * - * @return JsonGenerator - */ - public function setContent(array $contents) - { - $this->payload = []; - - array_walk($contents, function ($val, $key) { - $this->addContent($key, $val); - }); - - return $this; - } - - /** - * addContent - * - * @param string $key - * @param mixed $content - * - * @return JsonGenerator - */ - public function addContent($key, $content) - { - Arr::set($this->payload, $key, $content); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = false) - { - $writer = (new Writer) - ->writeln(json_encode($this->payload, JSON_PRETTY_PRINT)); - - return $raw ? $writer : $writer->dump(); - } -} diff --git a/lucid/writer/src/File/PhpGenerator.php b/lucid/writer/src/File/PhpGenerator.php deleted file mode 100644 index 5319a2c..0000000 --- a/lucid/writer/src/File/PhpGenerator.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\File; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Writer\FormatterHelper; -use Lucid\Writer\GeneratorInterface; - -/** - * @class PhpGenerator - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class PhpGenerator implements GeneratorInterface -{ - use FormatterHelper, - Stringable; - - /** @var array */ - private $contents = []; - - /** - * Constructor. - * - * @param bool $raw - */ - public function generate($raw = false) - { - $writer = new Writer; - $writer - ->writeln('newline(); - - array_map([$writer, 'writeln'], $this->contents); - - $writer->appendln(PHP_EOL); - - return $raw ? $writer : $writer->dump(); - } - - /** - * addString - * - * @param string $string - * - * @return void - */ - public function addString($string) - { - $this->contents[] = $string; - } - - /** - * addArray - * - * @param array $array - * - * @return void - */ - public function addArray(array $array) - { - $this->contents = $this->contents + explode("\n", $this->extractParams($array)); - } -} diff --git a/lucid/writer/src/FormatterHelper.php b/lucid/writer/src/FormatterHelper.php deleted file mode 100644 index 37f1377..0000000 --- a/lucid/writer/src/FormatterHelper.php +++ /dev/null @@ -1,105 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer; - -use Lucid\Common\Helper\Arr; - -/** - * @trait FormatterHelper - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -trait FormatterHelper -{ - /** - * indent - * - * @param int $count - * - * @access public - * @return string - */ - public function indent($count = 4) - { - return $count <= 0 ? '' : str_repeat(' ', (int)$count); - } - - /** - * extractParams - * - * @param array $params - * @param int $indent - * - * @access protected - * @return string - */ - public function extractParams(array $params, $indent = 0) - { - $indent = $indent + 4; - $result = $this->doExctractParams($params, $indent); - - return $this->indent(max($indent - 4, 0)) . $result; - } - - /** - * doExctractParams - * - * @param array $params - * @param int $indend - * - * @return string - */ - private function doExctractParams(array $params, $indent = 0) - { - $array = []; - - foreach ($params as $param => $value) { - if (is_array($value)) { - $value = $this->doExctractParams($value, $indent + 4); - } elseif (is_string($value) && 0 === strpos($value, '$this')) { - $value = $value; - } else { - $value = $this->exportVar($value); - } - - $key = $this->exportVar($param); - $array[$key] = sprintf('%s%s => %s,', $this->indent($indent), $key, $value); - } - - if (empty($array)) { - return '[]'; - } - - $flat = sprintf("[\n%s\n%s]", implode("\n", $array), $this->indent(max($indent - 4, 0))); - - if (Arr::isList($array, true)) { - return preg_replace('#\d+ \=\>\s?#i', '', $flat); - } - - return $flat; - } - - /** - * dumpExport - * - * @param mixed $param - * - * @access public - * @return string - */ - public function exportVar($param) - { - return preg_replace(['~NULL~', '~FALSE~', '~TRUE~'], ['null', 'false', 'true'], var_export($param, true)); - } -} diff --git a/lucid/writer/src/GeneratorInterface.php b/lucid/writer/src/GeneratorInterface.php deleted file mode 100644 index dd5a1ef..0000000 --- a/lucid/writer/src/GeneratorInterface.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer; - -/** - * @class GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -interface GeneratorInterface -{ - /** @var bool */ - const RV_STRING = false; - - /** @var bool */ - const RV_WRITER = true; - - /** - * generate - * - * @param bool $raw - * - * @return string|WriterInterface - */ - public function generate($raw = self::RV_STRING); - - public function __toString(); -} diff --git a/lucid/writer/src/Object/AbstractBlock.php b/lucid/writer/src/Object/AbstractBlock.php deleted file mode 100644 index fdab199..0000000 --- a/lucid/writer/src/Object/AbstractBlock.php +++ /dev/null @@ -1,108 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Writer\GeneratorInterface; - -/** - * @class DocBlock implements GeneratorInterface - * - * @see GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author Thomas Appel - */ -abstract class AbstractBlock implements GeneratorInterface -{ - use Stringable; - - /** - * {@inheritdoc} - */ - public function generate($raw = self::RV_STRING) - { - $writer = new Writer; - - $this->openBlock($writer); - $this->writeBlock($writer); - $this->closeBlock($writer); - - return $raw ? $writer : $writer->dump(); - } - - /** - * isEmpty - * - * @return boolean - */ - abstract public function isEmpty(); - - /** - * openBlock - * - * @param Writer $writer - * - * @return void - */ - abstract protected function openBlock(Writer $writer); - - /** - * closeBlock - * - * @param Writer $writer - * - * @return void - */ - protected function closeBlock(Writer $writer) - { - return $writer->writeln(' */'); - } - - /** - * writeBlock - * - * @param Writer $writer - * - * @return void - */ - protected function writeBlock(Writer $writer) - { - $newline = false; - $lnbuff = []; - - $this->doWriteBlock($$writer, $lnbuff, $newline); - - $this->blockLines($writer, $lnbuff); - } - - abstract protected function doWriteBlock(Writer $writer, array &$lnbuff, &$newline); - - abstract public function copy(); - - /** - * blockLine - * - * @param Writer $writer - * @param array $lines - * - * @return void - */ - protected function blockLines(Writer $writer, array $lines) - { - foreach ($lines as $line) { - $writer->writeln(' * ' . $line); - } - } -} diff --git a/lucid/writer/src/Object/AbstractWriter.php b/lucid/writer/src/Object/AbstractWriter.php deleted file mode 100644 index 6e7ac24..0000000 --- a/lucid/writer/src/Object/AbstractWriter.php +++ /dev/null @@ -1,584 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use DateTime; -use InvalidArgumentException; -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Writer\WriterInterface; -use Lucid\Writer\GeneratorInterface; - -/** - * @class AbstractWriter - * @see GeneratorInterface - * @abstract - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -abstract class AbstractWriter implements GeneratorInterface -{ - use ImportHelper, - Stringable; - - /** @var int */ - const I_NATIVE = 890; - - /** @var int */ - const I_HHVM = 891; - - /** @var string */ - const N_SEP = '\\'; - - /** @var string */ - private $fqn; - - /** @var string */ - private $name; - - /** @var string */ - private $namespace; - - /** @var array */ - private $methods; - - /** @var array */ - protected $uses; - - /** @var array */ - protected $blocks; - - /** @var int */ - private $type; - - /** @var int */ - private $doctype; - - /** @var bool */ - private $locked; - - /** @var bool */ - private $noAutoGenerateTag; - - /** @var callable */ - private $usesort; - - /** @var array */ - private static $types = [ - T_CLASS => 'class', - T_TRAIT => 'trait', - T_INTERFACE => 'interface' - ]; - - /** @var array */ - private static $docTypes = [ - self::I_NATIVE => ' 'setNamespace($namespace); - $this->setName($name); - - $this->type = $type; - $this->uses = []; - $this->blocks = []; - $this->methods = []; - - $this->noAutoGenerateTag = false; - - $this->setDocType($docType); - } - - /** - * Sets the sort function for use statements. - * - * @param callable $sort - * - * @return void - */ - public function setUseSort(callable $sort) - { - $this->usesort = $sort; - } - - /** - * setDocType - * - * @param mixed $type - * - * @return void - */ - public function setDocType($type = self::I_NATIVE) - { - $this->doctype = $this->validateDocType($type); - } - - /** - * Get the interface|class|trait name. - * - * @return string the name - */ - final public function getName() - { - return $this->name; - } - - /** - * Get the namespace - * - * @return string the namespace `NULL` if none. - */ - final public function getNamespace() - { - return $this->namespace; - } - - /** - * Get the full qualified name - * - * @return string the full qualified name as string - */ - public function getFqn() - { - return $this->fqn; - } - - /** - * Get the document block - * - * @return DocBlock the docblock - */ - public function getDoc() - { - if (!isset($this->blocks['doc'])) { - $this->blocks['doc'] = new CommentBlock; - } - - return $this->blocks['doc']; - } - - /** - * Get the class|interface|trait description - * - * @return DocBlock - */ - public function getObjDoc() - { - if (!isset($this->blocks['obj'])) { - $this->blocks['obj'] = new DocBlock; - } - - return $this->blocks['obj']; - } - - /** - * Switch off auto generation of parameter annotaions. - * - * @return void - */ - public function noAutoGenerateTag() - { - $this->noAutoGenerateTag = true; - } - - /** - * Set methods. - * - * @param array $methods - * - * @return void - */ - public function setMethods(array $methods) - { - $this->methods = []; - - array_map([$this, 'addMethod'], $methods); - } - - /** - * Add a method. - * - * @param MethodInterface $method - * - * @return void - */ - public function addMethod(MethodInterface $method) - { - $this->methods[] = $method; - } - - /** - * Add a use statement. - * - * @param string $use - * - * @return void - */ - public function addUseStatement($use) - { - $this->addToImportPool($this->uses, $use); - } - - /** - * Shortcut for `addUseStatement()` - * - * @see ObjectWriter::addUseStatement() - */ - public function addImport($use) - { - return $this->addUseStatement($use); - } - - /** - * {@inheritdoc} - */ - public function generate($raw = self::RV_STRING) - { - $writer = new Writer; - - $this->prepareDoc($ddoc = clone($this->getDoc())); - $this->prepareObjDoc($odoc = clone($this->getObjDoc())); - - $writer->writeln($this->getDocType()); - - if (!$ddoc->isEmpty()) { - $writer - ->newline() - ->writeln($ddoc->generate()); - } - - $writer - ->newline() - ->writeln(sprintf('namespace %s;', $ns = $this->getNamespace())); - - $this->writeUseStatements($writer, $this->getImportResolver(), null !== $ns); - - $writer - ->newline() - ->writeln($odoc->generate()); - - $this->writeObject($writer); - - return $raw ? $writer : $writer->dump(); - } - - /** - * trimNs - * - * @param string $name - * - * @return string - */ - public static function trimNs($name) - { - return trim($name, self::N_SEP); - } - - /** - * writeBody - * - * @param Writer $writer - * - * @access protected - * @return mixed - */ - protected function writeObject(WriterInterface $writer) - { - $writer - ->writeln($this->getTypePrefix() . $this->getType().' '.$this->getName()); - - $writer->appendln($this->getObjectDeclarationExtension()) - ->writeln('{'); - - $this->writeObjectBody($writer) - //->outdent() - ->writeln('}'.PHP_EOL); - } - - /** - * tagAutogenerated - * - * @return bool - */ - protected function tagAutogenerated() - { - return !$this->noAutoGenerateTag; - } - - /** - * writeDoc - * - * @param Writer $writer - * - * @return void - */ - protected function prepareDoc(DocBlock $doc) - { - if ($this->tagAutogenerated()) { - $doc->setDescription( - $this->getAutoGenerateTag() . ($doc->hasDescription() ? PHP_EOL . $doc->getDescription() : '') - ); - } - } - - /** - * prepareObjDoc - * - * @param DocBlock $doc - * - * @return void - */ - abstract protected function prepareObjDoc(DocBlock $doc); - - /** - * getType - * - * @throws \LogicException - * @return string - */ - protected function getType() - { - return self::$types[$this->type]; - } - - /** - * getDocType - * - * @return string - */ - protected function getDocType() - { - return self::$docTypes[$this->doctype]; - } - - - /** - * getTypePrefix - * - * @return null|string - */ - protected function getTypePrefix() - { - return null; - } - - /** - * getUseStatements - * - * @param Writer $writer - * @param bool $newLine - * - * @return void - */ - protected function writeUseStatements(WriterInterface $writer, ImportResolver $resolver, $newLine = false) - { - $uses = []; - - false !== $newLine && $writer->newline(); - - foreach (array_unique($this->getImports()) as $use) { - if ($this->inNamespace($use) && !$resolver->hasAlias($use)) { - continue; - } - - $uses[] = $resolver->getImport($use); - } - - if (is_callable($this->usesort)) { - usort($uses, $this->usesort); - } else { - natsort($uses); - } - - foreach ($uses as $u) { - $writer->writeln(sprintf('use %s;', $u)); - } - } - - /** - * getImports - * - * @return array - */ - protected function getImports() - { - return $this->uses; - } - - /** - * hasMethods - * - * @return bool - */ - protected function hasMethods() - { - return !empty($this->methods); - } - - /** - * getObjectDeclarationExtension - * - * @return null|string - */ - protected function getObjectDeclarationExtension() - { - return null; - } - - /** - * getObjectBody - * - * @param Writer $writer - * - * @return Writer - */ - protected function writeObjectBody(WriterInterface $writer) - { - if (empty($this->methods)) { - return $writer; - } - - if ($this->hasItemsBeforeMethods()) { - $writer->newline(); - } - - foreach ($this->methods as $method) { - $writer->writeln($method)->newline(); - } - - $writer->popln(); - - return $writer; - } - - /** - * hasItemsBeforeMethods - * - * @return bool - */ - protected function hasItemsBeforeMethods() - { - return false; - } - - /** - * getAutoGenerateTag - * - * @return string - */ - private function getAutoGenerateTag() - { - return sprintf('This file was generated at %s.', (new DateTime())->format('Y-m-d h:m:s')); - } - - /** - * Get the knowen FQN from a name. - * - * @param string $name - * - * @return array indexed array with two items, $namespace, and $name. - */ - final protected function desectName($name) - { - if (0 === substr_count($name, self::N_SEP) || 0 === strrpos($name, self::N_SEP)) { - return [null, $name]; - } - - $namespace = substr($name, 0, $pos = strrpos($name, self::N_SEP)); - $name = substr($name, $pos + 1); - - return [static::trimNs($namespace), $name]; - } - - /** - * @param string $name - * - * @return void - */ - protected function inNamespace($name) - { - list ($ns, $name) = $this->desectName($name); - - if (0 === strcmp($this->getNamespace(), static::trimNs($ns))) { - return true; - } - - return false; - } - - /** - * setNamespace - * - * @param string|null $namespace - * - * @return void - */ - private function setNamespace($namespace = null) - { - if (null === $namespace) { - return; - } - - $this->namespace = static::trimNs($namespace); - } - - /** - * setName - * - * @param mixed $name - * - * @return void - */ - private function setName($name) - { - list ($ns, $name) = $this->desectName($name); - - $namespace = $this->getNamespace(); - - if (null !== $ns) { - if (null !== $namespace) { - throw new InvalidArgumentException(); - } else { - $this->setNamespace($ns); - } - } - - $this->fqn = self::N_SEP.static::trimNs($this->namespace ? $this->namespace.self::N_SEP.$name : $name); - $this->name = $name; - } - - /** - * validateDocType - * - * @param int $type - * - * @return int - */ - private function validateDocType($type) - { - if (null !== $type && !in_array($type, array_keys(self::$docTypes))) { - throw new InvalidArgumentException('Invalid doctype.'); - } - - return $type; - } -} diff --git a/lucid/writer/src/Object/Annotateable.php b/lucid/writer/src/Object/Annotateable.php deleted file mode 100644 index 7f4a7c2..0000000 --- a/lucid/writer/src/Object/Annotateable.php +++ /dev/null @@ -1,109 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; - -/** - * @class Annotateable - * @see MemberInterface - * @abstract - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -abstract class Annotateable implements MemberInterface -{ - /** @var DocBlock */ - private $docBlock; - - /** @var bool */ - private $inline; - - /** - * Constructor. - */ - public function __construct() - { - $this->docBlock = new DocBlock; - } - - /** - * Set the doc block comment - * - * @param mixed $comment - * - * @return void - */ - public function addAnnotation($name, $desc = null) - { - $this->docBlock->addAnnotation($name, $desc); - } - - /** - * addParam - * - * @param string $type - * @param string $var - * @param string $desc - * - * @return void - */ - public function addParam($type, $var, $desc = null) - { - $this->docBlock->addParam($type, $var, $desc); - } - - /** - * Set the doc block comment - * - * @param mixed $comment - * - * @return void - */ - public function setDescription($description) - { - $this->docBlock->setDescription($description); - } - - /** - * setLongDescription - * - * @return void - */ - public function setLongDescription($description) - { - $this->docBlock->setLongDescription($description); - } - - /** - * getDocBlock - * - * @return Writer|string - */ - final protected function getDoc(Writer $writer, $asString = false) - { - $this->prepareAnnotations($block = clone $this->docBlock); - - $writer->writeln($block); - - return $asString ? $writer->dump() : $writer; - } - - /** - * prepareAnnotations - * - * @return array - */ - abstract protected function prepareAnnotations(DocBlock $block); -} diff --git a/lucid/writer/src/Object/Argument.php b/lucid/writer/src/Object/Argument.php deleted file mode 100644 index 67a34b9..0000000 --- a/lucid/writer/src/Object/Argument.php +++ /dev/null @@ -1,161 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Writer\GeneratorInterface; - -/** - * @class Argument - * @see GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class Argument implements GeneratorInterface -{ - use Stringable; - - /** @var string */ - private $name; - - /** @var string */ - private $type; - - /** @var string */ - private $default; - - /** @var bool */ - private $isReference; - - /** @var bool */ - private $isVariadic; - - /** @var array */ - private static $primitives = [ - 'bool', 'boolean', 'int', 'integer', 'float', - 'double', 'string', 'object', 'mixed', - ]; - - /** @var array */ - private static $silent = ['null', 'void', 'variadic']; - - /** - * Constructor. - * - * @param string $name - * @param string $type - * @param string $default - * @param bool $isRef - * @param bool $isVariadic - */ - public function __construct($name, $type = null, $default = null, $isRef = false, $isVariadic = false) - { - $this->name = $name; - $this->type = $type; - $this->default = $default; - - $this->isReference($isRef); - $this->isVariadic($isVariadic); - } - - /** - * Get the argument name. - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Set the value type. - * - * @return void - */ - public function setType($type) - { - $this->type = $type; - } - - /** - * Get the argument type. - * - * @return string - */ - public function getType() - { - return $this->type; - } - - /** - * Sets or unsets the default value. - * - * @return void - */ - public function setDefault($default) - { - $this->default = $default; - } - - /** - * Sets the `isReference` property to true or false. - * - * @param bool $ref - * - * @return void - */ - public function isReference($ref) - { - $this->isReference = (bool)$ref; - } - - /** - * Sets the `isVariadic` property to true or false. - * - * @param bool $variadic - * - * @return void - */ - public function isVariadic($variadic) - { - $this->isVariadic = (bool)$variadic; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = false) - { - $writer = new Writer; - - $prefix = $this->isReference ? '&' : ''; - $prefix .= $this->isVariadic ? '...' : ''; - - $type = null === $this->type || in_array($this->type, array_merge(self::$silent, self::$primitives)) ? - '' : - $this->type.' '; - - if (null !== $this->default) { - $line = sprintf('%s%s$%s = %s', $type, $prefix, $this->name, $this->default); - } else { - $line = sprintf('%s%s$%s', $type, $prefix, $this->name); - } - - $writer->writeln($line); - - return $raw ? $writer : $writer->dump(); - } -} diff --git a/lucid/writer/src/Object/ClassWriter.php b/lucid/writer/src/Object/ClassWriter.php deleted file mode 100644 index 78c4e93..0000000 --- a/lucid/writer/src/Object/ClassWriter.php +++ /dev/null @@ -1,176 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use InvalidArgumentException; -use Lucid\Writer\WriterInterface; - -/** - * @class ClassWriter - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class ClassWriter extends InterfaceWriter -{ - use TraitAwareWriterHelper; - - /** @var bool */ - private $abstract; - - /** @var array */ - private $interfaces; - - /** - * Constructor. - * - * @param string $name - * @param string $namespace - * @param string $parent - * @param int $docType - */ - public function __construct($name, $namespace = null, $parent = null, $docType = self::I_NATIVE) - { - $this->traits = []; - $this->interfaces = []; - $this->properties = []; - $this->abstract = false; - - parent::__construct($name, $namespace, $parent, $docType); - } - - /** - * Marks the class abstract. - * - * @param bool $abstract - * - * @return void - */ - public function setAbstract($abstract) - { - $this->abstract = (bool)$abstract; - } - - /** - * Adds an interface to the class definition. - * - * @param string $interface - * - * @return void - */ - public function addInterface($interface) - { - $this->addToImportPool($this->interfaces, $interface); - } - - /** - * {@inheritdoc} - */ - public function addMethod(MethodInterface $method) - { - if ($method instanceof InterfaceMethod) { - throw new InvalidArgumentException( - sprintf('Class method %s must not be instance of "InterfaceMethod".', $method->getName()) - ); - } - - AbstractWriter::addMethod($method); - } - - /** - * {@inheritdoc} - */ - protected function hasItemsBeforeMethods() - { - return parent::hasItemsBeforeMethods() || !empty($this->traits) || !empty($this->properties); - } - - /** - * {@inheritdoc} - */ - protected function getObjectDeclarationExtension() - { - $dcl = parent::getObjectDeclarationExtension(); - - if (empty($this->interfaces)) { - return $dcl; - } - - $aliases = []; - - foreach ($this->interfaces as $interface) { - $aliases[] = $this->getImportResolver()->getAlias($interface); - } - - return $dcl . ' implements ' . implode(', ', $aliases); - } - - /** - * {@inheritdoc} - */ - protected function writeObjectBody(WriterInterface $writer) - { - $this->writeTraits($writer, $resolver = $this->getImportResolver()); - $this->writeProperties($writer, $resolver); - - return parent::writeObjectBody($writer); - } - - /** - * {@inheritdoc} - */ - protected function getImports() - { - return array_merge($this->uses, $this->interfaces, $this->traits, $this->interfaces); - } - - /** - * {@inheritdoc} - */ - protected function getTypeConstant() - { - return T_CLASS; - } - - /** - * {@inheritdoc} - */ - protected function getTypePrefix() - { - return $this->abstract ? 'abstract ' : null; - } - - /** - * {@inheritdoc} - */ - protected function prepareObjDoc(DocBlock $doc) - { - $resolver = $this->getImportResolver(); - - if (!empty($this->interfaces)) { - foreach (array_reverse($this->interfaces) as $interface) { - $name = $resolver->hasAlias($interface) ? $resolver->getAlias($interface) : $interface; - $doc->unshiftAnnotation('see', self::trimNs($name)); - } - } - - if ($this->parent) { - $name = $resolver->hasAlias($this->getParent()) ? - $resolver->getAlias($this->getParent()) : - $this->getParent(); - $doc->unshiftAnnotation('see', self::trimNs($name)); - } - - $doc->unshiftAnnotation($this->getType(), $this->getName()); - } -} diff --git a/lucid/writer/src/Object/Constant.php b/lucid/writer/src/Object/Constant.php deleted file mode 100644 index 1a48aaa..0000000 --- a/lucid/writer/src/Object/Constant.php +++ /dev/null @@ -1,93 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; -use Lucid\Writer\GeneratorInterface; - -/** - * @class Constant - * @see GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class Constant extends Annotateable implements GeneratorInterface -{ - use Stringable; - - /** @var string */ - private $name; - - /** @var string */ - private $value; - - /** @var string */ - private $type; - - /** - * Constructor. - * - * @param string $name - * @param string $value - */ - public function __construct($name, $value, $type = 'string') - { - $this->name = $name; - $this->value = $value; - $this->type = $type; - - parent::__construct(); - } - - /** - * generate - * - * @param boolean $raw - * - * @return void - */ - public function generate($raw = self::RV_STRING) - { - $writer = new Writer; - $writer->setOutputIndentation(1); - $this->getDoc($writer); - $writer->setOutputIndentation(1); - $writer->writeln(sprintf('const %s = %s;', strtoupper($this->name), $this->getValue())); - - return $raw ? $writer : $writer->dump(); - } - - protected function prepareAnnotations(DocBlock $block) - { - if (!$block->hasDescription()) { - $block->setInline(true); - } - - if ($block->hasAnnotations()) { - $block->unshiftAnnotation(null); - } - - $block->unshiftAnnotation('var', $this->type); - } - - private function getValue() - { - if ('string' === $this->type) { - return sprintf('\'%s\'', trim($this->value, '\'\"')); - } - - return $this->value; - } -} diff --git a/lucid/writer/src/Object/DocBlock.php b/lucid/writer/src/Object/DocBlock.php deleted file mode 100644 index 6e045dc..0000000 --- a/lucid/writer/src/Object/DocBlock.php +++ /dev/null @@ -1,358 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Stringable; -use Lucid\Writer\Writer; -use Lucid\Writer\WriterInterface; -use Lucid\Writer\GeneratorInterface; - -/** - * @class DocBlock - * @see GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class DocBlock implements GeneratorInterface -{ - use Stringable; - - /** @var int */ - const DESC_SHORT = 234; - - /** @var int */ - const DESC_LONG = 235; - - /** @var array */ - private $description = []; - - /** @var array */ - private $annotations = []; - - /** @var string|null */ - private $returnAnnotation; - - /** @var bool */ - private $inline = false; - - /** @var bool */ - private $setInline = false; - - /** - * Sets the short description. - * - * @param string $description - * - * @return void - */ - public function setDescription($description) - { - $this->description[self::DESC_SHORT] = $description; - } - - /** - * setInline - * - * @param bool $inline - * - * @return void - */ - public function setInline($inline) - { - $this->inline = (bool)$inline; - } - - /** - * Check if a short description is set. - * - * @return bool - */ - public function hasDescription() - { - return isset($this->description[self::DESC_SHORT]); - } - - /** - * Check if the long description is set. - * - * @return boolean - */ - public function hasLongDescription() - { - return isset($this->description[self::DESC_LONG]); - } - - /** - * Sets the long description. - * - * @param string $description - * - * @return void - */ - public function setLongDescription($description) - { - $this->description[self::DESC_LONG] = $description; - } - - /** - * Gets the short description. - * - * @return string|null - */ - public function getDescription() - { - return $this->description[self::DESC_SHORT]; - } - - /** - * Gets the long description. - * - * @return string|null - */ - public function getLongDescription() - { - return $this->description[self::DESC_LONG]; - } - - /** - * Sets the annotations. - * - * @param array $annotations - * - * @return void - */ - public function setAnnotations(array $annotations) - { - $this->annotations = []; - - foreach ($annotations as $annotation) { - if (is_array($annotation)) { - list ($name, $desc) = array_pad($annotation, 2, null); - $this->addAnnotation($name, $desc); - } else { - $this->addAnnotation(null); - } - } - } - - /** - * Adds a param annotation. - * - * @param string $type - * @param string $var - * @param string $description - * - * @return void - */ - public function addParam($type, $var, $description = null) - { - $this->annotations[] = ['param', $type . ' $'.$var . (null ===$description ? '' : ' ' .$description)]; - } - - /** - * Adds an annotation. - * - * @param string $name - * @param string $description - * - * @return void - */ - public function addAnnotation($name, $description = null) - { - $this->annotations[] = null === $name ? null : [$name, $description]; - } - - /** - * Adds an annotation to the top. - * - * @param string $name - * @param string $description - * - * @return void - */ - public function unshiftAnnotation($name, $description = null) - { - $this->addAnnotation($name, $description); - - array_unshift($this->annotations, array_pop($this->annotations)); - } - - /** - * Checks if annotations are set. - * - * @return bool - */ - public function hasAnnotations() - { - return !empty($this->annotations); - } - - /** - * Sets the return annotation. - * - * @param string $type - * @param string $description - * - * @return void - */ - public function setReturn($type, $description = null) - { - $this->returnAnnotation = [$type, $description]; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = self::RV_STRING) - { - $writer = new Writer; - - $this->openBlock($writer); - $this->writeBlock($writer); - $this->closeBlock($writer); - - if ($this->setInline) { - $ln2 = $writer->popln(); - $ln1 = $writer->popln(); - $writer->appendln($ln1.$ln2); - - $this->setInline = false; - } - - return $raw ? $writer : $writer->dump(); - } - - /** - * Check if the dock block is empty. - * - * @return bool - */ - public function isEmpty() - { - return !$this->hasDescription() && !$this->hasLongDescription() && - empty($this->annotations) && null === $this->returnAnnotation; - } - - /** - * Writes the opening dockblock line. - * - * @param WriterInterface $writer - * - * @return WriterInterface - */ - protected function openBlock(WriterInterface $writer) - { - return $writer->writeln('/**'); - } - - /** - * Writes the closing dockblock line. - * - * @param WriterInterface $writer - * - * @return WriterInterface - */ - protected function closeBlock(WriterInterface $writer) - { - return $writer->writeln(' */'); - } - - /** - * Writes descriptions and annotations to the Writer instance. - * - * @param WriterInterface $writer - * - * @return void - */ - protected function writeBlock(WriterInterface $writer) - { - $newline = false; - $lnbuff = []; - - if ($this->hasDescription()) { - $newline = true; - foreach (explode("\n", $this->description[self::DESC_SHORT]) as $line) { - $lnbuff[] = $line; - } - } - - if ($this->hasLongDescription()) { - if ($newline) { - $lnbuff[] = ''; - } - - $newline = true; - foreach (explode("\n", $this->description[self::DESC_LONG]) as $line) { - $lnbuff[] = $line; - } - } - - if ($this->hasAnnotations() && $newline) { - $lnbuff[] = ''; - } else { - $newline = $newline; - } - - foreach ($this->annotations as $annotation) { - if (null === $annotation) { - $lnbuff[] = ''; - continue; - } - - list ($name, $desc) = $annotation; - - $anno = '@'.$name . (null === $desc ? '' : ' ' .$desc); - - foreach (explode("\n", $anno) as $annot) { - $lnbuff[] = $annot; - } - } - - if (null !== $this->returnAnnotation) { - if ($newline) { - $lnbuff[] = ''; - } - - list ($type, $desc) = $this->returnAnnotation; - - foreach (explode("\n", $anno = '@return '.$type . (null === $desc ? '' : ' ' .$desc)) as $annot) { - $lnbuff[] = $annot; - } - } - - $this->blockLines($writer, $lnbuff); - } - - /** - * blockLine - * - * @param WriterInterface $writer - * @param array $lines - * - * @return void - */ - protected function blockLines(WriterInterface $writer, array $lines) - { - if ($this->inline && 1 === count($lines)) { - $this->setInline = true; - $prefix = ' '; - } else { - $prefix = ' * '; - } - - foreach ($lines as $line) { - $writer->writeln($prefix . $line); - } - } -} diff --git a/lucid/writer/src/Object/ImportHelper.php b/lucid/writer/src/Object/ImportHelper.php deleted file mode 100644 index 1c19777..0000000 --- a/lucid/writer/src/Object/ImportHelper.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -/** - * @trait ImportHelper - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -trait ImportHelper -{ - /** @var ImportResolver */ - private $importResolver; - - /** - * Returns the ImportResolver instance. - * - * @return ImportResolver - */ - public function getImportResolver() - { - if (null === $this->importResolver) { - $this->importResolver = new ImportResolver; - } - - return $this->importResolver; - } - - /** - * Adds an import to a given pool. - * - * @param array $pool the pool passed by reference. - * @param string $string the import name. - * - * @return void - */ - protected function addToImportPool(array &$pool, $string) - { - $this->getImportResolver()->add($string); - $pool[] = $string; - } -} diff --git a/lucid/writer/src/Object/ImportInterface.php b/lucid/writer/src/Object/ImportInterface.php deleted file mode 100644 index 78e196d..0000000 --- a/lucid/writer/src/Object/ImportInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -/** - * @interface ImportInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -interface ImportInterface -{ - /** - * Sets the ImportResolver instance. - * - * @param ImportResolver $resolver - * - * @return void - */ - public function setResolver(ImportResolver $resolver); -} diff --git a/lucid/writer/src/Object/ImportResolver.php b/lucid/writer/src/Object/ImportResolver.php deleted file mode 100644 index fe83835..0000000 --- a/lucid/writer/src/Object/ImportResolver.php +++ /dev/null @@ -1,232 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -/** - * This class handles class, interface, and trait aliases. - * - * @class ImportResolver - * - * @package Lucid\Writer - * @version $Id$ - * @author Thomas Appel - */ -class ImportResolver -{ - /** @var array */ - private $aliases = []; - - /** @var array */ - private $imports = []; - - /** - * Adds an import. - * - * @param string $import - * - * @return void - */ - public function add($import) - { - $import = $this->prepareImport($this->pad($import)); - - $name = $this->getBaseName($import); - - $imports = array_keys($this->imports); - - if (in_array($name, $this->imports) && !in_array($import, $imports)) { - $this->setAlias($import, $name); - } - - if (!isset($this->imports[$import])) { - $this->imports[$import] = $name; - } - } - - /** - * Returns an alias for an import. - * - * @param string $import - * - * @return string - */ - public function getAlias($import) - { - list($import, $alias) = $this->splitImport($o = $this->pad($import)); - - if (isset($this->aliases[$import])) { - return $this->trim($this->aliases[$import]); - } - - if (isset($this->imports[$import])) { - return $this->trim($this->imports[$import]); - } - - return $this->trim($o); - } - - /** - * Checks if the alias exitsts? - * - * @param string $import - * - * @return bool - */ - public function hasAlias($import) - { - list($import, $alias) = $this->splitImport($o = $this->pad($import)); - - return isset($this->aliases[$import]); - } - - /** - * Gets the import name as used with the use statement. - * - * If an alias exists for the given import, this method will return the - * original import name suffixed with the alias, e.g. `\Foo as FooAlias` - * - * @param string $import - * - * @return string - */ - public function getImport($import) - { - list($import, $alias) = $this->splitImport($o = $this->pad($import)); - - if (isset($this->aliases[$import])) { - return $this->trim($import.' as '.$this->aliases[$import]); - } - - return $this->trim($o); - } - - /** - * Generate an alias name for an import. - * - * @param string $import - * @param string $name - * - * @return void - */ - protected function setAlias($import, $name) - { - if (isset($this->aliases[$import])) { - return; - } - - $parts = preg_split('~\\\~', $import, -1, PREG_SPLIT_NO_EMPTY); - array_pop($parts); - $alias = $name; - - while (in_array($alias = $this->getAliasName($alias, $parts), $this->aliases)) { - continue; - } - - $this->aliases[$import] = $alias; - } - - /** - * Construct the alias name. - * - * @param string $alias - * @param array $parts - * @param string $suffix - * - * @return string - */ - protected function getAliasName($alias, array &$parts, $suffix = 'Alias') - { - if (0 < count($parts)) { - $alias = array_pop($parts) . $alias; - } else { - $alias = $alias . $suffix; - } - - return $alias; - } - - /** - * Get the import base name without the namespace. - * - * @param string $import - * - * @return string - */ - protected function getBaseName($import) - { - if (1 === substr_count($import, '\\')) { - return ltrim($import, '\\'); - } - - return substr($import, strrpos($import, '\\') + 1); - } - - /** - * prepareImport - * - * @param string $import - * - * @return string - */ - protected function prepareImport($import) - { - list ($import, $alias) = $this->splitImport($import); - - if (null !== $alias && !in_array($alias, $this->aliases)) { - $this->aliases[$import] = $alias; - } - - return $import; - } - - /** - * splitImport - * - * @param string $import - * - * @return array - */ - protected function splitImport($import) - { - if (1 === substr_count($import, $delim = ' as ')) { - list ($import, $alias) = explode($delim, $import); - - return [$import, $alias]; - } - - return [$import, null]; - } - - /** - * pad - * - * @param string $import - * - * @return string - */ - private function pad($import) - { - return sprintf('\%s', $this->trim($import)); - } - - /** - * trim - * - * @param mixed $str - * - * @return string - */ - private function trim($str) - { - return ltrim($str, '\\'); - } -} diff --git a/lucid/writer/src/Object/InterfaceMethod.php b/lucid/writer/src/Object/InterfaceMethod.php deleted file mode 100644 index bba267b..0000000 --- a/lucid/writer/src/Object/InterfaceMethod.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use LogicException; -use Lucid\Writer\Writer; - -/** - * @class InterfaceMethod - * @see Method - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class InterfaceMethod extends Method -{ - /** - * Constructor. - * - * @param string $name - * @param string $type - * @param bool $static - */ - public function __construct($name, $type = self::T_VOID, $static = false) - { - parent::__construct($name, self::IS_PUBLIC, $type, $static); - } - - /** - * {@inheritdoc} - */ - public function generate($raw = false) - { - $writer = new Writer(4, true); - $writer->setOutputIndentation(1); - - $this->getMethodDeclaration($writer, false); - $writer->appendln(';'); - - return $raw ? $writer : $writer->dump(); - } - - /** - * {@inheritdoc} - * - * @throws LogicException every time it is called. - */ - public function setAbstract($abstract) - { - if ((bool)$abstract) { - throw new LogicException('Cannot set interface method abstract.'); - } - } - - /** - * {@inheritdoc} - * - * @throws LogicException every time it is called. - */ - public function setBody($body) - { - throw new LogicException('Cannot set a method body on an interface method.'); - } -} diff --git a/lucid/writer/src/Object/InterfaceWriter.php b/lucid/writer/src/Object/InterfaceWriter.php deleted file mode 100644 index db23c3c..0000000 --- a/lucid/writer/src/Object/InterfaceWriter.php +++ /dev/null @@ -1,246 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use BadMethodCallException; -use Lucid\Writer\Stringable; -use InvalidArgumentException; -use Lucid\Writer\WriterInterface; - -/** - * @class InterfaceWriter - * @see AbstractWriter - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class InterfaceWriter extends AbstractWriter -{ - use Stringable; - - /** @var string */ - protected $parent; - - /** @var array */ - protected $constants; - - /** - * Constructor. - * - * @param string $name - * @param string $namespace - * @param string $parent - */ - public function __construct($name, $namespace = null, $parent = null, $docType = self::I_NATIVE) - { - parent::__construct($name, $namespace, $this->getTypeConstant(), $docType); - - $this->setParent($parent); - $this->constants = []; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = self::RV_STRING) - { - $this->prepareGenerate(); - - return parent::generate($raw); - } - - /** - * Sets the interface constants. - * - * @param array $constants - * - * @return void - */ - public function setConstants(array $constants) - { - $this->constants = []; - - array_map([$this, 'addConstant'], $constants); - } - - /** - * Adds an interface constant. - * - * @param Constant $const - * - * @return void - */ - public function addConstant(Constant $const) - { - $this->constants[] = $const; - } - - /** - * @throws InvalidArgumentException if no InterfaceMethod is passed as - * argument. - * - * {@inheritdoc} - */ - public function addMethod(MethodInterface $method) - { - if (!$method instanceof InterfaceMethod) { - throw new InvalidArgumentException( - sprintf('Method "%s" must be instance of "InterfaceMethod".', $method->getName()) - ); - } - - parent::addMethod($method); - } - - /** - * Sets the parent Interface. - * - * @param string $parent - * @throws BadMethodCallException if a parent is aleready set. - * - * @return void - */ - public function setParent($parent) - { - if (null === $parent) { - return; - } - - if (null !== $this->getParent()) { - throw new BadMethodCallException('Cannot set parent Parent. already set.'); - } - - $this->getImportResolver()->add($parent); - $this->parent = $parent; - } - - /** - * {@inheritdoc} - */ - protected function getTypeConstant() - { - return T_INTERFACE; - } - - /** - * prepareGenerate - * - * @return void - */ - protected function prepareGenerate() - { - if (($parent = $this->getParent())) { - if ($this->getImportResolver()->hasAlias($parent) || !$this->inNamespace($parent)) { - $this->addUseStatement($this->getImportResolver()->getImport($parent)); - } - } - } - - /** - * Get the parent Interface - * - * @return string|null - */ - public function getParent() - { - return $this->parent; - } - - /** - * getObjectBody - * - * @param Writer $writer - * - * @return Writer - */ - protected function writeObjectBody(WriterInterface $writer) - { - $this->writeConstants($writer); - - return parent::writeObjectBody($writer); - } - - /** - * writeConstants - * - * @param WriterInterface $writer - * - * @return void - */ - protected function writeConstants(WriterInterface $writer) - { - if (empty($this->constants)) { - return; - } - - foreach ($this->constants as $i => $constant) { - $writer - ->writeln($constant->generate()) - ->newline(); - } - - if (!$this->hasMethods()) { - $writer->popln(); - } - } - - /** - * hasItemsBeforeMethods - * - * @return boolean - */ - protected function hasItemsBeforeMethods() - { - return !empty($this->constants); - } - - /** - * {@inheritdoc} - */ - protected function prepareObjDoc(DocBlock $doc) - { - if (null !== $parent = $this->getParent()) { - $resolver = $this->getImportResolver(); - $doc->unshiftAnnotation( - 'see', - $resolver->hasAlias($parent) ? $resolver->getAlias($parent) : $resolver->getImport($parent) - ); - } - - $doc->unshiftAnnotation($this->getType(), $this->getName()); - } - - /** - * getExtends - * - * @return string - */ - protected function getExtends() - { - if (null === ($parent = $this->getParent())) { - return; - } - - return sprintf(' extends %s', $this->getImportResolver()->getAlias($parent)); - } - - /** - * getObjectDeclarationExtension - * - * @return string - */ - protected function getObjectDeclarationExtension() - { - return $this->getExtends(); - } -} diff --git a/lucid/writer/src/Object/MemberInterface.php b/lucid/writer/src/Object/MemberInterface.php deleted file mode 100644 index 6a5ede9..0000000 --- a/lucid/writer/src/Object/MemberInterface.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -/** - * @interface MemberInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -interface MemberInterface -{ - const IS_PUBLIC = 'public'; - const IS_PROTECTED = 'protected'; - const IS_PRIVATE = 'private'; - - const T_VOID = 'void'; - const T_STRING = 'string'; - const T_BOOL = 'boolean'; - const T_INT = 'integer'; - const T_FLOAT = 'float'; - const T_ARRAY = 'array'; - const T_MIXED = 'mixed'; -} diff --git a/lucid/writer/src/Object/Method.php b/lucid/writer/src/Object/Method.php deleted file mode 100644 index b4c672c..0000000 --- a/lucid/writer/src/Object/Method.php +++ /dev/null @@ -1,305 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\Stringable; - -/** - * @class Method - * @see MethodInterface - * @see Annotateable - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class Method extends Annotateable implements MethodInterface -{ - use Stringable; - - /** @var string */ - private $visibility; - - /** @var string */ - private $type; - - /** @var string */ - private $prefix; - - /** @var string */ - private $name; - - /** @var string */ - private $body; - - /** @var bool */ - private $abstract; - - /** @var bool */ - private $php7 = false; - - /** @var array */ - private static $magick = [ - '__clone', '__call', '__callstatic', '__construct', - '__destruct', '__invoke', '__tostring', '__get', - '__set', '__sleep', '__wakeup', '__debuginfo', - '__unset', '__isset', '__set_state', - ]; - - /** - * Constructor. - * - * @param string $name - * @param string $visibility - * @param string $type - * @param bool $static - */ - public function __construct($name, $visibility = self::IS_PUBLIC, $type = self::T_VOID, $static = false) - { - $this->name = $name; - $this->type = $type; - $this->visibility = $visibility; - $this->arguments = []; - $this->abstract = false; - $this->setStatic($static); - - parent::__construct(); - } - - /** - * Get tht method name. - * - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * Check if this method is magick. - * - * @return bool - */ - public function isMagick() - { - return in_array(strtolower($this->name), self::$magick); - } - - /** - * Check if this method is a Constructor/Destructor. - * - * @return bool - */ - public function isConstructor() - { - return in_array(strtolower($this->name), ['__constrct', '__destruct']); - } - - /** - * Set this method stattic. - * - * @param bool $static - * - * @return void - */ - public function setStatic($static) - { - $this->prefix = (bool)$static ? ' static' : ''; - } - - /** - * Set this method abstract - * - * @param bool $abstract - * - * @return void - */ - public function setAbstract($abstract) - { - $this->abstract = (bool)$abstract; - } - - /** - * Sets the method return type. - * - * @param string $type - * - * @return void - */ - public function setType($type) - { - $this->type = $type; - } - - /** - * Set the method arguments - * - * @param array $arguments an array of Argument instances. - * - * @return void - */ - public function setArguments(array $arguments) - { - $this->arguments = []; - - array_map([$this, 'addArgument'], $arguments); - } - - /** - * Add an argument - * - * @param Argument $argument - * - * @return void - */ - public function addArgument(Argument $argument) - { - $this->arguments[] = $argument; - } - - /** - * Set the method body. - * - * @param string|Writer $body - * - * @return void - */ - public function setBody($body) - { - $this->body = $body; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = false) - { - $writer = new Writer(4, true); - $writer->setOutputIndentation(1); - - $abstract = $this->abstract; - - $this->getMethodDeclaration($writer, $abstract); - - if (!$abstract) { - $writer - ->writeln('{') - ->indent() - ->writeln($this->getBody()) - ->outdent() - ->writeln('}'); - } else { - $writer - ->appendln(';'); - } - - return $raw ? $writer : $writer->dump(); - } - - /** - * getBody - * - * @return string - */ - protected function getBody() - { - if (null === $this->body) { - return; - } - - if ($this->body instanceof Writer) { - $body = $this->body; - } else { - $body = new Writer; - $body->writeln($this->body); - } - - $body->setOutputIndentation(0); - - return $body->dump(); - } - - /** - * getMethodDeclaration - * - * @param Writer $writer - * - * @return void - */ - protected function getMethodDeclaration(Writer $writer, $abstract) - { - $abs = $abstract ? 'abstract ' : ''; - - $str = '%s%s%s function %s(%s)'; - - if ($this->isPhp7Strict()) { - $str .= sprintf(' : %s', $this->type); - } - - $this->getDoc($writer, false) - ->writeln( - sprintf( - $str, - $abs, - $this->visibility, - $this->prefix, - $this->name, - $this->getArguments() - ) - ); - } - - protected function isPhp7Strict() - { - return false; - } - - /** - * getArguments - * - * @return string - */ - protected function getArguments() - { - if (empty($this->arguments)) { - return ''; - } - - $args = []; - - foreach ($this->arguments as $argument) { - $args[] = $argument->generate(); - } - - return implode(', ', $args); - } - - /** - * {@inheritdoc} - */ - protected function prepareAnnotations(DocBlock $block) - { - if (!$block->hasDescription()) { - $block->setDescription($this->name); - } - - foreach ($this->arguments as $argument) { - $block->addParam($argument->getType(), $argument->getName()); - } - - if ('__construct' !== $this->name) { - $block->setReturn($this->type); - } - } -} diff --git a/lucid/writer/src/Object/MethodInterface.php b/lucid/writer/src/Object/MethodInterface.php deleted file mode 100644 index db5f9a8..0000000 --- a/lucid/writer/src/Object/MethodInterface.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\GeneratorInterface; - -/** - * @interface MethodInterface - * @see GeneratorInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -interface MethodInterface extends GeneratorInterface -{ - /** - * Sets the methods return type. - * - * @param string $type - * - * @return void - */ - public function setType($type); - - /** - * Returns the name of the method. - * - * @return string - */ - public function getName(); - - /** - * Sets the methods arguments. - * - * @param array $arguments a list of `Argument` - * - * @return void - */ - public function setArguments(array $arguments); - - /** - * Adds an argument. - * - * @param Argument $argument - * - * @return void - */ - public function addArgument(Argument $argument); -} diff --git a/lucid/writer/src/Object/Property.php b/lucid/writer/src/Object/Property.php deleted file mode 100644 index 9385f01..0000000 --- a/lucid/writer/src/Object/Property.php +++ /dev/null @@ -1,161 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\GeneratorInterface; - -/** - * @class Property - * @see MemberInterface - * @see GeneratorInterface - * @see Annotateable - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class Property extends Annotateable implements MemberInterface, GeneratorInterface -{ - use VisibilityHelperTrait; - - /** @var string */ - private $name; - - /** @var string */ - private $visibility; - - /** @var string */ - private $prefix; - - /** @var string */ - private $type; - - /** @var string */ - private $value; - - /** @var bool */ - private $inline = false; - - /** - * Constructor. - * - * @param string $name - * @param string $visibility - * @param string $type - * @param boolean $static - */ - public function __construct($name, $visibility = self::IS_PUBLIC, $type = 'mixed', $static = false) - { - $this->setVisibility($visibility); - $this->setStatic($static); - - $this->name = $name; - $this->type = $type; - - parent::__construct(); - } - - /** - * __toString - * - * @return string - */ - public function __toString() - { - return $this->generate(); - } - - /** - * Set the property value. - * - * @param string $value - * - * @return void - */ - public function setValue($value) - { - $this->value = $value; - } - - /** - * Set the properties visibility. - * - * @param string $visibility - * - * @return void - */ - public function setVisibility($visibility) - { - $this->checkVisibility($visibility); - $this->visibility = $visibility; - } - - /** - * Set the properties type. - * - * @param string $type - * - * @return void - */ - public function setType($type) - { - $this->type = $type; - } - - /** - * Set this property to be static. - * - * @param boolean $static - * - * @return void - */ - public function setStatic($static) - { - $this->prefix = (bool)$static ? ' static' : ''; - } - - /** - * {@inheritdoc} - */ - public function generate($raw = false) - { - $this->getDoc($writer = new Writer) - ->writeln(sprintf('%s%s $%s', $this->visibility, $this->prefix, $this->name)); - - if (null !== $this->value) { - $writer->appendln(' = ' . $this->value); - } - - $writer - ->appendln(';') - ->setOutputIndentation(1); - - return $raw ? $writer : $writer->dump(); - } - - /** - * {@inheritdoc} - */ - protected function prepareAnnotations(DocBlock $block) - { - if (!$block->hasDescription()) { - $block->setInline(true); - } - - if ($block->hasAnnotations()) { - $block->unshiftAnnotation(null); - } - - $block->unshiftAnnotation('var', $this->type); - } -} diff --git a/lucid/writer/src/Object/TraitAwareWriterHelper.php b/lucid/writer/src/Object/TraitAwareWriterHelper.php deleted file mode 100644 index 23f4a3a..0000000 --- a/lucid/writer/src/Object/TraitAwareWriterHelper.php +++ /dev/null @@ -1,285 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use Lucid\Writer\WriterInterface; - -/** - * @trait TraitHelper - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -trait TraitAwareWriterHelper -{ - use ImportHelper; - - /** - * traits - * - * @var array - */ - protected $traits; - - /** - * properties - * - * @var array - */ - protected $properties; - - /** - * replacements - * - * @var array - */ - protected $replacements; - - /** - * setProperties - * - * @param array $props - * - * @return void - */ - public function setProperties(array $props) - { - foreach ($props as $prop) { - $this->addProperty($prop); - } - } - - /** - * addProperty - * - * @param Property $prop - * - * @return void - */ - public function addProperty(Property $prop) - { - $this->properties[] = $prop; - } - - /** - * addTrait - * - * @param mixed $trait - * - * @return void - */ - public function addTrait($trait) - { - $this->addToImportPool($this->traits, $trait); - } - - /** - * useTraitMethodAs - * - * @param string $trait - * @param string $method - * @param string $replacement - * - * @return void - */ - public function useTraitMethodAs($trait, $method, $replacement, $visibility = MemberInterface::IS_PUBLIC) - { - $this->replacements['trait_use_as'][] = [$trait, $method, $replacement, $visibility]; - } - - /** - * replaceTraitConflict - * - * @param string $trait - * @param string $conflict - * @param string $method - * - * @return void - */ - public function replaceTraitConflict($trait, $conflict, $method) - { - $this->replacements['trait_conflict'][] = [$trait, $conflict, $method]; - } - - /** - * writeProperties - * - * @param WriterInterface $writer - * @param ImportResolver $resolver - * - * @return void - */ - protected function writeProperties(WriterInterface $writer, ImportResolver $resolver) - { - if (empty($this->properties)) { - return; - } - - if (!empty($this->traits)) { - $writer->newline(); - } - - foreach ((array)$this->properties as $prop) { - $writer->writeln($prop->generate())->newline(); - } - - if (empty($this->methods)) { - $writer->popln(); - } - } - - /** - * writeTraits - * - * @param WriterInterface $writer - * @param ImportResolver $resolver - * @param mixed $newLine - * - * @return void - */ - protected function writeTraits(WriterInterface $writer, ImportResolver $resolver, $newLine = false) - { - if (empty($this->traits)) { - return; - } - - $traits = $this->traits; - - $writer - ->indent() - ->writeln('use ' . $resolver->getAlias(array_shift($traits))) - ->indent(); - - foreach ($traits as $trait) { - $writer - ->appendln(',') - ->writeln($resolver->getAlias($trait)); - } - - $writer->outdent(); - $this->completeTraitList($writer, $resolver); - $writer->outdent(); - } - - /** - * completeTraitList - * - * @param WriterInterface $writer - * @param ImportResolver $resolver - * - * @return void - */ - protected function completeTraitList(WriterInterface $writer, ImportResolver $resolver) - { - $useRpl = []; - $cflRpl = []; - - $replUse = isset($this->replacements['trait_use_as']); - $replCfl = isset($this->replacements['trait_conflict']); - - foreach ($this->traits as $trait) { - if ($replUse) { - $useRpl = array_merge( - $useRpl, - array_filter($this->replacements['trait_use_as'], function ($def) use ($trait) { - return AbstractWriter::trimNs($trait) === AbstractWriter::trimNs($def[0]); - }) - ); - } - - if ($replCfl) { - $cflRpl = array_merge( - $cflRpl, - array_filter($this->replacements['trait_conflict'], function ($def) use ($trait, $resolver) { - // conflicting trait exists: - $cflExists = false; - - foreach ($this->traits as $strait) { - if ($clfExists = (AbstractWriter::trimNs($strait) === AbstractWriter::trimNs($def[1]))) { - break; - } - } - - return $clfExists && (AbstractWriter::trimNs($trait) === AbstractWriter::trimNs($def[0])); - }) - ); - } - } - - if (empty($useRpl) && empty($cflRpl)) { - $writer->appendln(';'); - - return; - } - - $writer - ->appendln(' {') - ->indent(); - - - foreach ($useRpl as $urpl) { - list ($trait, $method, $replacement, $visibility) = $urpl; - - $this->writeUseReplacement($writer, $resolver->getAlias($trait), $method, $replacement, $visibility); - } - - foreach ($cflRpl as $crpl) { - list ($trait, $conflict, $method) = $crpl; - - $this->writeConflictReplacement( - $writer, - $resolver->getAlias($trait), - $method, - $resolver->getAlias($conflict) - ); - } - - $writer - ->outdent() - ->writeln('}'); - } - - /** - * writeUseReplacement - * - * @param WriterInterface $writer - * @param string $alias - * @param string $method - * @param string $replacement - * @param string $visibility - * - * @return void - */ - protected function writeUseReplacement(WriterInterface $writer, $alias, $method, $replacement, $visibility = null) - { - $visibility = $visibility ? sprintf('%s ', $visibility) : ''; - - $writer->writeln(sprintf('%s::%s as %s%s;', $alias, $method, $visibility, $replacement)); - } - - /** - * writeUseReplacement - * - * @param WriterInterface $writer - * @param string $alias - * @param string $method - * @param string $replacement - * @param string $visibility - * - * @return void - */ - protected function writeConflictReplacement(WriterInterface $writer, $alias, $method, $replacement) - { - $writer->writeln(sprintf('%s::%s insteadof %s;', $alias, $method, $replacement)); - } -} diff --git a/lucid/writer/src/Object/TraitWriter.php b/lucid/writer/src/Object/TraitWriter.php deleted file mode 100644 index cd13987..0000000 --- a/lucid/writer/src/Object/TraitWriter.php +++ /dev/null @@ -1,97 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use InvalidArgumentException; -use Lucid\Writer\WriterInterface; - -/** - * @class TraitWriter - * @see AbstractWriter - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class TraitWriter extends AbstractWriter -{ - use TraitAwareWriterHelper; - - /** - * Constructor; - * - * @param string $name - * @param string $namespace - */ - public function __construct($name, $namespace = null, $docType = self::I_NATIVE) - { - parent::__construct($name, $namespace, T_TRAIT, $docType); - - $this->traits = []; - $this->properties = []; - } - - /** - * addMethod - * - * @param MethodInterface $method - * - * @return void - */ - public function addMethod(MethodInterface $method) - { - if ($method instanceof InterfaceMethod) { - throw new InvalidArgumentException( - sprintf('Trait method "%s" must not be instance of "InterfaceMethod".', $method->getName()) - ); - } - - parent::addMethod($method); - } - - /** - * hasItemsBeforeMethods - * - * @return boolean - */ - protected function hasItemsBeforeMethods() - { - return !empty($this->traits) || !empty($this->properties); - } - - /** - * {@inheritdoc} - */ - protected function writeObjectBody(WriterInterface $writer) - { - $this->writeTraits($writer, $resolver = $this->getImportResolver()); - $this->writeProperties($writer, $resolver); - - return parent::writeObjectBody($writer); - } - - /** - * {@inheritdoc} - */ - protected function prepareObjDoc(DocBlock $block) - { - $block->unshiftAnnotation($this->getType(), $this->getName()); - } - - /** - * {@inheritdoc} - */ - protected function getImports() - { - return array_merge($this->uses, $this->traits); - } -} diff --git a/lucid/writer/src/Object/VisibilityHelperTrait.php b/lucid/writer/src/Object/VisibilityHelperTrait.php deleted file mode 100644 index b8d93ff..0000000 --- a/lucid/writer/src/Object/VisibilityHelperTrait.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Object; - -use InvalidArgumentException; - -/** - * @trait VisibilityHelperTrait - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -trait VisibilityHelperTrait -{ - /** @var array */ - private $vpool = [ - MemberInterface::IS_PUBLIC, - MemberInterface::IS_PROTECTED, - MemberInterface::IS_PRIVATE - ]; - - /** - * checkVisibility - * - * @param string $visibility - * - * @throws \InvalidArgumentException - * @return bool - */ - private function checkVisibility($visibility) - { - if (in_array($visibility, $this->vpool)) { - return true; - } - - throw new InvalidArgumentException(sprintf( - '"%s" is not a valid visibility, possible values are: %s.', - $visibility, - implode(', ', $this->vpool) - )); - } -} diff --git a/lucid/writer/src/Stringable.php b/lucid/writer/src/Stringable.php deleted file mode 100644 index 8e6b60c..0000000 --- a/lucid/writer/src/Stringable.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer; - -/** - * @trait Stringable - * @package Lucid\Writer - * @version $Id$ - */ -trait Stringable -{ - /** - * __toString - * - * @return string - */ - public function __toString() - { - return $this->generate(GeneratorInterface::RV_STRING); - } -} diff --git a/lucid/writer/src/Writer.php b/lucid/writer/src/Writer.php deleted file mode 100644 index 482e77e..0000000 --- a/lucid/writer/src/Writer.php +++ /dev/null @@ -1,341 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer; - -use Exception; -use OutOfBoundsException; -use InvalidArgumentException; - -/** - * This is the base writer that concats lines of strings to a visual block of - * text. - * - * @class Writer - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class Writer implements WriterInterface -{ - /** @var bool */ - private $ingnoreNull; - - /** @var bool */ - private $useTabs; - - /** @var bool */ - private $noTrailingSapce; - - /** @var int */ - private $indent; - - /** @var int */ - private $indentLevel; - - /** @var int */ - private $outputIndentation; - - /** @var array */ - private $lnbuff; - - /** - * Constructor. - * - * @param int $indentLevel - * @param boolean $ignoreNull - */ - public function __construct($indentLevel = 4, $ignoreNull = false) - { - $this->lnbuff = []; - $this->indent = 0; - $this->indentLevel = $indentLevel; - $this->outputIndentation = 0; - $this->useTabs = false; - $this->noTrailingSpace = true; - - $this->ignoreNull($ignoreNull); - } - - /** - * Use tabs for indentation. - * - * @api - * @return void - */ - public function useTabs() - { - $this->useTabs = true; - } - - /** - * allowTrailingSpace - * - * @api - * @return void - */ - public function allowTrailingSpace($space) - { - $this->noTrailingSpace = !(bool)$space; - } - - /** - * Ignores adding null values to Writer::writeln() is null. - * - * @param boolean $ignore - * - * @api - * @return void - */ - public function ignoreNull($ignore = false) - { - $this->ignoreNull = (bool)$ignore; - } - - /** - * Set the level of the output indentation. - * - * The default level is 0, 1 means one indent, etc. - * - * @param int $level - * - * @api - * @return void - */ - public function setOutputIndentation($level = 0) - { - $this->outputIndentation = ($this->indentLevel * $level); - } - - /** - * Get the level of the output indentation. - * - * @return int - */ - public function getOutputIndentation() - { - return $this->outputIndentation; - } - - /** - * {@inheritdoc} - */ - public function writeln($str = null) - { - if (null === $str && $this->ignoreNull) { - return $this; - } - - $this->addStr($str); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function appendln($str) - { - if ($buff = array_pop($this->lnbuff)) { - $lines = explode("\n", $str); - $this->lnbuff[] = $buff.array_shift($lines); - array_map([$this, 'writeln'], $lines); - } - - return $this; - } - - /** - * {@inheritdoc} - */ - public function popln() - { - $writer = clone $this; - $writer->lnbuff = [array_pop($this->lnbuff)]; - - return $writer; - } - - /** - * {@inheritdoc} - */ - public function replaceln($str, $index = 0) - { - $this->throwIfOutOfBounds(__METHOD__, $index); - $this->addStr($str, $index); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function removeln($index = 0) - { - $this->throwIfOutOfBounds(__METHOD__, $index); - array_splice($this->lnbuff, $index, 1); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function indent() - { - $this->indent += $this->indentLevel; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function outdent() - { - $this->indent -= $this->indentLevel; - $this->indent = max(0, $this->indent); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function newline() - { - $this->lnbuff[] = ''; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function dump() - { - $pad = $this->padString('', $this->outputIndentation); - - return preg_replace('/^\s+$/m', '', $pad.implode("\n".$pad, $this->lnbuff)); - } - - /** - * {@inheritdoc} - */ - public function __toString() - { - return $this->dump(); - } - - /** - * addStr - * - * @param mixed $str - * @param int $index - * - * @return void - */ - private function addStr($str, $index = null) - { - try { - $str = (string)$str; - } catch (Exception $e) { - throw new InvalidArgumentException('Input value must be stringable.'); - } - - foreach (explode("\n", (string)$str) as $i => $line) { - if (0 !== strlen($line)) { - $this->pushStr($line, $index ? $index + $i : null); - continue; - } - - if (null === $index) { - $this->newline(); - } else { - $this->lnbuff[$index + $i] = null; - } - } - } - - /** - * pushStr - * - * @param mixed $str - * - * @return void - */ - private function pushStr($str, $index = null) - { - $line = $this->padString($str, $this->indent); - - if ($this->noTrailingSpace) { - $line = rtrim($line); - } - - if (null !== $index) { - $this->lnbuff[(int)$index] = $line; - - return; - } - - $this->lnbuff[] = $line; - } - - /** - * indentLine - * - * @param mixed $str - * - * @return string - */ - private function padString($str, $indent = 0) - { - if ($indent === 0 || null === $str) { - return $str; - } - - return sprintf('%s%s', $this->getIndent($indent), $str); - } - - /** - * getIndent - * - * @param int $indent - * - * @return string space or tab chars - */ - private function getIndent($indent) - { - if ($this->useTabs) { - $level = $indent / $this->indentLevel; - - return str_repeat(chr(11), $level); - } - - return str_repeat(' ', $indent); - } - - /** - * throwIfOutOfBounds - * - * @param string $method - * @param int $index - * - * @return void - */ - private function throwIfOutOfBounds($method, $index) - { - if ($index < 0 || ($index + 1) > count($this->lnbuff)) { - throw new OutOfBoundsException(sprintf('%s: undefined index "%s".', $method, $index)); - } - } -} diff --git a/lucid/writer/src/WriterInterface.php b/lucid/writer/src/WriterInterface.php deleted file mode 100644 index 2eeb54c..0000000 --- a/lucid/writer/src/WriterInterface.php +++ /dev/null @@ -1,103 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer; - -/** - * @interface WriterInterface - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -interface WriterInterface -{ - /** - * Adds a line to the line stack. - * - * @param string $str - * - * @api - * @return self - */ - public function writeln($str = null); - - /** - * Inserts a blanc line to the line stack. - * - * @api - * @return Writer - */ - public function newline(); - - /** - * Concatenates the line stack into a single string. - * - * @api - * @return string - */ - public function dump(); - - /** - * Remove a line by a given index. - * - * @param int $index - * - * @api - * @throws OutOfBoundsException - * - * @return self - */ - public function removeln($index = 0); - - /** - * Replace a line at a given index. - * - * @param string $line - * @param int $index - * - * @api - * @throws OutOfBoundsException - * - * @return self - */ - public function replaceln($str, $index = 0); - - /** - * Removes the last line. - * - * popln removes the last line of the current writer instance - * and returns a new instance of writer with a singel line. - * - * @api - * @return WriterInterface a new instance of the writer object which - * contains the popped line from the current writer. - */ - public function popln(); - - /** - * Adds an indentation to the following line. - * - * @api - * @return self - */ - public function indent(); - - /** - * Removes the previous indentation. - * - * @api - * @return self - */ - public function outdent(); - - public function __toString(); -} diff --git a/lucid/writer/tests/File/JsonGeneratorTest.php b/lucid/writer/tests/File/JsonGeneratorTest.php deleted file mode 100644 index 4e57f4c..0000000 --- a/lucid/writer/tests/File/JsonGeneratorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -addContent('foo', 'bar'); - $this->assertJsonStringEqualsJsonString(json_encode(['foo' => 'bar']), $js->generate()); - $js = new JsonGenerator; - $js->setContent($data = [ - 'foo' => [ - 'bar' => [ - ] - ] - ]); - $this->assertJsonStringEqualsJsonString(json_encode($data), $js->generate()); - $js = new JsonGenerator; - $js->setContent([ - 'foo' => [ - 'bar' => [ - ] - ] - ]); - $js->addContent('foo.bar', 'baz'); - $this->assertJsonStringEqualsJsonString(json_encode(['foo' => ['bar' => 'baz']]), $js->generate()); - } -} diff --git a/lucid/writer/tests/File/PhpGeneratorTest.php b/lucid/writer/tests/File/PhpGeneratorTest.php deleted file mode 100644 index f1b2776..0000000 --- a/lucid/writer/tests/File/PhpGeneratorTest.php +++ /dev/null @@ -1,19 +0,0 @@ -addString('return '); - $gen->addArray(['foo' => 'bar']); - $gen->addString('; '); - - $ret = $gen->generate(true); - } -} diff --git a/lucid/writer/tests/FormatterHelperTest.php b/lucid/writer/tests/FormatterHelperTest.php deleted file mode 100644 index ae7f37a..0000000 --- a/lucid/writer/tests/FormatterHelperTest.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Writer\Tests; - -use Lucid\Writer\FormatterHelper; - -/** - * @class FormatterHelperTest - * @see PHPUnit_Framework_TestCase - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class FormatterTraitTest extends \PHPUnit_Framework_TestCase -{ - use FormatterHelper; - - /** @test */ - public function itShouldFormatIntents() - { - $this->assertSame(' ', $this->indent(4)); - $this->assertSame(' ', $this->indent(6)); - $this->assertSame('', $this->indent(0)); - } - - /** @test */ - public function itShouldFormatVariables() - { - - $var = ['foo' => 'bar']; - - $str = $this->extractParams($var, 0); - - $this->assertEquals("[\n 'foo' => 'bar',\n]", $str); - - $str = $this->extractParams($var, 4); - - $this->assertEquals(" [\n 'foo' => 'bar',\n ]", $str); - - $var = ['foo' => null]; - - $str = $this->extractParams($var, 0); - - $this->assertEquals("[\n 'foo' => null,\n]", $str); - - $var = ['foo' => true]; - - $str = $this->extractParams($var, 0); - - $this->assertEquals("[\n 'foo' => true,\n]", $str); - - $var = ['foo' => ['bar' => false]]; - - $str = $this->extractParams($var, 0); - - $this->assertEquals("[\n 'foo' => [\n 'bar' => false,\n ],\n]", $str); - - $var = ['foo' => '$this->doStuff()']; - - $str = $this->extractParams($var, 0); - - $this->assertEquals("[\n 'foo' => \$this->doStuff(),\n]", $str); - - //$var = ['foo' => 'bar', 'baz' => ['foof' => 'swosh']]; - - $this->assertSame("[]", $this->extractParams([])); - $this->assertSame("[\n 1,\n 2,\n 3,\n]", $this->extractParams([1, 2, 3])); - } -} diff --git a/lucid/writer/tests/Object/AbstractWriterTest.php b/lucid/writer/tests/Object/AbstractWriterTest.php deleted file mode 100644 index fb84974..0000000 --- a/lucid/writer/tests/Object/AbstractWriterTest.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -/** - * @class AbstractWriterTest - * @package Lucid\Writer - * @version $Id$ - */ -abstract class AbstractWriterTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - abstract public function itShouldBeInstantiable(); - - /** @test */ - abstract public function itShouldExplodeOnInvalidDoctype(); - - /** @test */ - public function itShouldSetNamespaceAndFqn() - { - $cwr = $this->newObw('MyObject', 'Acme\Test'); - - $this->assertSame('Acme\Test', $cwr->getNamespace(), 'Namespace should be "Acme\Test"'); - $this->assertSame('MyObject', $cwr->getName()); - $this->assertSame('\Acme\Test\MyObject', $cwr->getFqn()); - } - - - /** @test */ - public function isShouldExtractNamespaceAndFqnFromName() - { - $cwr = $this->newObw('Acme\Test\MyObject'); - - $this->assertSame('Acme\Test', $cwr->getNamespace()); - $this->assertSame('MyObject', $cwr->getName()); - $this->assertSame('\Acme\Test\MyObject', $cwr->getFqn()); - } - - /** - * newObw - * - * @param string $name - * @param string $namespace - * - * @return AbstractWriter - */ - abstract protected function newObw($name = 'MyObject', $namespace = null, $parent = null); - - - protected function getContents($file) - { - if (!is_file($path = __DIR__.'/Fixures/'.$file)) { - throw new \InvalidArgumentException($file.' is not a valid file.'); - } - - return file_get_contents($path); - } -} diff --git a/lucid/writer/tests/Object/ArgumentTest.php b/lucid/writer/tests/Object/ArgumentTest.php deleted file mode 100644 index 85701aa..0000000 --- a/lucid/writer/tests/Object/ArgumentTest.php +++ /dev/null @@ -1,103 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Argument; - -/** - * @class ArgumentTest - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class ArgumentTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldGenerateArgs() - { - $arg = new Argument('foo', 'stdClass', 'null'); - - $this->assertSame('stdClass $foo = null', $arg->generate()); - } - - /** @test */ - public function argTypesShouldBeSettable() - { - $arg = new Argument('foo'); - - $arg->setType('stdClass'); - - $this->assertSame('stdClass $foo', $arg->generate()); - } - - /** @test */ - public function defaultsShouldBeSettable() - { - $arg = new Argument('foo', 'stdClass'); - - $arg->setDefault('null'); - - $this->assertSame('stdClass $foo = null', $arg->generate()); - } - - /** @test */ - public function itShouldBeStringifiable() - { - $arg = new Argument('foo'); - $this->assertSame('$foo', (string)$arg); - } - - /** @test */ - public function itShouldHandleVariadicArguments() - { - $arg = new Argument('args'); - $arg->isVariadic(true); - - $this->assertSame('...$args', (string)$arg); - } - - /** @test */ - public function itShouldAcceptReferencedVariadics() - { - $arg = new Argument('args'); - $arg->isVariadic(true); - $arg->isReference(true); - - $this->assertSame('&...$args', (string)$arg); - } - - /** @test */ - public function itShouldTypedVariadics() - { - $arg = new Argument('args'); - $arg->setType('SomeClass'); - $arg->isVariadic(true); - - $this->assertSame('SomeClass ...$args', (string)$arg); - - $arg = new Argument('args'); - $arg->setType('SomeClass'); - $arg->isVariadic(true); - $arg->isReference(true); - - $this->assertSame('SomeClass &...$args', (string)$arg); - } - - /** @test */ - public function itShouldBeReference() - { - $arg = new Argument('foo'); - $arg->isReference(true); - $this->assertSame('&$foo', (string)$arg); - } -} diff --git a/lucid/writer/tests/Object/ClassWriterTest.php b/lucid/writer/tests/Object/ClassWriterTest.php deleted file mode 100644 index 2153c29..0000000 --- a/lucid/writer/tests/Object/ClassWriterTest.php +++ /dev/null @@ -1,196 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Method; -use Lucid\Writer\Object\Argument; -use Lucid\Writer\Object\Property; -use Lucid\Writer\Object\ClassWriter; -use Lucid\Writer\Object\InterfaceMethod; - -/** - * @class ClassWriterTest - * @see AbstractWriterTest - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class ClassWriterTest extends AbstractWriterTest -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Writer\Object\ClassWriter', $this->newObw()); - } - - /** @test */ - public function itShouldExplodeOnInvalidDoctype() - { - try { - $cwr = new ClassWriter('MyObject', 'Acme\Test', null, 44); - } catch (\InvalidArgumentException $e) { - $this->assertSame('Invalid doctype.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itItShouldIgnoreItemsWhithinSameNamespace() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - $cg->getDoc()->setDescription('I am a bloody block'); - $cg->addImport('Acme\Bar'); - - $expected = <<assertEquals($expected, $cg->generate()); - } - - /** @test */ - public function itShouldAutogenerateDocblocks() - { - $cg = $this->newObw('Foo', 'Acme'); - $str = (string)$cg->generate(); - - preg_match('#^\<\?php\n\n/\*\n\s\*#', $str, $matches); - - $this->assertArrayHasKey(0, $matches); - } - - /** @test */ - public function itShouldGenerateClasses() - { - $cg = $this->newObw('Foo', 'Acme'); - - $cg->noAutoGenerateTag(); - $cg->setParent('Acme\Bar'); - $cg->addInterface('Acme\Baz'); - - $this->assertEquals($this->getContents('class.0.php'), $cg->generate()); - } - - /** @test */ - public function itShouldBeAliasAware() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - $cg->setParent('\Acme\Lib\Bar'); - $cg->addInterface('\Acme\Interfaces\Bar as FooBar'); - - $this->assertEquals($this->getContents('class.1.php'), $cg->generate()); - } - - /** @test */ - public function itShouldHaveProperties() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - - $cg->setProperties([ - new Property('bar', Property::IS_PRIVATE), - $p = new Property('baz', Property::IS_PUBLIC, 'string') - ]); - - $p->setValue("'baz'"); - - $this->assertEquals($this->getContents('class.3.php'), $cg->generate()); - } - - /** @test */ - public function itShouldAddTraits() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - $cg->setParent('\Acme\Bar'); - $cg->addInterface('\Acme\Baz'); - $cg->addTrait('\Acme\Traits\FooTrait'); - $cg->addTrait('\Acme\Traits\BarTrait'); - - $this->assertEquals($this->getContents('class.2.php'), $cg->generate()); - } - - /** @test */ - public function itShouldAddTraitReplacements() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - $cg->setParent('\Acme\Bar'); - $cg->addInterface('\Acme\Baz'); - $cg->addTrait('\Acme\Traits\FooTrait'); - $cg->addTrait('\Acme\Traits\BarTrait'); - - $cg->useTraitMethodAs('\Acme\Traits\FooTrait', 'bar', 'baz', 'private'); - $cg->replaceTraitConflict('\Acme\Traits\BarTrait', '\Acme\Traits\FooTrait', 'foo'); - - $this->assertEquals($c = $this->getContents('class.2.1.php'), $cg->generate()); - } - - /** @test */ - public function itShouldHaveMethods() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - - $cg->setMethods([$m = new Method('__construct')]); - $m->addArgument(new Argument('bar', 'Bar')); - - $c = $this->getContents('class.4.php'); - $this->assertEquals($c = $this->getContents('class.4.php'), $cg->generate()); - } - - /** @test */ - public function itShouldBeAbstract() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - $cg->setAbstract(true); - - $this->assertEquals($c = $this->getContents('class.5.php'), $cg->generate()); - } - - /** - * @test - * @expectedException \InvalidArgumentException - */ - public function itShouldThrowIfAddingInterfaceMethod() - { - $cg = $this->newObw('Foo', 'Acme'); - $cg->noAutoGenerateTag(); - - $cg->addMethod($m = new InterfaceMethod('__construct')); - } - - protected function newObw($name = 'MyClass', $namespace = null, $parent = null) - { - return new ClassWriter($name, $namespace, $parent); - } -} diff --git a/lucid/writer/tests/Object/CommentBlockTest.php b/lucid/writer/tests/Object/CommentBlockTest.php deleted file mode 100644 index 70bb102..0000000 --- a/lucid/writer/tests/Object/CommentBlockTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertInstanceOf('Lucid\Writer\Object\DocBlock', new CommentBlock); - } - - /** @test */ - public function itShouldWriteCommentBlock() - { - $comment = new CommentBlock; - $comment->addAnnotation('foo', 'bar'); - - $this->assertSame("/*\n * @foo bar\n */", (string)$comment->generate()); - } - - /** @test */ - public function itShouldInlineComments() - { - $comment = new CommentBlock; - $comment->addAnnotation('foo', 'bar'); - $comment->setInline(true); - - $this->assertSame("/* @foo bar */", (string)$comment->generate()); - } -} diff --git a/lucid/writer/tests/Object/ConstantTest.php b/lucid/writer/tests/Object/ConstantTest.php deleted file mode 100644 index 8d5bca6..0000000 --- a/lucid/writer/tests/Object/ConstantTest.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Constant; - -/** - * @class ConstantTest - * @package Lucid\Writer\Tests\Object - * @version $Id$ - */ -class ConstantTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldCompileToConstnatString() - { - $this->assertSame(" /** @var string */\n const FOO = '12';", (new Constant('foo', '12'))->generate()); - $this->assertSame(" /** @var int */\n const FOO = 12;", (new Constant('foo', '12', 'int'))->generate()); - } -} diff --git a/lucid/writer/tests/Object/DocBlockTest.php b/lucid/writer/tests/Object/DocBlockTest.php deleted file mode 100644 index 5479705..0000000 --- a/lucid/writer/tests/Object/DocBlockTest.php +++ /dev/null @@ -1,199 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\DocBlock; - -/** - * @class DocBlockTest - * @package Lucid\Writer - * @version $Id$ - */ -class DocBlockTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldGenerateBlockComments() - { - $this->assertSame("/**\n */", (new DocBlock)->generate()); - } - - /** @test */ - public function itShouldAddDescription() - { - $doc = new DocBlock; - $doc->setDescription('test'); - - $this->assertSame("/**\n * test\n */", $doc->generate()); - } - - /** @test */ - public function itShouldAddLongDescription() - { - $expected = <<setDescription('test'); - $doc->setLongDescription("A\nB"); - - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldAddAnnotations() - { - $expected = <<addAnnotation('name', 'foo'); - $doc->addParam('string', 'bar'); - - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldWriteFullBlock() - { - - $expected = <<setDescription('Foo'); - $doc->setLongDescription("Bar\nBaz"); - $doc->addAnnotation('name', 'foo'); - $doc->addParam('string', 'bar'); - $doc->setReturn('string|null', 'description'); - - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldWriteDescAndReturn() - { - - $expected = <<setDescription('Foo'); - $doc->setReturn('void'); - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldWriteNewLinesOnAnnotations() - { - - $expected = <<setAnnotations([ - ['name', 'foo'], - null, - ['name', 'bar'] - ]); - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldInlineBlocks() - { - $expected = '/** @test */'; - - $doc = new DocBlock; - $doc->addAnnotation('test'); - $doc->setInline(true); - - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldOverrideInlineIfDiscriptionOrAnnotations() - { - - $expected = <<setAnnotations([ - ['foo', 'foo'], - ['bar', 'bar'] - ]); - $doc->setInline(true); - $this->assertSame($expected, $doc->generate()); - - $expected = <<setDescription('FooBar'); - $doc->setAnnotations([ - ['bar', 'bar'] - ]); - $doc->setInline(true); - $this->assertSame($expected, $doc->generate()); - } - - /** @test */ - public function itShouldGetDescription() - { - $doc = new DocBlock; - $doc->setDescription('FooBar'); - - $this->assertSame('FooBar', $doc->getDescription()); - } - - /** @test */ - public function itShouldGetLongDescription() - { - $doc = new DocBlock; - $doc->setLongDescription('Long description.'); - - $this->assertSame('Long description.', $doc->getLongDescription()); - } -} diff --git a/lucid/writer/tests/Object/Fixures/class.0.php b/lucid/writer/tests/Object/Fixures/class.0.php deleted file mode 100644 index 1365fa4..0000000 --- a/lucid/writer/tests/Object/Fixures/class.0.php +++ /dev/null @@ -1,12 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\ImportResolver; - -/** - * @class ImportResolverTest - * @package Lucid\Writer - * @version $Id$ - */ -class ImportResolverTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itIsExpectedThat() - { - $res = new ImportResolver(); - $res->add('\Foo\Bar'); - $res->add('\Faz\Bar'); - - $this->assertSame('FazBar', $res->getAlias('Faz\Bar')); - - $res = new ImportResolver(); - $res->add('\Foo'); - $res->add('\Acme\Foo'); - - $this->assertSame('Foo', $res->getAlias('Foo')); - $this->assertSame('AcmeFoo', $res->getAlias('Acme\Foo')); - - $res = new ImportResolver(); - $res->add('\Acme\Foo'); - $res->add('\Foo'); - - $this->assertSame('Foo', $res->getAlias('Acme\Foo')); - $this->assertSame('FooAlias', $res->getAlias('Foo')); - } - - /** @test */ - public function itShouldGetImportsAndAlias() - { - $res = new ImportResolver(); - - $this->assertSame('Foo\Bar', $res->getAlias('Foo\Bar')); - - $res->add('\Foo\Bar'); - - $this->assertSame('Foo\Bar', $res->getImport('Foo\Bar')); - $this->assertSame('Bar', $res->getAlias('Foo\Bar')); - - $res->add('\Faz\Bar'); - - $this->assertSame('Faz\Bar as FazBar', $res->getImport('Faz\Bar')); - $this->assertSame('baz as bar', $res->getImport('baz as bar')); - } - - /** @test */ - public function itShouldHandleAsStatements() - { - $res = new ImportResolver(); - - $res->add('\Lunar\Foo as FooAlias'); - $res->add('\Acme\Foo'); - $res->add('\Foo'); - - $this->assertSame('FooAlias', $res->getAlias('Lunar\Foo')); - $this->assertSame('AcmeFoo', $res->getAlias('Acme\Foo')); - $this->assertSame('FooAliasAlias', $res->getAlias('Foo')); - - $res = new ImportResolver(); - - $res->add('\Acme\Foo'); - $res->add('\Lunar\Foo as FooAlias'); - $res->add('\Foo'); - - $this->assertSame('Foo', $res->getAlias('Acme\Foo')); - $this->assertSame('FooAlias', $res->getAlias('Lunar\Foo')); - $this->assertSame('FooAliasAlias', $res->getAlias('Foo')); - - $res = new ImportResolver(); - - $res->add('\Acme\Foo'); - $res->add('\Foo'); - $res->add('\Lunar\Foo as FooAlias'); - - $this->assertSame('Foo', $res->getAlias('Acme\Foo')); - $this->assertSame('FooAlias', $res->getAlias('Foo')); - $this->assertSame('LunarFoo', $res->getAlias('Lunar\Foo')); - } - - /** @test */ - public function itIsExpectedThatIt() - { - $res = new ImportResolver(); - - $res->add('\Lunar\Foo as FooAlias'); - - $this->assertSame('FooAlias', $res->getAlias('\Lunar\Foo as FooAlias')); - $this->assertSame('Lunar\Foo as FooAlias', $res->getImport('\Lunar\Foo as FooAlias')); - $this->assertSame('Lunar\Bar as BarAlias', $res->getImport('\Lunar\Bar as BarAlias')); - } -} diff --git a/lucid/writer/tests/Object/InterfaceMethodTest.php b/lucid/writer/tests/Object/InterfaceMethodTest.php deleted file mode 100644 index 5017976..0000000 --- a/lucid/writer/tests/Object/InterfaceMethodTest.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\InterfaceMethod; - -/** - * @class InterfaceMethodTest - * @see \PHPUnit_Framework_TestCase - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class InterfaceMethodTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldThrowExcetptionWhenSetToAbstract() - { - $im = new InterfaceMethod('getBar'); - - try { - $im->setAbstract(false); - } catch (\LogicException $e) { - $this->fail('setting abstract to false should cause no side effect.'); - } - - try { - $im->setAbstract(true); - } catch (\LogicException $e) { - $this->assertSame('Cannot set interface method abstract.', $e->getMessage()); - } - } - - /** @test */ - public function itShouldThrowExcetptionWhenBodyIsSet() - { - $im = new InterfaceMethod('getBar'); - - try { - $im->setBody('return null;'); - } catch (\LogicException $e) { - $this->assertSame('Cannot set a method body on an interface method.', $e->getMessage()); - } - } - - /** @test */ - public function itShouldWriteInterfaceMethod() - { - $expected = <<assertSame($expected, $im->generate()); - } -} diff --git a/lucid/writer/tests/Object/InterfaceWriterTest.php b/lucid/writer/tests/Object/InterfaceWriterTest.php deleted file mode 100644 index 72e52b5..0000000 --- a/lucid/writer/tests/Object/InterfaceWriterTest.php +++ /dev/null @@ -1,152 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Constant; -use Lucid\Writer\Object\InterfaceWriter; -use Lucid\Writer\Object\InterfaceMethod; - -/** - * @class InterfaceWriterTest - * @see AbstractWriterTest - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class InterfaceWriterTest extends AbstractWriterTest -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Writer\Object\AbstractWriter', new InterfaceWriter('MyObject')); - } - - /** @test */ - public function itShouldExplodeOnInvalidDoctype() - { - try { - $cwr = new InterfaceWriter('MyObject', 'Acme\Test', null, 44); - } catch (\InvalidArgumentException $e) { - $this->assertSame('Invalid doctype.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldDisallowClassMethods() - { - $cwr = $this->newObw('Acme\FooInterface'); - $m = $this->getMockbuilder('Lucid\Writer\Object\MethodInterface') - ->disableOriginalConstructor() - ->getMock(); - $m->method('getName')->willReturn('foo'); - try { - $cwr->addMethod($m); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('Method "foo" must be instance of "InterfaceMethod".', $e->getMessage()); - } - } - - /** @test */ - public function itShouldNotSetParent() - { - $cwr = $this->newObw('FooInterface', 'Acme', 'BarInterface'); - try { - $cwr->setParent('Lube'); - } catch (\BadMethodCallException $e) { - $this->assertEquals('Cannot set parent Parent. already set.', $e->getMessage()); - } - } - - /** @test */ - public function itShouldBeExtendable() - { - $cwr = $this->newObw('Acme\FooInterface'); - $cwr->noAutoGenerateTag(); - $cwr->setParent('\Acme\BarInterface'); - - $this->assertEquals($this->getContents('interface.0.php'), $cwr->generate()); - } - - /** @test */ - public function itShouldHaveConstants() - { - $cwr = $this->newObw('Acme\FooInterface'); - $cwr->noAutoGenerateTag(); - - $cwr->setConstants([ - new Constant('t_foo', '12', 'int'), - new Constant('t_bar', '13', 'string') - ]); - - $this->assertEquals($this->getContents('interface.4.php'), $cwr->generate()); - } - - /** @test */ - public function itShouldHaveConstantsAndMethods() - { - $cwr = $this->newObw('Acme\FooInterface'); - $cwr->noAutoGenerateTag(); - - $cwr->setConstants([ - new Constant('t_foo', '12', 'int') - ]); - - $cwr->addMethod(new InterfaceMethod('setFoo')); - $cwr->addMethod(new InterfaceMethod('setBar')); - - $this->assertEquals($this->getContents('interface.4.1.php'), $res = $cwr->generate()); - } - - /** @test */ - public function itShouldWriteNewLineBetweenConstantAndMethod() - { - $expected = <<newObw('Acme\Foo'); - $cwr->noAutoGenerateTag(); - $cwr->addConstant(new Constant('MY_CONST', 1, 'int')); - $cwr->addMethod(new InterfaceMethod('getMyConst', 'int')); - - $this->assertSame($expected, (string)$cwr->generate()); - } - - protected function newObw($name = 'MyObject', $namespace = null, $parent = null) - { - return new InterfaceWriter($name, $namespace, $parent); - } -} diff --git a/lucid/writer/tests/Object/MethodTest.php b/lucid/writer/tests/Object/MethodTest.php deleted file mode 100644 index 4200150..0000000 --- a/lucid/writer/tests/Object/MethodTest.php +++ /dev/null @@ -1,222 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Writer; -use Lucid\Writer\Object\Method; -use Lucid\Writer\Object\Argument; - -/** - * @class MethodTest - * @package Lucid\Writer - * @version $Id$ - */ -class MethodTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeStringifiable() - { - $method = new Method('setFoo'); - - $expected = <<assertSame($expected, (string)$method); - } - - /** @test */ - public function itShouldAddArguments() - { - $method = new Method('setFoo'); - - $expected = <<addArgument(new Argument('foo', 'stdClass')); - $this->assertSame($expected, $method->generate()); - - $method = new Method('setFoo'); - - $method->setArguments([new Argument('foo', 'stdClass')]); - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldAllowToAddComments() - { - $method = new Method('getFoo'); - - $expected = <<setDescription("Foo\nBar"); - - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldNotAddReturnsOnConstructors() - { - $method = new Method('__construct'); - - $expected = <<setDescription('Constructor.'); - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itsTypeShouldBeSettable() - { - - $method = new Method('getFoo'); - - $expected = <<setType(Method::T_STRING); - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldSetItsBody() - { - $method = new Method('getFoo'); - - $expected = <<setBody('return;'); - $this->assertSame($expected, $method->generate()); - - $method = new Method('getFoo'); - $method->setBody((new Writer)->writeln('return;')); - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldAddAnnotations() - { - $expected = << - * @param stdClass \$foo - * - * @return void - */ - public function getFoo(stdClass \$foo) - { - return; - } -PHP; - $method = new Method('getFoo'); - $method->setBody('return;'); - $method->setArguments([new Argument('foo', 'stdClass')]); - $method->addAnnotation('author', 'Thomas Appel '); - $method->setDescription('This is doc a comment.'); - $method->setLongDescription('This is the long description.'); - - $this->assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldWriteLongDesc() - { - $method = new Method('setFoo'); - $method->addParam('string', 'bar', 'nonesense'); - $method->addArgument(new Argument('foo', 'string')); - - $expected = <<assertSame($expected, $method->generate()); - } - - /** @test */ - public function itShouldWriteAbstract() - { - - $expected = <<setAbstract(true); - $this->assertSame($expected, $method->generate()); - } -} diff --git a/lucid/writer/tests/Object/PropertyTest.php b/lucid/writer/tests/Object/PropertyTest.php deleted file mode 100644 index adfe3a3..0000000 --- a/lucid/writer/tests/Object/PropertyTest.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Property; - -/** - * @class PropertyTest - * @package Lucid\Writer - * @version $Id$ - */ -class PropertyTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldCreateAClassProperty() - { - $prop = new Property('foo'); - - $expected = <<assertSame($expected, (string)$prop); - } - - /** @test */ - public function itShouldHaveAnInitialValueAndType() - { - $prop = new Property('foo'); - - $expected = <<setValue("'foo'"); - $prop->setType(Property::T_STRING); - $prop->setVisibility(Property::IS_PRIVATE); - $this->assertSame($expected, (string)$prop); - } - - /** @test */ - public function itIsExpectedThat() - { - $expected = <<setDescription('Acme\Foo\Wahtever'); - $prop->addAnnotation('ORM\Column(type="integer")'); - $prop->addAnnotation('ORM\Id'); - $prop->addAnnotation('ORM\GeneratedValue(strategy="AUTO")'); - - $this->assertSame($expected, (string)$prop); - } -} diff --git a/lucid/writer/tests/Object/TraitWriterTest.php b/lucid/writer/tests/Object/TraitWriterTest.php deleted file mode 100644 index 3cae98d..0000000 --- a/lucid/writer/tests/Object/TraitWriterTest.php +++ /dev/null @@ -1,95 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\Method; -use Lucid\Writer\Object\Property; -use Lucid\Writer\Object\TraitWriter; - -/** - * @class ConstantTest - * @package Lucid\Writer - * @version $Id$ - */ -class TraitWriterTest extends AbstractWriterTest -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('Lucid\Writer\Object\TraitWriter', $this->newObw()); - } - - /** @test */ - public function itShouldExplodeOnInvalidDoctype() - { - try { - $cwr = new TraitWriter('MyObject', 'Acme\Test', 44); - } catch (\InvalidArgumentException $e) { - $this->assertSame('Invalid doctype.', $e->getMessage()); - - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldWriteSimpleTraits() - { - $tw = $this->newObw('FooTrait', 'Acme\Traits'); - $tw->noAutoGenerateTag(); - - $tw->addMethod(new Method('setThing')); - $this->assertSame($this->getContents('trait.0.php'), (string)$tw->generate()); - } - - /** @test */ - public function itShouldRejectInterfaceMethods() - { - $tw = $this->newObw('FooTrait', 'Acme\Traits'); - $m = $this->getMockbuilder('Lucid\Writer\Object\InterfaceMethod') - ->disableOriginalConstructor() - ->getMock(); - $m->method('getName')->willReturn('foo'); - - try { - $tw->addMethod($m); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('Trait method "foo" must not be instance of "InterfaceMethod".', $e->getMessage()); - } - } - - /** @test */ - public function itShouldCheckItemsBeforeMethods() - { - } - - /** @test */ - public function itShouldCompileToConstnatString() - { - $tw = $this->newObw('FooTrait', 'Acme\Traits'); - $tw->noAutoGenerateTag(); - - $tw->addProperty(new Property('foo')); - $tw->addTrait('Acme\Traits\BarTrait'); - $tw->addTrait('Acme\Test\HelperTrait'); - $tw->useTraitMethodAs('Acme\Traits\BarTrait', 'getFoo', 'bla'); - $tw->replaceTraitConflict('Acme\Traits\BarTrait', 'Acme\Test\HelperTrait', 'retrieve'); - - $this->assertSame($this->getContents('trait.1.php'), (string)$tw->generate()); - } - - protected function newObw($name = 'MyObject', $namespace = null, $parent = null) - { - return new TraitWriter($name, $namespace); - } -} diff --git a/lucid/writer/tests/Object/VisibilityHelperTraitTest.php b/lucid/writer/tests/Object/VisibilityHelperTraitTest.php deleted file mode 100644 index 6d9239c..0000000 --- a/lucid/writer/tests/Object/VisibilityHelperTraitTest.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests\Object; - -use Lucid\Writer\Object\VisibilityHelperTrait; - -/** - * @class VisibilityHelperTraitTest - * @see \PHPUnit_Framework_TestCase - * - * @package Lucid\Writer - * @version $Id$ - * @author iwyg - */ -class VisibilityHelperTraitTest extends \PHPUnit_Framework_TestCase -{ - use VisibilityHelperTrait; - - /** @test */ - public function itShouldExplode() - { - try { - $this->checkVisibility('translucent'); - } catch (\InvalidArgumentException $e) { - $this->assertEquals( - '"translucent" is not a valid visibility, possible values are: public, protected, private.', - $e->getMessage() - ); - } - } -} diff --git a/lucid/writer/tests/WriterTest.php b/lucid/writer/tests/WriterTest.php deleted file mode 100644 index c884c90..0000000 --- a/lucid/writer/tests/WriterTest.php +++ /dev/null @@ -1,190 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Writer\Tests; - -use Lucid\Writer\Writer; - -/** - * @class WriterTest - * - * @package Lucid\Writer - * @version $Id$ - * @author Thomas Appel - */ -class WriterTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldUseSpacesForIndentation() - { - $wr = new Writer; - $wr - ->writeln('foo') - ->indent() - ->writeln('bar'); - - $this->assertSame("foo\n bar", $wr->dump()); - } - - /** @test */ - public function itShouldBeStringable() - { - - $this->assertSame('', (string)(new Writer)); - } - - /** @test */ - public function itShouldBeAbleToUseTabs() - { - $wr = new Writer; - $tab = chr(11); - $wr->useTabs(); - - $wr - ->writeln('foo') - ->indent() - ->writeln('bar'); - - $this->assertSame("foo\n".$tab."bar", $wr->dump()); - } - - /** @test */ - public function itShouldBeAbleToRemoveLines() - { - $wr = new Writer; - $wr - ->writeln('foo') - ->writeln('bar') - ->writeln('baz'); - - $wr->removeln(1); - - $this->assertSame("foo\nbaz", $wr->dump()); - } - - /** @test */ - public function itShouldPopLines() - { - $wr = new Writer; - $wr - ->writeln('foo') - ->writeln('bar') - ->writeln('baz'); - - $ln = $wr->popln(); - - $this->assertInstanceOf('Lucid\Writer\WriterInterface', $ln); - $this->assertFalse($wr === $ln); - $this->assertSame("foo\nbar", $wr->dump()); - } - - /** @test */ - public function itShouldAbleToReplaceLines() - { - $wr = new Writer; - $wr - ->writeln('foo') - ->writeln('bar') - ->writeln('baz'); - - $wr->replaceln('bam', 1); - - $this->assertSame("foo\nbam\nbaz", $wr->dump()); - } - - /** @test */ - public function itShouldThrowErrorWhenIndexIsWronOnReplace() - { - $wr = new Writer; - $wr - ->writeln('foo'); - - try { - $wr->replaceln('bar', 1); - } catch (\OutOfBoundsException $e) { - $this->assertSame('Lucid\Writer\Writer::replaceln: undefined index "1".', $e->getMessage()); - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldThrowErrorWhenIndexIsWronOnRemove() - { - $wr = new Writer; - $wr - ->writeln('foo'); - - try { - $wr->removeln(1); - } catch (\OutOfBoundsException $e) { - $this->assertSame('Lucid\Writer\Writer::removeln: undefined index "1".', $e->getMessage()); - return; - } - - $this->fail(); - } - - /** @test */ - public function itShouldAllowTrailingSpaces() - { - $wr = new Writer; - $wr - ->writeln('foo '); - - $this->assertSame('foo', $wr->dump()); - - $wr = new Writer; - $wr->allowTrailingSpace(true); - $wr - ->writeln('foo '); - - $this->assertSame('foo ', $wr->dump()); - } - - /** @test */ - public function defaultOutputIndentShouldBeZero() - { - $wr = new Writer; - - $this->assertSame(0, $wr->getOutputIndentation()); - } - - /** @test */ - public function itShouldAddNoExtraSpace() - { - $wr = new Writer; - - $wr->writeln('foo') - ->writeln('bar') - ->indent() - ->replaceln('', 1) - ->writeln('baz'); - - $this->assertSame("foo\n\n baz", $wr->dump()); - } - - /** @test */ - public function itShouldThrowExceptionOnNoneStrangableValues() - { - $wr = new Writer; - - try { - $wr->writeln([]); - } catch (\InvalidArgumentException $e) { - $this->assertSame('Input value must be stringable.', $e->getMessage()); - return; - } - - $this->fail(); - } -} diff --git a/lucid/xml/.coveralls.yml b/lucid/xml/.coveralls.yml deleted file mode 100644 index 8f3ccf8..0000000 --- a/lucid/xml/.coveralls.yml +++ /dev/null @@ -1,2 +0,0 @@ -coverage_clover: coverage/clover.xml -json_path: coverage/coveralls.json diff --git a/lucid/xml/.travis.yml b/lucid/xml/.travis.yml deleted file mode 100644 index 5feedc8..0000000 --- a/lucid/xml/.travis.yml +++ /dev/null @@ -1,38 +0,0 @@ -sudo: false - -language: php - -matrix: - fast_finish: true - include: - - php: 5.6 - env: - - CS_CHECK_ENABLED: true - - php: 7.0 - env: - - CODE_COVERAGE_LOG: true - - php: hhvm - allow_failures: - - php: hhvm - -before_install: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi - - composer self-update - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then composer require --no-update satooshi/php-coveralls:dev-master; fi - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then composer require squizlabs/php_codesniffer:~2.5; fi - -install: - - composer install --prefer-source --no-interaction - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then mkdir -p coverage; fi - -script: - - if [[ $CODE_COVERAGE_LOG != 'true' ]]; then php vendor/bin/phpunit --verbose; fi; - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/phpunit --coverage-clover coverage/clover.xml; fi; - - if [[ $CS_CHECK_ENABLED == 'true' ]]; then php vendor/bin/phpcs --standard=PSR2 --ignore=vendor/* src tests; fi - -after_script: - - if [[ $CODE_COVERAGE_LOG == 'true' ]]; then php vendor/bin/coveralls; fi - -notififation: - on_success: never - on_failure: always diff --git a/lucid/xml/LICENSE.md b/lucid/xml/LICENSE.md deleted file mode 100644 index 95729d0..0000000 --- a/lucid/xml/LICENSE.md +++ /dev/null @@ -1,7 +0,0 @@ -Copyright (c) 2013-2014 Thomas Appel - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lucid/xml/README.md b/lucid/xml/README.md deleted file mode 100644 index 0d9f495..0000000 --- a/lucid/xml/README.md +++ /dev/null @@ -1,389 +0,0 @@ -# XML writer and parser utilities - -[![Author](http://img.shields.io/badge/author-iwyg-blue.svg?style=flat-square)](https://github.com/iwyg) -[![Source Code](http://img.shields.io/badge/source-lucid/signal-blue.svg?style=flat-square)](https://github.com/lucidphp/xml/tree/develop) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/lucidphp/xml/blob/develop/LICENSE.md) - -[![Build Status](https://img.shields.io/travis/lucidphp/xml/develop.svg?style=flat-square)](https://travis-ci.org/lucidphp/xml) -[![Code Coverage](https://img.shields.io/coveralls/lucidphp/xml/develop.svg?style=flat-square)](https://coveralls.io/r/lucidphp/xml) -[![HHVM](https://img.shields.io/hhvm/lucid/xml/dev-develop.svg?style=flat-square)](http://hhvm.h4cc.de/package/lucid/xml) - -## Installing - -```bash -$ composer require lucid/xml -``` - -## Testing - -Run tests with: - -```bash -$ ./vendor/bin/phpunit -``` - -## The Parser - -The `Parser` class can parse xml string, files, DOMDocuments, and DOMElements -into a php array. - -### Parsing xml strings -```php -parse('bar'); - -``` - -### Parsing xml files - -```php -parse('/path/to/data.xml'); - -``` - -### Parsing a `DOMDocument` - -```php -parseDom($dom); - -``` - -### Parsing a `DOMElement` - -```php -parseDomElement($element); - -``` - -## Parser Options - -### Dealing with Attributes -Xml attributes are captured as an deticated section within the actual node data. -The section key defaults to `@attributes`, but can be changed using the `setAttributesKey` method. - -```php -some text' - -$parser = new Parser; -$parser->setAttributesKey('__attrs__'); -$parser->parse($xml); -``` - -```php -['data' => ['node' => ['__attrs__' => ['id' => 1], 'value' => 'some text']]]; -``` -#### Merging attributes -Setting `Parser::mergeAttributes(true)` will merge any attributes as key/value -into the data set. - -```php -$parser->setMergeAttributes(true); -$parser->parse($xml); -``` -The above example will output something simmilar to this: - -```php -['data' => ['node' => ['id' => 1, 'value' => 'some text']]]; -``` - -### Normalizing keys - -You may specifay how keys are being transformed by setting a key normalizer callback. - -The default normalizer transforms dashes to underscores and camelcase to snakecase notation. - -```php -setKeyNormalizer(function ($key) { - // do string transfomations - return $key; -}); - -$parser->parseDomElement($element); - -``` - -### Set index key - -This forces the parser to treat nodes with a nodeName of the given key to be -handled as list. - - -```php -setIndexKey('item'); - -``` - -### Set a pluralizer - -By default the parser will parse xml structures like - - -```xml - - 1 - 2 - - -``` - -To something like: - -```php - ['entry' => [1, 2]]] - -``` - -Setting a pluralizer can fix this. - -Note, that a pluralizer can be any [callable](http://www.php.net/manual/en/language.types.callable.php) that takes a string and returns -a string. - -```php -setPluralizer(function ($string) { - if ('entry' === $string) { - return 'entries'; - } -}); - -``` - -```php - [1, 2]] -``` - -## The Writer - -### Dumping php data to a xml string - -```php - 'bar' -]; - -$writer->dump($data); // bar - -// set the xml root node name: - -$writer->dump($data, 'data'); // bar - -``` - -### Dumping php data to a DOMDocument - -Note: this will create an instance of `Lucid\Xml\Dom\DOMDocument`. - -```php - - 'bar' -]; - -$dom = $writer->writeToDom($data); - -``` - -## Writer options - -### Set the normalizer instance - -Normaly, the `NormalizerInterface` implementation is set for you when instantiating a new `Writer`, however you can set your own normalizer instance. - -Note: the normalizer must implement the `Lucid\Xml\Normalizer\NormalizerInterface` interface. - -```php -setNormalizer($myNormalizer); -``` - -### Set the inflector - -The inflector is the exact oppoite of the Parser's pluralizer. It singularizes -strings. - - -```php -setInflector(function ($string) { - if ('items' === $string) { - return 'item'; - } -}); - -``` - -### Set the document encoding - -Default encoding is `UTF-8`. - -```php -setEncoding($encoding); // string -``` - -### Set an attribute key map - -This is usefull if you want to output certain keys as xml attribute - -```php -setKeyMap([ - 'nodeName' => ['id', 'entry'] // nested keys 'id' and 'entry' of the key - element 'nodeName' will be set as attributes instead of childnodes. -]); - -``` -Note: you can also use use `addMappedAttribute($nodeName, $attributeName)` to add more mapped attributes. - -### Set value keys - -```php - [ - '@attributes' => [ - 'bar' => 'baz' - ], - 'value' => 'tab' - ] -]; -``` - -The data structure above would dump the following xml string - -```xml -tab -``` - -However, if you need the value node as actual value of the parent node, you may -use `Writer::useKeyAsValue(string $key)` to do so - -```php -useKeyAsValue('value'); - -$writer->dump($data); -``` - -now dumps: -```xml -tab -``` - -### Writing indexed array structure - -Indexed arrays will create xml structures like the example below: - -```php -$data = ['data' => [1, 2, 3]]; -$writer->dump($data); -``` - -```xml - - 1 - 2 - 3 - -``` - -You can change the node names associated with indexed items by using the -`useKeyAsIndex(string $key)` method. - -```php -$writer->useKeyAsIndex('thing'); -$writer->dump($data); -``` - -```xml - - 1 - 2 - 3 - -``` - -### Writing arrays with none valid index keys - -Arrays containing invalid indices (e.g. unordererd lists) will be treated -slightly different. - -```php -$data = ['data' => [1 => 'foo', 4 => 'bar', 3 => 'baz']]; -$writer->dump($data); -``` - -```xml - - foo - bar - baz - -``` diff --git a/lucid/xml/composer.json b/lucid/xml/composer.json deleted file mode 100644 index 7462714..0000000 --- a/lucid/xml/composer.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "lucid/xml", - "license": "MIT", - "description": "Xml writing and parsing utilities", - "keywords": ["xml", "parser"], - "authors": [ - { - "name": "iwyg", - "email": "mail@thomas-appel.com" - } - ], - "require": { - "php":">=5.6.0", - "lucid/common":"dev-master" - }, - "require-dev": { - "phpunit/phpunit": "5.2.*@dev" - }, - "autoload": { - "psr-4": { - "Lucid\\Xml\\":"src/" - } - }, - "autoload-dev": { - "psr-4": { - "Lucid\\Xml\\Tests\\":"tests/" - } - }, - "minimum-stability": "dev" -} diff --git a/lucid/xml/phpunit.xml.dist b/lucid/xml/phpunit.xml.dist deleted file mode 100644 index ab9e4bd..0000000 --- a/lucid/xml/phpunit.xml.dist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - ./tests/ - - - - - ./vendor - ./tests - - - ./src - - - diff --git a/lucid/xml/src/Dom/DOMDocument.php b/lucid/xml/src/Dom/DOMDocument.php deleted file mode 100644 index 7743a6e..0000000 --- a/lucid/xml/src/Dom/DOMDocument.php +++ /dev/null @@ -1,136 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Dom; - -use DOMNode; -use DOMXpath; -use DOMDocument as Document; -use DOMElement as XmlElement; - -/** - * @class DOMDocument extends BaseDom - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -class DOMDocument extends Document -{ - /** - * xpath - * - * @var DOMXpath - */ - protected $xpath; - protected $nodeClasses; - - /** - * Constructor. - * - * @param mixed $version - * @param mixed $encoding - */ - public function __construct($version = null, $encoding = null) - { - $this->nodeClasses = []; - parent::__construct($version, $encoding); - $this->registerNodeClass('DOMElement', 'Lucid\Xml\Dom\DOMElement'); - } - - /** - * {@inheritdoc} - */ - public function registerNodeClass($baseClass, $extendClass) - { - $this->nodeClasses[$a = ltrim($baseClass, '\\')] = ltrim($b = $extendClass, '\\'); - - return parent::registerNodeClass($a, $b); - } - - /** - * {@inheritdoc} - */ - public function createElement($name, $content = null) - { - return $this->ensureNodeClass(parent::createElement($name, $content)); - } - - /** - * {@inheritdoc} - */ - public function createElementNS($namespaceURI, $qualifiedName, $value = null) - { - return $this->ensureNodeClass(parent::createElementNS($namespaceURI, $qualifiedName, $value)); - } - - /** - * xPath - * - * @param string $query - * @param DOMNode $contextNode - * - * @return DOMNodeList - */ - public function xpath($query, DOMNode $contextNode = null) - { - return $this->getXpath()->query($query, $contextNode); - } - - /** - * {@inheritdoc} - */ - public function appendDomElement(XmlElement $import, XmlElement $element = null, $deep = true) - { - $import = $this->importNode($import, $deep); - - if (null === $element) { - return $this->firstChild->appendChild($import); - } - - return $element->appendChild($import); - } - - /** - * getXpath - * - * @access protected - * @return DOMXpath - */ - public function getXpath() - { - if (!$this->xpath) { - $this->xpath = new DOMXpath($this); - } - return $this->xpath; - } - - /** - * Workarround for hhvm issue - * - * @see https://github.com/facebook/hhvm/issues/1848 - * - * @param DOMNode $node - * - * @return DOMNode - */ - private function ensureNodeClass(DOMNode $node) - { - $class = $this->nodeClasses['DOMElement']; - - if (true !== ($node instanceof $class) && $node instanceof \DOMElement) { - return $node->ownerDocument->importNode($node, true); - } - - return $node; - } -} diff --git a/lucid/xml/src/Dom/DOMElement.php b/lucid/xml/src/Dom/DOMElement.php deleted file mode 100644 index 07f0062..0000000 --- a/lucid/xml/src/Dom/DOMElement.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Dom; - -use BadMethodCallException; -use DOMElement as XmlElement; - -/** - * @class DOMElement - * - * @package Lucid\Xml - * @version $Id$ - * @author iwyg - */ -class DOMElement extends XmlElement -{ - /** - * xPath - * - * @access public - * @return mixed - */ - public function xpath($query) - { - if ($this->ownerDocument) { - return $this->ownerDocument->getXpath()->query($query, $this); - } - - throw new BadMethodCallException('cannot xpath on element without an owner document'); - } - - /** - * appendDomElement - * - * @param DOMElement $import - * @access public - * @return mixed - */ - public function appendDomElement(XMLElement $import, $deep = true) - { - if ($this->ownerDocument) { - return $this->ownerDocument->appendDomElement($import, $this, $deep); - } - - throw new BadMethodCallException('cannot add an element without an owner document'); - } -} diff --git a/lucid/xml/src/Inflector/InflectorInterface.php b/lucid/xml/src/Inflector/InflectorInterface.php deleted file mode 100644 index c41523c..0000000 --- a/lucid/xml/src/Inflector/InflectorInterface.php +++ /dev/null @@ -1,40 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Inflector; - -/** - * @interface InflectorInterface - * - * @package Lucid\Xml - * @version $Id$ - * @author iwyg - */ -interface InflectorInterface -{ - /** - * pluralize - * - * @param string $value - * - * @return string - */ - public function pluralize($value); - - /** - * singularize - * - * @param string $value - * - * @return string - */ - public function singularize($value); -} diff --git a/lucid/xml/src/Inflector/SimpleInflector.php b/lucid/xml/src/Inflector/SimpleInflector.php deleted file mode 100644 index 9f50559..0000000 --- a/lucid/xml/src/Inflector/SimpleInflector.php +++ /dev/null @@ -1,203 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Inflector; - -/** - * This class helps for handling Xml conversions of pluralized/singularaized - * node sets. - * - * It is not suiteable for inflecting strings in general. - * - * @class SimleInflector - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - */ -class SimpleInflector implements InflectorInterface -{ - /** @var string */ - const APPEND = '+'; - - /** @var string */ - const REDUCE = '-'; - - /** @var array */ - protected $cache; - - /** @var bool */ - protected $truncate; - - /** @var array */ - protected $singulars; - - /** @var array */ - protected $plurals; - - /** - * Pattern for 'y' to 'ies' and 'as/ox' to 'ases/oxes' conversion. - * - * @var array - */ - protected static $splurals = [ - '~(\w+[^y])y$~i' => '$1ies', - '~(\w+(as|ox))$~i' => '$1es', - ]; - - /** - * Pattern for 'ies' to 'y' and 'ases/oxes' to 'as/ox' conversion. - * - * @var array - */ - protected static $ssingulars = [ - '~(\w+[^ies])ies$~i' => '$1y', - '~(\w+(ox|as))(es)$~i' => '$1', - ]; - - /** - * Constructor. - * - * @param boolean $truncate will reduce or append trailing `s` if pattern - * match is unsuccessful. - * @param array $singulars additional pattern - * @param array $plurals additional pattern - */ - public function __construct($truncate = false, array $singulars = [], array $plurals = []) - { - $this->truncate = (bool)$truncate; - $this->cache = ['s' => [], 'p' => []]; - - $this->setSingulars($singulars); - $this->setPlurals($plurals); - } - - /** - * pluralize - * - * @param string $value - * - * @return string - */ - public function pluralize($value) - { - $key = $this->truncate ? self::APPEND : '_'; - - return $this->getValue($value, $key, $this->plurals, $this->cache['p'], $this->truncate); - } - - /** - * singularize - * - * @param string $value - * - * @return string - */ - public function singularize($value) - { - - $key = $this->truncate ? self::REDUCE : '_'; - - return $this->getValue($value, $key, $this->singulars, $this->cache['s'], $this->truncate); - } - - /** - * getValue - * - * @param string $value - * @param string $key - * @param array $patterns - * @param array $cache - * @param boolean $truncate - * - * @return string - */ - protected function getValue($value, $key, array $patterns, array &$cache = [], $truncate = false) - { - if (null === $value || !strlen($value)) { - return $value; - } - - if (!isset($cache[$key][$value])) { - $cache[$key][$value] = $this->inflect($patterns, $value, $truncate ? $key : null); - } - - return $cache[$key][$value]; - } - - /** - * inflect - * - * @param array $plurals - * @param string $value - * @param boolran $rdc - * - * @return string - */ - protected function inflect(array $plurals, $value, $rdc = null) - { - foreach ($plurals as $pattern => $repl) { - if (preg_match($pattern, $value)) { - return preg_replace($pattern, $repl, $value); - } - } - - return null === $rdc ? $value : (self::REDUCE === $rdc ? $this->reduceVal($value) : $this->appendVal($value)); - } - - /** - * reduceVal - * - * @param string $val - * - * @return string - */ - protected function reduceVal($val) - { - return 's' === substr($val, -1) ? substr($val, 0, -1) : $val; - } - - /** - * appendVal - * - * @param string $val - * - * @return string - */ - protected function appendVal($val) - { - return 's' === substr($val, -1) ? $val : $val . 's'; - } - - /** - * setSingulars - * - * @param array $singulars - * - * @return void - */ - protected function setSingulars(array $singulars) - { - $this->singulars = array_merge(static::$ssingulars, $singulars); - } - - /** - * setPlurals - * - * @param array $plurals - * - * @return void - */ - protected function setPlurals(array $plurals) - { - $this->plurals = array_merge(static::$splurals, $plurals); - } -} diff --git a/lucid/xml/src/Loader/Loader.php b/lucid/xml/src/Loader/Loader.php deleted file mode 100644 index 4d59bbb..0000000 --- a/lucid/xml/src/Loader/Loader.php +++ /dev/null @@ -1,53 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Loader; - -/** - * @class Loader - * - * @package Lucid\Xml\Loader - * @version $Id$ - * @author iwyg - */ -class Loader implements LoaderInterface -{ - use LoaderTrait; - - /** - * {@inheritdoc} - * @deprecated - */ - public function setOption($option, $value) - { - } - - /** - * {@inheritdoc} - * @deprecated - */ - public function getOption($option, $default = null) - { - } - - /** - * {@inheritdoc} - * @deprecated - */ - public function load($file, array $options = []) - { - if ($this->getDefault($options, self::SIMPLEXML, false)) { - return $this->loadSimpleXml($file, $options); - } - - return $this->loadDom($file, $options); - } -} diff --git a/lucid/xml/src/Loader/LoaderInterface.php b/lucid/xml/src/Loader/LoaderInterface.php deleted file mode 100644 index acfa030..0000000 --- a/lucid/xml/src/Loader/LoaderInterface.php +++ /dev/null @@ -1,83 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Loader; - -/** - * @interface LoaderInterface - * - * @package Lucid\Xml - * @version $Id$ - */ -interface LoaderInterface -{ - /** @var string */ - const ENCODING = 'encoding'; - - /** @var string */ - const FROM_STRING = 'from_string'; - - /** @var string */ - const DOM_CLASS = 'dom_class'; - - /** @var string */ - const SIMPLEXML = 'simplexml'; - - /** @var string */ - const SIMPLEXML_CLASS = 'simplexml_class'; - - /** - * load - * - * @param string $xml - * - * @return DOMDocument or SimpleXMLElement - */ - public function load($xml); - - /** - * loadToDom - * - * @param string $xml - * - * @return \DOMDocument - */ - public function loadDom($xml); - - /** - * loadToSimpleXml - * - * @param string $xml - * - * @return \SimpleXmlElement - */ - public function loadSimpleXml($xml); - - /** - * setOption - * - * @param string $option - * @param mixed $value - * - * @return void - */ - public function setOption($option, $value); - - /** - * getOption - * - * @param string $option - * @param mixed $default - * - * @return mixed - */ - public function getOption($option, $default = null); -} diff --git a/lucid/xml/src/Loader/LoaderTrait.php b/lucid/xml/src/Loader/LoaderTrait.php deleted file mode 100644 index 14cf239..0000000 --- a/lucid/xml/src/Loader/LoaderTrait.php +++ /dev/null @@ -1,127 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Loader; - -use InvalidArgumentException; -use Lucid\Common\Traits\Getter; - -/** - * @trait LoaderTrait - * - * @package Lucid\Xml\Loader - * @version $Id$ - * @author iwyg - */ -trait LoaderTrait -{ - use Getter; - - /** - * {@inheritdoc} - */ - public function loadDom($file, array $options = []) - { - $class = $this->getDefault($options, static::DOM_CLASS, 'Lucid\Xml\Dom\DOMDocument'); - - $this->loadXmlInDom( - $dom = new $class('1.0', $this->getDefault($options, static::ENCODING, 'UTF-8')), - $file, - $this->getDefault($options, static::FROM_STRING, false) ? 'loadXML' : 'load' - ); - - return $dom; - } - - /** - * {@inheritdoc} - */ - public function loadSimpleXml($file, array $options = []) - { - return simplexml_import_dom( - $this->loadDom($file, $options), - $this->getDefault($options, static::SIMPLEXML_CLASS, 'Lucid\Xml\SimpleXMLElement') - ); - } - - /** - * loadXmlInDom - * - * @param \DOMDocument $dom - * @param mixed $file - * @access protected - * @return DOMDocument; - */ - private function loadXmlInDom(\DOMDocument $dom, $xml, $method) - { - $usedInternalErrors = libxml_use_internal_errors(true); - $externalEntitiesDisabled = libxml_disable_entity_loader(false); - - libxml_clear_errors(); - - if (!$this->loadDomDocument($dom, $xml, $method)) { - libxml_disable_entity_loader($externalEntitiesDisabled); - - throw new InvalidArgumentException($this->formatLibXmlErrors($usedInternalErrors)); - } - - // restore previous libxml setting: - libxml_use_internal_errors($usedInternalErrors); - libxml_disable_entity_loader($externalEntitiesDisabled); - - return $dom; - } - - /** - * loadDomDocument - * - * @param \DOMDocument $dom - * @param mixed $method - * @param mixed $content - * - * @return bool - */ - private function loadDomDocument(\DOMDocument $dom, $xml, $method) - { - return call_user_func_array( - [$dom, $method], - // set LIBXML_NONET to prevent local and remote file inclusion attacks. - [$xml, LIBXML_NONET | LIBXML_DTDATTR | defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0] - ); - } - - /** - * getXmlErrors - * - * @return array - */ - private function formatLibXmlErrors($usedInternalErrors) - { - $errors = []; - - foreach (libxml_get_errors() as $error) { - $errors[] = sprintf( - '[%s %s] %s (in file %s in column %s on line %s)', - LIBXML_ERR_ERROR === $error->level ? 'ERROR' : 'WARNING', - $error->code, - $error->message, - $error->file ?: 'n/a', - $error->column, - $error->line - ); - } - - libxml_clear_errors(); - libxml_use_internal_errors($usedInternalErrors); - - return implode("\n", $errors); - } -} diff --git a/lucid/xml/src/Normalizer/Normalizer.php b/lucid/xml/src/Normalizer/Normalizer.php deleted file mode 100644 index dfc9e2c..0000000 --- a/lucid/xml/src/Normalizer/Normalizer.php +++ /dev/null @@ -1,421 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - - -namespace Lucid\Xml\Normalizer; - -use ReflectionObject; -use ReflectionMethod; -use ReflectionProperty; -use Lucid\Common\Helper\Str; -use Lucid\Xml\Traits\XmlHelperTrait; - -/** - * @class Normalizer - * - * @package Lucid\Xml - * @version $Id$ - * @author iwyg - */ -class Normalizer implements NormalizerInterface -{ - use XmlHelperTrait; - - /** @var array */ - protected $objectCache; - - /** @var array */ - protected $ignoredAttributes; - - /** @var array */ - protected $ignoredObjects; - - /** @var array */ - protected $normalized; - - /** Creates a new Normalizer instance. */ - public function __construct() - { - $this->normalized = []; - $this->objectCache = []; - $this->ignoredObjects = []; - $this->ignoredAttributes = []; - } - - /** - * {@inheritdoc} - */ - public function normalize($value) - { - $ovalue = $value; - - if (!isset($this->normalized[$value])) { - $this->normalized[$ovalue] = $this->normalizeString($value); - } - - return $this->normalized[$ovalue]; - } - - /** - * {@inheritdoc} - * @deprecated - */ - public function ensureArray($data) - { - if (!is_array($result = $this->convertValue($data))) { - return; - } - - return $result; - } - - /** - * {@inheritdoc} - */ - public function ensureBuildable($data) - { - if ($this->isXMLElement($data)) { - return $data; - } - - if ($this->isTraversable($data)) { - return $this->recursiveConvertArray($data); - } - - if (is_object($data)) { - return $this->convertObject($data) ?: null; - } - - return $data; - } - - /** - * Sets ignored attributes - * - * @param array $attributes - * - * @return void - */ - public function setIgnoredAttributes(array $attributes) - { - $this->ignoredAttributes = $attributes; - } - - /** - * Adds an ignored attribute - * - * @param string $attribute - * - * @return void - */ - public function addIgnoredAttribute($attribute) - { - $this->ignoredAttributes[] = $attribute; - } - - /** - * Sets ignored classes - * - * @param array $classes - * - * @return void - */ - public function setIgnoredObjects(array $classes) - { - $this->ignoredObjects = []; - - array_map([$this, 'addIgnoredObject'], $classes); - } - - /** - * Adds an ignored class. - * - * @param string $classname - * - * @return void - */ - public function addIgnoredObject($classname) - { - $this->ignoredObjects[] = preg_replace('~^\\\~', '', strtolower($classname)); - } - - /** - * Conversts a given value into a traversable one. - * - * @param mixed $data - * - * @return mixed - */ - protected function convertValue($data) - { - if ($this->isTraversable($data)) { - return $this->recursiveConvertArray($data); - } - - if (is_object($data)) { - return $this->convertObject($data) ?: null; - } - - return $data; - } - - /** - * Checks if given value is traversable. - * - * @param mixed $data - * - * @return bool - */ - protected function isTraversable($data) - { - return is_array($data) || $data instanceof \Traversable; - } - - /** - * Recursivly converts a given value to an array. - * - * @param array $data - * - * @return array - */ - protected function recursiveConvertArray($data) - { - $out = []; - - foreach ($data as $key => $value) { - - $nkey = $this->normalize($key); - - if (in_array($nkey, $this->ignoredAttributes)) { - continue; - } - - $out[$nkey] = is_scalar($value) ? $value : $this->convertValue($value); - - } - - return $out; - } - - /** - * isArrayAble - * - * @param mixed $reflection a reflection object - * @access protected - * @return boolean - */ - protected function isArrayable($data) - { - return $data->hasMethod('toArray') && $data->getMethod('toArray')->isPublic(); - } - - /** - * convertObject - * - * @param Object $data - * - * @return array - */ - protected function convertObject($data) - { - - if ($this->isIgnoredObject($data)) { - return; - } - - if ($this->isXMLElement($data)) { - return $data; - } - - $reflection = new ReflectionObject($data); - - if ($this->isArrayAble($reflection)) { - $data = $data->toArray(); - return $this->ensureBuildable($data); - } - - if ($this->isCircularReference($data)) { - return; - } - - $out = []; - - $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); - $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC); - - $this->getObjectGetterValues($methods, $data, $out); - - $this->setObjectProperties($properties, $data, $out); - - return $out; - } - - /** - * isGetMethod - * - * @param mixed $method - * @access public - * @return boolean - */ - protected function isGetMethod(\ReflectionMethod $method) - { - return 'get' === substr($method->name, 0, 3) && strlen($method->name) > 3 && - 0 === $method->getNumberOfRequiredParameters(); - } - - /** - * getObjectGetterValues - * - * @param mixed $methods - * @param array $out - * @access protected - * @return mixed - */ - protected function getObjectGetterValues($methods, $object, array &$out = []) - { - foreach ($methods as $method) { - $this->setObjectGetterValue($method, $object, $out); - } - } - /** - * setObjectGetterValue - * - * @param ReflectionMethod $method - * @param array $out - * - * @return void - */ - protected function setObjectGetterValue(ReflectionMethod $method, $object, array &$out = []) - { - if (!$this->isGetMethod($method)) { - return; - } - - $attributeName = substr($method->name, 3); - $attributeValue = $method->invoke($object); - - $nkey = $this->normalize($attributeName); - - if (is_callable($attributeValue) || in_array($nkey, $this->ignoredAttributes)) { - return; - } - - if (null !== $attributeValue && !is_scalar($attributeValue)) { - $attributeValue = $this->ensureBuildable($attributeValue); - } - - $out[$nkey] = $attributeValue; - } - - /** - * Set opbject properties to an output array. - * - * @param array $properties - * @param mixed $data - * @param array $out - * - * @return void - */ - protected function setObjectProperties(array $properties, $data, array &$out = []) - { - foreach ($properties as $property) { - $prop = $property->getName(); - - if (in_array($name = $this->normalize($prop), $this->ignoredAttributes)) { - continue; - } - - $out[$prop] = $this->getObjectPropertyValue($property, $prop, $data); - } - } - - /** - * getPropertyValue - * - * @param \ReflectionProperty $property - * @param mixed $prop - * - * @return mixed - */ - protected function getObjectPropertyValue(\ReflectionProperty $property, $prop, $data) - { - $prop = $property->getName(); - - try { - $value = $data->{$prop}; - } catch (\Exception $e) { - } - - if (!is_scalar($value)) { - $value = $this->ensureBuildable($value); - } - - return $value; - } - - /** - * isCircularReference - * - * @return boolean - */ - protected function isCircularReference($data) - { - $circularReference = in_array($hash = spl_object_hash($data), $this->objectCache); - $this->objectCache[] = $hash; - - return $circularReference; - } - - /** - * normalizeString - * - * @param string $string - * - * @return string - */ - protected function normalizeString($string) - { - $value = $this->isAllUpperCase($string) ? - strtolower(trim($string, '_-#$%')) : - Str::snakeCase(trim($string, '_-#$%')); - - return strtolower(preg_replace('/[^a-zA-Z0-9(^@)]+/', '-', $value)); - } - - /** - * isIgnoredObject - * - * @param object $oject - * - * @return boolean - */ - protected function isIgnoredObject($object) - { - return in_array( - strtolower($class = ($parent = get_parent_class($object)) ? $parent : get_class($object)), - $this->ignoredObjects - ); - } - - /** - * isAllUpperCase - * - * @param string $str - * - * @return boolean - */ - private function isAllUpperCase($str) - { - $str = preg_replace('/[^a-zA-Z0-9]/', null, $str); - - return ctype_upper($str); - } -} diff --git a/lucid/xml/src/Normalizer/NormalizerInterface.php b/lucid/xml/src/Normalizer/NormalizerInterface.php deleted file mode 100644 index e77cbec..0000000 --- a/lucid/xml/src/Normalizer/NormalizerInterface.php +++ /dev/null @@ -1,50 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Normalizer; - -/** - * @interface NormalizerInterface - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -interface NormalizerInterface -{ - /** - * normalize - * - * @param string $string - * - * @return string - */ - public function normalize($string); - - /** - * ensureArray - * - * @param mixed $data - * - * @return array - */ - public function ensureArray($data); - - /** - * ensureBuilable - * - * @param mixed $data - * - * @return mixed - */ - public function ensureBuildable($data); -} diff --git a/lucid/xml/src/Parser.php b/lucid/xml/src/Parser.php deleted file mode 100644 index e92c5c3..0000000 --- a/lucid/xml/src/Parser.php +++ /dev/null @@ -1,568 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml; - -use Lucid\Xml\Dom\DOMElement; -use Lucid\Xml\Dom\DOMDocument; -use Lucid\Xml\Loader\Loader; -use Lucid\Xml\Loader\LoaderInterface; -use Lucid\Xml\Traits\XmlHelperTrait; -use Lucid\Common\Helper\Str; -use Lucid\Common\Traits\Getter; - -/** - * @class Parser - * - * @package Lucid\Xml - * @version $Id$ - * @author iwyg - */ -class Parser implements ParserInterface -{ - use Getter, - XmlHelperTrait; - - /** @var callable */ - private $pluralizer; - - /** @var callable */ - private $keyNormalizer; - - /** - * @var array */ - private $options; - - /** - * Creates a new `Parser` instance. - * - * @param LoaderInterface $loader - */ - public function __construct(LoaderInterface $loader = null) - { - $this->loader = $loader ?: new Loader($this->getLoaderConfig()); - $this->options = []; - } - - /** - * Toggle on/off merging attributes to array keys. - * - * @param bool $merge - * - * @return void - */ - public function setMergeAttributes($merge) - { - $this->options['merge_attributes'] = (bool)$merge; - } - - /** - * Set the attributes key name. - * - * The default key will be `@attributes`. - * This will be ignored if merging attributes is active. - * - * @param string $key - * - * @return void - */ - public function setAttributesKey($key) - { - $this->options['attribute_key'] = $key; - } - - /** - * Get the attributes key. - * - * Defaults to `@attributes`. - * - * @return string - */ - public function getAttributesKey() - { - return $this->getDefault($this->options, 'attribute_key', '@attributes'); - } - - /** - * Set the list identifier key. - * - * Elements that match with that key will always be considered a list, - * as long as thy have any parent element. - * - * @param string $key - * - * @return void - */ - public function setIndexKey($key) - { - $this->options['index_key'] = $key; - } - - /** - * Sets the index key used in attributes. - * - * @param string $key - * - * @return void - */ - public function setIndexAttributeKey($key) - { - $this->options['index_attr_key'] = $key; - } - - /** - * getIndexKey - * - * @return mixed - */ - public function getIndexKey() - { - return $this->getDefault($this->options, 'index_key', null); - } - - /** - * getIndexAttributeKey - * - * @return string - */ - public function getIndexAttributeKey() - { - return $this->getDefault($this->options, 'index_attr_key', 'index'); - } - - /** - * Set a custom function to normalize an xml node name to a php array key name. - * - * By default, hyphens are converted to underscores. - * - * @param callable $normalizer - * - * @return void - */ - public function setKeyNormalizer(callable $normalizer) - { - $this->keyNormalizer = $normalizer; - } - - /** - * Set the pluralizer. - * - * @param callable $pluralizer - * - * @return void - */ - public function setPluralizer(callable $pluralizer = null) - { - $this->pluralizer = $pluralizer; - } - - /** - * {@inheritdoc} - */ - public function parseDom(\DOMDocument $xml) - { - if (!$xml instanceof DOMDocument) { - $xml = $this->convertDocument($xml); - } - - if (!$root = $xml->documentElement) { - throw new \InvalidArgumentException('DOM has no root element'); - } - - return [$xml->documentElement->nodeName => $this->parseDomElement($root)]; - } - - /** - * {@inheritdoc} - */ - public function parse($xml) - { - $opts = $this->getLoaderConfig(); - $opts[LoaderInterface::FROM_STRING] = !(is_file($xml) && stream_is_local($xml)); - - return $this->parseDom($this->loader->load($xml, $opts)); - } - - /** - * {@inheritdoc} - */ - public function parseDomElement(DOMElement $xml) - { - $result = $this->parseElementNodes($xml->xpath('./child::*'), $xml->nodeName); - $attributes = $this->parseElementAttributes($xml); - $text = $this->prepareTextValue($xml, current($attributes) ?: null); - $hasAttributes = (bool)$attributes; - - if ($hasAttributes) { - - if (null !== $text) { - $result['value'] = $text; - } - - if ($this->mergeAttributes()) { - $attributes = $attributes[$this->getAttributesKey()]; - } - - $result = array_merge($attributes, $result); - return $result; - } - - if (null !== $text) { - if (!empty($result)) { - $result['value'] = $text; - } else { - $result = $text; - } - return $result; - } - - return (!(bool)$result && null === $text) ? null : $result; - } - - /** - * Get the php equivalent of an input value derived from any king of xml. - * - * @param mixed $val - * @param mixed $default - * @param ParserInterface $parser - * - * @return mixed - */ - public static function getPhpValue($val, $default = null, ParserInterface $parser = null) - { - if ($val instanceof DOMElement) { - $parser = $parser ?: new static; - - return $parser->parseDomElement($val); - } - - if (0 === strlen($val)) { - return $default; - } - - if (0 === strpos($val, '0x') && preg_match('#^[[:xdigit:]]+$#', substr($val, 2))) { - return hexdec($val); - } - - if (is_numeric($val)) { - return ctype_digit($val) ? intval($val) : floatval($val); - } - - if (in_array($lval = strtolower($val), ['true', 'false'])) { - return $lval === 'true' ? true : false; - } - - return $val; - } - - /** - * Get the text of a `DOMElement` excluding the contents - * of its child elements. - * - * @param DOMElement $element - * @param bool $concat - * - * @return string|array returns an array of strings if `$concat` is `false` - */ - public static function getElementText(DOMElement $element, $concat = true) - { - $textNodes = []; - - foreach ($element->xpath('./text()') as $text) { - - if ($value = static::clearValue($text->nodeValue)) { - $textNodes[] = $value; - } - } - return $concat ? implode($textNodes) : $textNodes; - } - - /** - * clearValue - * - * @param mixed $text - * - * @return string|null - */ - public static function clearValue($value) - { - return is_string($value) && 0 === strlen(trim($value)) ? null : $value; - } - - /** - * Convert hyphens to underscores. - * - * @param string $name - * - * @return string - */ - public static function fixNodeName($name) - { - return strtr(Str::snakeCase($name), ['-' => '_']); - } - - /** - * Check if a given string is the list identifier. - * - * @param string $name - * @param string $prefix - * - * @return bool - */ - protected function isListKey($name, $prefix = null) - { - return $this->prefixKey($this->getIndexKey(), $prefix) === $name; - } - - /** - * Determine weather to merge attributes or not. - * - * @return bool - */ - protected function mergeAttributes() - { - return $this->getDefault($this->options, 'merge_attributes', false); - } - - - /** - * getLoaderConfig - * - * @return mixed - */ - protected function getLoaderConfig() - { - return [ - LoaderInterface::FROM_STRING => false, - LoaderInterface::SIMPLEXML => false, - LoaderInterface::DOM_CLASS => __NAMESPACE__.'\\Dom\DOMDocument', - LoaderInterface::SIMPLEXML_CLASS => __NAMESPACE__.'\\SimpleXMLElement' - ]; - } - - /** - * Normalize a node key - * - * @param mixed $key - * - * @return mixed - */ - protected function normalizeKey($key) - { - if (null !== $this->keyNormalizer) { - return call_user_func($this->keyNormalizer, $key); - } - - return static::fixNodeName($key); - } - - /** - * Convert bool like and numeric values to their php equivalent values. - * - * @param DOMElement $xml the element to get the value from - * @param array $attributes - * @return mixed - */ - private function prepareTextValue(DOMElement $xml, array $attributes = null) - { - $text = static::getElementText($xml, true); - - return (isset($attributes['type']) && 'text' === $attributes['type']) ? - static::clearValue($text) : - static::getPhpValue($text, null, $this); - } - - /** - * Parse a nodelist into a array - * - * @param \DOMNodeList|array $children elements to parse - * @param string $parentName node name of the parent element - * - * @return array - */ - private function parseElementNodes($children, $parentName = null) - { - $result = []; - - foreach ($children as $child) { - $prefix = $child->prefix ?: null; - $oname = $this->normalizeKey($child->nodeName); - $name = $this->prefixKey($oname, $prefix); - - if (isset($result[$name])) { - $this->parseSetResultNodes($child, $name, $result); - continue; - } - - $this->parseUnsetResultNodes($child, $name, $oname, $parentName, $result, $prefix); - } - - return $result; - } - - /** - * Parse a `DOMElement` if a result key is set. - * - * @param DOMElement $child - * @param string $name - * @param array $result - * - * @return mixed|bool the result, else `false` if no result. - */ - private function parseSetResultNodes(DOMElement $child, $name, array &$result = null) - { - if (!(is_array($result[$name]) && $this->valueIsList($result[$name]))) { - return false; - } - - $value = static::getPhpValue($child, null, $this); - - if (is_array($value) && $this->valueIsList($value)) { - return $result[$name] = array_merge($result[$name], $value); - } - - return $result[$name][] = $value; - } - - /** - * Parse a `DOMElement` if a result key is unset. - * - * @param DOMElement $child - * @param string $name - * @param string $oname - * @param string $pName - * @param array $result - * @param string $prefix - * @param array $attrs - * - * @return mixed the result - */ - private function parseUnsetResultNodes(DOMElement $child, $name, $oname, $pName, array &$result, $prefix = null) - { - if ($this->isListKey($name, $prefix) || $this->isEqualOrPluralOf($this->normalizeKey($pName), $oname)) { - // if attributes index is set, use it as index - if (($index = $child->getAttribute($this->getIndexAttributeKey())) && 0 !== strlen($index)) { - $child->removeAttribute($this->getIndexAttributeKey()); - - return $result[static::getPhpValue($index)] = static::getPhpValue($child, null, $this); - } - - return $result[] = static::getPhpValue($child, null, $this); - } - - $value = static::getPhpValue($child, null, $this); - - if (1 < $this->getEqualNodes($child, $prefix)->length) { - return $result[$name][] = $value; - } - - return $result[$name] = $value; - } - - /** - * Parse element attributes into an array. - * - * @param DOMElement $xml - * - * @return array - */ - private function parseElementAttributes(DOMElement $xml) - { - $attrs = []; - $elementAttrs = $xml->xpath('./@*'); - - if (0 === $elementAttrs->length) { - return $attrs; - } - - foreach ($elementAttrs as $key => $attribute) { - $value = static::getPhpValue($attribute->nodeValue, null, $this); - $name = $this->normalizeKey($attribute->nodeName); - $attrs[$this->prefixKey($name, $attribute->prefix ?: null)] = $value; - } - - return [$this->getAttributesKey() => $attrs]; - } - - /** - * Check if the input string is a plural or equal to a given comparative string. - * - * @param string $name the input string - * @param string $singular the string to compare with - * - * @return bool - */ - private function isEqualOrPluralOf($name, $singular) - { - return 0 === strnatcmp($name, $singular) || 0 === strnatcmp($name, $this->pluralize($singular)); - } - - /** - * Attempt to pluralize a string. - * - * @param string $singular - * - * @return string - */ - private function pluralize($singular) - { - if (null === $this->pluralizer) { - return $singular; - } - - return call_user_func($this->pluralizer, $singular); - } - - /** - * A lookahead to find sibling elements with similar names. - * - * @param DOMElement $node the node in charge. - * @param string $prefix the element prefix - * - * @return \DOMNodeList - */ - private function getEqualNodes(DOMElement $node, $prefix = null) - { - $name = $this->prefixKey($node->nodeName, $prefix); - - return $node->xpath( - sprintf(".|following-sibling::*[name() = '%s']|preceding-sibling::*[name() = '%s']", $name, $name) - ); - } - - /** - * Prepend a string. - * - * Will pass-through the original string if `$prefix` is `null`. - * - * @param string $key the key to prefix - * @param string $prefix the prefix - * - * @return string - */ - private function prefixKey($key, $prefix = null) - { - return $prefix ? sprintf('%s:%s', $prefix, $key) : $key; - } - - /** - * Converts a `\DOMDocument`that is not an instance of - * `Selene\Module\Dom\DOMDocument`. - * - * @param \DOMDocument $xml the document to convert - * - * @return DOMDocument - */ - private function convertDocument(\DOMDocument $xml) - { - return $this->loader->load($xml->saveXML(), [LoaderInterface::FROM_STRING => true]); - } -} diff --git a/lucid/xml/src/ParserInterface.php b/lucid/xml/src/ParserInterface.php deleted file mode 100644 index 62fba32..0000000 --- a/lucid/xml/src/ParserInterface.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml; - -use Lucid\Xml\Dom\DOMElement; - -/** - * @interface ParserInterface - * - * @package Selene\Module\Xml - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -interface ParserInterface -{ - /** - * Parses an xml string or file into an array. - * - * @param string $xml - * - * @return array - */ - public function parse($xml); - - /** - * Parses a `\DOMDocument` into an array. - * - * @param \DOMDocument $xml - * - * @return array - */ - public function parseDom(\DOMDocument $xml); - - /** - * Parse the contents of a `DOMElement` to an array. - * - * @param DOMElement $xml - * - * @return null|array - */ - public function parseDomElement(DOMElement $param); -} diff --git a/lucid/xml/src/SimpleXMLElement.php b/lucid/xml/src/SimpleXMLElement.php deleted file mode 100644 index 24d4801..0000000 --- a/lucid/xml/src/SimpleXMLElement.php +++ /dev/null @@ -1,156 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml; - -use \SimpleXMLElement as SimpleXML; - -/** - * Class: SimpleXMLElement - * - * @uses \SimpleXMLElement - * - * @package - * @version - * @author Thomas Appel - * @license MIT - */ -class SimpleXMLElement extends SimpleXML -{ - /** - * argumentsAsArray - * - * @access public - * @return array - */ - public function attributesAsArray($namespace = null) - { - $attributes = []; - - foreach ($this->attributes() as $key => $value) { - $attributes[$key] = $this->getPhpValue((string)$value); - } - - return $attributes; - } - - /** - * phpValue - * - * @access public - * @return mixed - */ - public function phpValue() - { - return $this->getPhpValue((string)$this); - } - - /** - * addCDATASection - * - * @param mixed $content string, SimpleXMLElement, or DOMDocument - * @access public - * @return void - */ - public function addCDATASection($content) - { - switch (true) { - case is_string($content): - break; - case ($content instanceof \SimpleXMLElement): - $dom = dom_import_simplexml($content); - $content = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement); - break; - case ($content instanceof \DOMDocument): - $content = $content->saveXML($content->childNodes->item(0)); - break; - case ($content instanceof \DOMNode): - $dom = new \DOMDocument(); - $import = $dom->importNode($content, true); - $dom->appendChild($import); - $content = $dom->saveXML($dom->firstChild); - break; - default: - throw new \InvalidArgumentException( - sprintf( - 'expected arguement 1 to be String, SimpleXMLElement, DOMNode, or DOMDocument, instead saw %s', - gettype($content) - ) - ); - } - - $import = dom_import_simplexml($this); - $node = $import->ownerDocument; - $import->appendChild($node->createCDATASection($content)); - } - - /** - * Append a childelement from a well formed html string. - * - * @param string a html string - * @access public - * @return void - */ - public function appendChildFromHtmlString($html) - { - $dom = new \DOMDocument; - $dom->loadHTML($html); - - $element = simplexml_import_dom($dom); - $element = current($element->xPath('//body/*')); - - $this->appendChildNode($element); - } - - /** - * Append a childelement from a well formed xml string. - * - * @param string $xml a well formed xml string - * @access public - * @return void - */ - public function appendChildFromXmlString($xml) - { - $dom = new \DOMDocument; - $dom->loadXML($xml); - - $element = simplexml_import_dom($dom); - - $this->appendChildNode($element); - } - - /** - * Append a SimpleXMLElement to the current SimpleXMLElement. - * - * @param \SimpleXMLElement $element the element to be appended. - * - * @access public - * @return void - */ - public function appendChildNode(\SimpleXMLElement $element) - { - $target = dom_import_simplexml($this); - $insert = $target->ownerDocument->importNode(dom_import_simplexml($element), true); - $target->appendChild($insert); - } - - /** - * phpValue - * - * @param mixed $param - * @access public - * @return mixed - */ - protected function getPhpValue($value) - { - return Parser::getPhpValue($value); - } -} diff --git a/lucid/xml/src/Traits/XmlHelperTrait.php b/lucid/xml/src/Traits/XmlHelperTrait.php deleted file mode 100644 index eb38669..0000000 --- a/lucid/xml/src/Traits/XmlHelperTrait.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Traits; - -use Lucid\Common\Helper\Arr; - -/** - * @trait XmlHelperTrait - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - */ -trait XmlHelperTrait -{ - /** - * isXmlElement - * - * @param mixed $element - * - * @return bool - */ - public function isXmlElement($element) - { - return $element instanceof \DOMNode || $element instanceof \SimpleXmlElement; - } - - /** - * valueIsList - * - * @param mixed $value - * - * @return bool - */ - public function valueIsList($value) - { - return is_array($value) && Arr::isList($value, true); - } -} diff --git a/lucid/xml/src/Writer.php b/lucid/xml/src/Writer.php deleted file mode 100644 index a4c8903..0000000 --- a/lucid/xml/src/Writer.php +++ /dev/null @@ -1,620 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml; - -use Lucid\Common\Helper\Arr; -use Lucid\Xml\DOM\DOMElement; -use Lucid\Xml\DOM\DOMDocument; -use Lucid\Xml\Traits\XmlHelperTrait; -use Lucid\Xml\Normalizer\Normalizer; -use Lucid\Xml\Normalizer\NormalizerInterface; - -/** - * @class Writer - * - * @package Lucid\Xml - * @version $Id$ - * @author iwyg - */ -class Writer -{ - use XmlHelperTrait; - - /** @var DOMDocument */ - private $dom; - - /** @var string */ - private $encoding; - - /** @var NormalizerInterface */ - private $normalizer; - - /** @var callable */ - private $inflector; - - /** @var mixed */ - private $attributemap; - - /** @var string */ - private $nodeValueKey; - - /** @var string */ - private $indexKey; - - /** @var string */ - private $indexAttrKey; - - /** - * Create a new xml writer instance. - * - * @param NormalizerInterface $normalizer the normalizer instance. - * @param string $encoding the default encoding - * - */ - public function __construct(NormalizerInterface $normalizer = null, $encoding = 'UTF-8') - { - $this->setNormalizer($normalizer ?: new Normalizer); - $this->setEncoding($encoding); - - $this->attributemap = []; - $this->indexKey = 'item'; - } - - /** - * Sets the encoding. - * - * @param mixed $encoding - * - * @return void - */ - public function setEncoding($encoding) - { - $this->encoding = $encoding; - } - - /** - * Gets the encoding. - * - * @return string - */ - public function getEncoding() - { - return $this->encoding; - } - - /** - * Sets the Normalizer. - * - * @param NormalizerInterface $normalizer - * - * @return void - */ - public function setNormalizer(NormalizerInterface $normalizer) - { - $this->normalizer = $normalizer; - } - - /** - * Returns the normalizer. - * - * @return Thapp\XmlBuilder\NormalizerInterface - */ - public function getNormalizer() - { - return $this->normalizer; - } - - /** - * Sets the inflector. - * - * @param callable $inflector - * - * @return void - */ - public function setInflector(callable $inflector) - { - $this->inflector = $inflector; - } - - /** - * Sets the Nodename => attribtues map. - * - * @param array $map - * - * @return void - */ - public function setAttributeMap(array $map) - { - $this->attributemap = $map; - } - - /** - * Adds a mapped attribute to a nodename. - * - * @param string $nodeName - * @param string $attribute - * - * @return void - */ - public function addMappedAttribute($nodeName, $attribute) - { - $this->attributemap[$nodeName][] = $attribute; - } - - /** - * Tells if a node name as a mapped attribute of `$key`. - * - * @param string $name - * @param string $key - * - * @return bool - */ - public function isMappedAttribute($name, $key) - { - $map = isset($this->attributemap[$name]) ? $this->attributemap[$name] : []; - - if (isset($this->attributemap['*'])) { - $map = array_merge($this->attributemap['*'], $map); - } - - return in_array($key, $map); - } - - /** - * Sets the key that indicates a node value. - * - * @param string $key - * @param bool $normalize - * - * @throws \InvalidArgumentException if $key is not null and an invalid nodename. - * @return void - */ - public function useKeyAsValue($key, $normalize = false) - { - if (true !== $normalize && !$this->isValidNodeName($key)) { - throw new \InvalidArgumentException(sprintf('%s is an invalid node name', $key)); - } - - $this->nodeValueKey = $normalize ? $this->normalizer->normalize($key) : $key; - } - - /** - * Sets the index key used as key -> node identifier. - * - * @param string $key - * @param bool $normalize - * - * @throws \InvalidArgumentException if $key is an invalid nodename. - * @return void - */ - public function useKeyAsIndex($key, $normalize = false) - { - if (null === $key) { - $this->indexKey = null; - return; - } - - if (true !== $normalize && !$this->isValidNodeName($key)) { - throw new \InvalidArgumentException(sprintf('%s is an invalid node name', $key)); - } - - $this->indexKey = $normalize ? $this->normalizer->normalize($key) : $key; - } - - /** - * Sets the index key used in attributes. - * - * @param string $key - * @param bool $normalize - * - * @return void - */ - public function useKeyAsIndexAttribute($key, $normalize = false) - { - if (null === $key) { - $this->indexAttrKey = null; - return; - } - - if (true !== $normalize && !$this->isValidNodeName($key)) { - throw new \InvalidArgumentException(sprintf('%s is an invalid attribute name', $key)); - } - - $this->indexAttrKey = $normalize ? $this->normalizer->normalize($key) : $key; - } - - /** - * Dump the input data to a xml string. - * - * @param mixed $data - * @param string $rootName the xml root element name - * - * @return string - */ - public function dump($data, $rootName = 'root') - { - $dom = $this->writeToDom($data, $rootName); - - return $dom->saveXML(); - } - - /** - * Write the input data to a DOMDocument - * - * @param mixed $data - * @param string $rootName the xml root element name - * - * @return DOMDocument - */ - public function writeToDom($data, $rootName = 'root') - { - $this->buildXML( - $dom = new DOMDocument('1.0', $this->getEncoding()), - $root = $dom->createElement($rootName), - $data - ); - $dom->appendChild($root); - - return $dom; - } - - /** - * buildXML - * - * @param DOMNode $node - * @param mixed $data - * - * @return void - */ - protected function buildXML(DOMDocument $dom, \DOMNode $node, $data) - { - $normalizer = $this->getNormalizer(); - - if (null === $data) { - return; - } - - if ((is_array($data) || $data instanceof \Traversable) && !$this->isXmlElement($data)) { - return $this->buildXmlFromTraversable($dom, $node, $normalizer->ensureBuildable($data)); - } - - $this->setElementValue($dom, $node, $data); - } - - /** - * buildXmlFromTraversable - * - * @param \DOMDocument $dom - * @param \DOMNode $node - * @param mixed $data - * - * @return void - */ - protected function buildXmlFromTraversable(\DOMDocument $dom, \DOMNode $node, $data) - { - $normalizer = $this->getNormalizer(); - $hasAttributes = false; - $isList = is_array($data) && Arr::isList($data, true); - - foreach ($data as $key => $value) { - - if (!is_scalar($value) && !($value = $normalizer->ensureBuildable($value))) { - continue; - } - - if ($this->mapAttributes($node, $normalizer->normalize($key), $value)) { - $hasAttributes = true; - continue; - } - - // ensure "node value keys" are at the bottom. - if (null !== $this->nodeValueKey && is_array($value) && isset($value[$this->nodeValueKey])) { - $val = $value[$this->nodeValueKey]; - unset($value[$this->nodeValueKey]); - $value[$this->nodeValueKey] = $val; - } - - // Set the default index key if there's no other way. - if (is_int($key) || !$this->isValidNodeName($key)) { - $cey = $key; - $key = $this->indexKey; - // In order to keep the original index for none "list-like" arrays - // (numeric keys and none zero based indecies) add the original key - // as index attribute - if (is_int($cey) && !$isList) { - $value = [ - '@AAA' => [$this->indexAttrKey ?: 'index' => $cey], - $this->nodeValueKey ?: 'value' => $value, - ]; - } - } - - if (is_array($value) && !is_int($key) && $this->appendDomList($normalizer, $dom, $node, $key, $value)) { - continue; - } - - // if this is a non scalar value at this time, just set the - // value on the element - if ($this->isXMLElement($value)) { - $element = $dom->createElement($normalizer->normalize($key)); - $node->appendChild($element); - $this->setElementValue($dom, $element, $value); - continue; - } - - if ($this->isValidNodeName($key)) { - $this->appendDOMNode($dom, $node, $normalizer->normalize($key), $value, $hasAttributes); - } - } - } - - /** - * appendDomList - * - * @param NormalizerInterface $normalizer - * @param \DOMDocument $dom - * @param mixed $node - * @param mixed $key - * @param mixed $value - * - * @return bool - */ - protected function appendDomList(NormalizerInterface $normalizer, \DOMDocument $dom, $node, $key, $value) - { - if (!Arr::isList($value, true)) { - return false; - } - - if (($useKey = ($skey = $this->inflect($key)) && ($key !== $skey)) || (null !== $this->indexKey)) { - - $parentNode = $dom->createElement($key); - - if (!$useKey) { - $parentNode = $dom->createElement($key); - $this->buildXmlFromTraversable($dom, $parentNode, $value); - } else { - - foreach ($value as $arrayValue) { - $this->appendDOMNode($dom, $parentNode, $skey, $arrayValue); - } - } - - return $node->appendChild($parentNode); - } - - // if anything fails, append the domnodes directly. - foreach ($value as $arrayValue) { - $this->appendDOMNode($dom, $node, $this->inflect($normalizer->normalize($key)), $arrayValue); - } - - return true; - } - - /** - * isValidNodeName - * - * @param mixed $name - * - * @return bool - */ - protected function isValidNodeName($name) - { - return strlen($name) > 0 && false === strpos($name, ' ') && preg_match('#^[\pL_][\pL0-9._-]*$#ui', $name); - } - - /** - * inflect - * - * @param mixed $string - * - * @return string - */ - protected function inflect($string) - { - if (null === $this->inflector) { - return $string; - } - - return call_user_func($this->inflector, $string); - } - - /** - * mapAttributes - * - * @return bool - */ - private function mapAttributes(\DOMNode $node, $key, $value) - { - if (null !== $attrName = $this->getAttributeName($key)) { - foreach ((array)$value as $attrKey => $attrValue) { - $node->setAttribute($attrKey, $this->getAttributeValue($attrValue, $attrKey)); - } - - - return true; - } - - if (is_scalar($value) && $this->isMappedAttribute($node->nodeName, $key) && $this->isValidNodeName($key)) { - $node->setAttribute($key, $this->getAttributeValue($value, $key)); - - return true; - } - - return false; - } - - /** - * setElementValue - * - * @param DOMDocument $dom - * @param DOMElement $node - * @param mixed $value - * - * @return null|bool returns false if no value was set, else null; - */ - private function setElementValue(DOMDocument $dom, DOMElement $node, $value = null) - { - if ($value instanceof \SimpleXMLElement) { - return $node->appendChild($dom->importNode(dom_import_simplexml($value), true)); - } - - if ($value instanceof \DOMDocument) { - return $node->appendDomElement($value->firstChild); - } - - if ($value instanceof \DOMElement) { - return $dom->appendDomElement($value, $node); - } - - if (is_array($value) || $value instanceof \Traversable) { - return $this->buildXML($dom, $node, $value); - } - - if (is_int($value) || is_float($value)) { - return $this->createText($dom, $node, (string)$value); - } - - if (is_bool($value)) { - return $this->createText($dom, $node, $value ? 'true' : 'false'); - } - - if (is_string($value) || null === $value) { - return $this->appendTextNode($dom, $node, (string)$value); - } - - return false; - } - - /** - * appendTextNode - * - * @param \DOMDocument $dom - * @param mixed $node - * @param mixed $value - * - * @return void - */ - private function appendTextNode(\DOMDocument $dom, $node, $value) - { - if (in_array(strtolower($value), ['true', 'false']) || is_numeric($value)) { - return $this->createTextNodeWithTypeAttribute($dom, $node, $value, 'string'); - } - if (preg_match('/(<|>|&)/i', $value)) { - return $this->createCDATASection($dom, $node, $value); - } - - return $this->createText($dom, $node, $value); - } - - /** - * Appends a dom node to the DOM. - * - * @param DOMNode $node - * @param string $name - * @param mixed $value - * @param bool $hasAttributes - * - * @return void - */ - private function appendDOMNode(\DOMDocument $dom, $node, $name, $value = null, $hasAttributes = false) - { - $element = $dom->createElement($name); - - if ($hasAttributes && $this->nodeValueKey === $name) { - $this->setElementValue($dom, $node, $value); - - return; - } - - $this->setElementValue($dom, $element, $value); - $node->appendChild($element); - } - - /** - * Extracts the raw attribute indicator. - * - * @param string $key string with leading `@`. - * - * @return string - */ - private function getAttributeName($key) - { - if (0 === strpos($key, '@') && $this->isValidNodeName($attrName = substr($key, 1))) { - return $attrName; - } - - return null; - } - - /** - * Creates a text node on a DOMNode. - * - * @param DOMNode $node - * @param string $value - * - * @return bool - */ - private function createText(\DOMDocument $dom, \DOMNode $node, $value) - { - $text = $dom->createTextNode($value); - $node->appendChild($text); - } - - /** - * Creates a CDATA section node on a DOMNode. - * - * @param DOMNode $node - * @param string $value - * - * @return void - */ - private function createCDATASection(\DOMDocument $dom, \DOMNode $node, $value) - { - $cdata = $dom->createCDATASection($value); - $node->appendChild($cdata); - } - - /** - * Add a value and an associated type attribute to a DOMNode. - * - * @param DOMNode $node - * @param mixed $value - * @param string $type - * - * @return void - */ - private function createTextNodeWithTypeAttribute(\DOMDocument $dom, \DOMNode $node, $value, $type = 'int') - { - $text = $dom->createTextNode($value); - $attr = $dom->createAttribute('type'); - $attr->value = $type; - - $node->appendChild($text); - $node->appendChild($attr); - } - - /** - * getValue - * - * @param mixed $value - * - * @return mixed - */ - private function getAttributeValue($value, $attrKey = null) - { - if (is_bool($value)) { - return $value ? 'true' : 'false'; - } - - return is_scalar($value) ? $value : null; - } -} diff --git a/lucid/xml/tests/Dom/DomDocumentTest.php b/lucid/xml/tests/Dom/DomDocumentTest.php deleted file mode 100644 index 39368a1..0000000 --- a/lucid/xml/tests/Dom/DomDocumentTest.php +++ /dev/null @@ -1,91 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Dom; - -use Lucid\Xml\Dom\DOMElement; -use Lucid\Xml\Dom\DOMDocument; - -/** - * @class DomDocumentTest - * - * @package Lucid\Xml - * @version $Id$ - */ -class DomDocumentTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('\DOMDocument', new DOMDocument); - $this->assertInstanceof('\Lucid\Xml\Dom\DOMDocument', new DOMDocument); - } - - /** @test */ - public function itShouldCreateDOMElementsWithRightClass() - { - $dom = new DOMDocument; - - $element = $dom->createElement('foo', 'bar'); - - $this->assertInstanceof('\Lucid\Xml\Dom\DOMElement', $element); - } - - /** @test */ - public function itShouldReturnRightClassWhenIteratingOverDomNodeList() - { - $xml = 'foobar'; - $dom = new DOMDocument; - - $dom->loadXML($xml, LIBXML_NONET); - - foreach ($dom->xpath('//foo|//bar') as $node) { - $this->assertInstanceof('\Lucid\Xml\Dom\DOMElement', $node); - } - } - - /** @test */ - public function itShouldAppendElementToAGivenReference() - { - $dom = new DOMDocument; - - $root = $dom->createElement('root'); - - $firstChild = $dom->createElement('data'); - $nextChild = $dom->createElement('foo', 'bar'); - - $dom->appendChild($root); - $root->appendChild($firstChild); - - $dom->appendDomElement($nextChild, $firstChild); - - $list = $dom->xpath('data/foo'); - - $this->assertSame($list->item(0), $nextChild); - } - - /** @test */ - public function itShouldAppendElementToItsFirstChildIfNoReferenceIsGiven() - { - $dom = new DOMDocument; - - $firstChild = $dom->createElement('data'); - $nextChild = $dom->createElement('foo', 'bar'); - - $dom->appendChild($firstChild); - - $dom->appendDomElement($nextChild); - - $list = $dom->xpath('foo'); - - $this->assertSame($list->item(0), $nextChild); - } -} diff --git a/lucid/xml/tests/Dom/DomElementTest.php b/lucid/xml/tests/Dom/DomElementTest.php deleted file mode 100644 index 5f55179..0000000 --- a/lucid/xml/tests/Dom/DomElementTest.php +++ /dev/null @@ -1,61 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Dom; - -use Lucid\Xml\Dom\DOMElement; -use Lucid\Xml\Dom\DOMDocument; - -/** - * @class DomElementTest extends \PHPUnit_Framework_TestCase - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -class DomElementTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('\DOMElement', new DOMElement('foo')); - $this->assertInstanceof('\Lucid\Xml\Dom\DOMElement', new DOMElement('foo')); - } - - /** @test */ - public function xpathShouldThrowExceptionWithoutOwnerDocument() - { - $element = new DOMElement('foo'); - - try { - $element->xpath('//foo'); - } catch (\BadMethodCallException $e) { - $this->assertSame('cannot xpath on element without an owner document', $e->getMessage()); - } catch (\Exception $e) { - $this->fail($e->getMessage()); - } - } - - /** @test */ - public function appendDomElementShouldThrowExceptionWithoutOwnerDocument() - { - $element = new DOMElement('foo'); - - try { - $element->appendDomElement(new DOMElement('bar')); - } catch (\BadMethodCallException $e) { - $this->assertSame('cannot add an element without an owner document', $e->getMessage()); - } catch (\Exception $e) { - $this->fail($e->getMessage()); - } - } -} diff --git a/lucid/xml/tests/Fixures/test.xml b/lucid/xml/tests/Fixures/test.xml deleted file mode 100644 index 3c60217..0000000 --- a/lucid/xml/tests/Fixures/test.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - test - diff --git a/lucid/xml/tests/Inflector/SimpleInflectorTest.php b/lucid/xml/tests/Inflector/SimpleInflectorTest.php deleted file mode 100644 index 31dfb23..0000000 --- a/lucid/xml/tests/Inflector/SimpleInflectorTest.php +++ /dev/null @@ -1,78 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Inflector; - -use Lucid\Xml\Inflector\SimpleInflector; - -/** - * @class SimpleInflectorTest - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - */ -class SimpleInflectorTest extends \PHPUnit_Framework_TestCase -{ - /** - * @test - * @dataProvider singularProvider - */ - public function itShouldPluralizeStrings($singular, $plural) - { - $inflector = new SimpleInflector(true); - - $this->assertEquals($plural, $inflector->pluralize($singular)); - } - - /** - * @test - * @dataProvider singularProvider - */ - public function itShouldSingularizeStrings($singular, $plural) - { - $inflector = new SimpleInflector(true); - - $this->assertEquals($singular, $inflector->singularize($plural)); - } - - /** @test */ - public function itShouldNotSingularizeStringOnNoMatchAndNoTrailing() - { - $inflector = new SimpleInflector(true); - $this->assertEquals('xx', $inflector->singularize('xx')); - - $inflector = new SimpleInflector(false); - $this->assertEquals('xx', $inflector->singularize('xx')); - } - - /** @test */ - public function itShouldPluralizeStringOnNoMatch() - { - $inflector = new SimpleInflector(true); - $this->assertEquals('asxs', $inflector->pluralize('asx')); - - $inflector = new SimpleInflector(false); - $this->assertEquals('ax', $inflector->pluralize('ax')); - } - - public function singularProvider() - { - return [ - ['fox', 'foxes'], - ['box', 'boxes'], - ['entity', 'entities'], - ['repository', 'repositories'], - ['alias', 'aliases'], - ['pun', 'puns'], - ]; - } -} diff --git a/lucid/xml/tests/Loader/LoaderTest.php b/lucid/xml/tests/Loader/LoaderTest.php deleted file mode 100644 index 64c6aae..0000000 --- a/lucid/xml/tests/Loader/LoaderTest.php +++ /dev/null @@ -1,118 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Loader; - -use Lucid\Xml\Loader\Loader; - -/** - * @class XmlLoaderTest - * - * @package Lucid\Xml - * @version $Id$ - */ -class LoaderTest extends \PHPUnit_Framework_TestCase -{ - - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('\Lucid\Xml\Loader\LoaderInterface', new Loader); - } - - /** @test */ - public function itShouldLoadXmlFiles() - { - $file = $this->getFixure(); - - $loader = new Loader; - - $xml = $loader->load($file); - $this->assertInstanceof('Lucid\Xml\Dom\DOMDocument', $xml); - - $this->assertInstanceof('DOMDocument', $xml); - - $xml = $loader->loadDom($file); - $this->assertInstanceof('Lucid\Xml\Dom\DOMDocument', $xml); - - $this->assertInstanceof('DOMDocument', $xml); - - $xml = $loader->load($file, [Loader::SIMPLEXML => true]); - $this->assertInstanceof('Lucid\Xml\SimpleXMLElement', $xml); - - $xml = $loader->loadSimpleXml($file); - $this->assertInstanceof('Lucid\Xml\SimpleXMLElement', $xml); - } - - /** @test */ - public function itShouldLoadXmlStrings() - { - $loader = new Loader; - - $xml = $loader->loadDom('', [Loader::FROM_STRING => true]); - $this->assertInstanceOf('DOMDocument', $xml); - } - - /** @test */ - public function domClassesShouldBeSettable() - { - $file = $this->getFixure(); - - $loader = new Loader; - - $xml = $loader->loadDom($file, [Loader::DOM_CLASS => 'DOMDocument']); - - $this->assertFalse($xml instanceof \Lucid\Xml\Dom\DOMDocument); - $this->assertInstanceOf('DOMDocument', $xml); - } - - /** @test */ - public function simpleXmlClassesShouldBeSettable() - { - $file = $this->getFixure(); - - $loader = new Loader; - - $xml = $loader->loadSimpleXml($file, [Loader::SIMPLEXML_CLASS => 'SimpleXMLElement']); - - $this->assertFalse($xml instanceof \Lucid\Xml\SimpleXmlElement); - $this->assertInstanceof('SimpleXMLElement', $xml); - } - - /** @test */ - public function loadingInvalidXmlShouldThrowExcepton() - { - $file = $this->getFixure(); - - $loader = new Loader; - try { - $loader->loadDom('', [Loader::FROM_STRING => true]); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true); - return; - } catch (\Exception $e) { - $this->fail($e->getMessage()); - } - - $this->fail('test failed'); - } - - /** - * get the fixure file - * - * @access protected - * @return string - */ - private function getFixure() - { - return dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR.'test.xml'; - } -} diff --git a/lucid/xml/tests/Normalizer/NormalizerTest.php b/lucid/xml/tests/Normalizer/NormalizerTest.php deleted file mode 100644 index 456c6c4..0000000 --- a/lucid/xml/tests/Normalizer/NormalizerTest.php +++ /dev/null @@ -1,220 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer; - -use Lucid\Xml\Normalizer\Normalizer; -use Lucid\Xml\Normalizer\NormalizerInterface; -use Lucid\Xml\Tests\Normalizer\Stubs\ArrayableStub; -use Lucid\Xml\Tests\Normalizer\Stubs\ConvertToArrayStub; -use Lucid\Xml\Tests\Normalizer\Stubs\SinglePropertyStub; -use Lucid\Xml\Tests\Normalizer\Stubs\NestedPropertyStub; -use Lucid\Xml\Tests\Normalizer\Stubs\TraversableStub; - -/** - * @class NormalizerTest - * - * @package Lucid\Xml - * @version $Id$ - * @author Thomas Appel - * @license MIT - */ -class NormalizerTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $normalizer = new Normalizer; - $this->assertInstanceof('\Lucid\Xml\Normalizer\Normalizer', $normalizer); - } - - public function stringProvider() - { - return [ - ['foo-bar', 'fooBar'], - ['foo-bar', 'foo_bar'], - ['foo-bar', 'foo:bar'], - ['foo-bar', 'foo.bar'], - ['foo', '_foo'], - ['foo', '%foo'] - ]; - } - - /** - * @test - * @dataProvider stringProvider - */ - public function itShouldNormalizeInputToExpectedValue($expected, $value) - { - $normalizer = new Normalizer; - - $this->assertEquals($expected, $normalizer->normalize($value)); - } - - /** - * @test - */ - public function testConvertObjectToArray() - { - $normalizer = new Normalizer; - - $object = new ConvertToArrayStub; - $data = $normalizer->ensureArray($object); - - $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $data); - } - - /** @test */ - public function itShouldConvertObjectToArrayExtened() - { - $normalizer = new Normalizer; - - $data = array('foo' => new SinglePropertyStub); - $normalized = $normalizer->ensureArray($data); - - $this->assertEquals(array('foo' => array('baz' => 'bazvalue')), $normalized); - } - - /** @test */ - public function isShouldConvertNestedObjectProperties() - { - $normalizer = new Normalizer; - - $data = array('foo' => new NestedPropertyStub); - $normalized = $normalizer->ensureArray($data); - - $this->assertEquals(['foo' => ['baz' => ['foo' => 'foo', 'bar' => 'bar']]], $normalized); - } - - /** @test */ - public function isShouldIgnoreIgnoredObjects() - { - $normalizer = new Normalizer; - - $normalizer->setIgnoredObjects((array)'\Lucid\Xml\Tests\Normalizer\Stubs\NestedPropertyStub'); - $data = ['foo' => ['bar' => new NestedPropertyStub]]; - $normalized = $normalizer->ensureArray($data); - - $this->assertEquals(['foo' => ['bar' => null]], $normalized); - } - - /** @test */ - public function isShouldConvertArrayableObjectToArray() - { - $normalizer = new Normalizer; - - $data = ['foo' => 'foo', 'bar' => 'bar']; - $object = new ArrayableStub($data); - $this->assertEquals($data, $normalizer->ensureArray($object)); - } - - /** @test */ - public function itShouldConvertObjectToArrayAndIgnoreRecursion() - { - $normalizer = new Normalizer; - - $data = ['bar' => 'bar', 'foo' => [null]]; - - $objectA = new ConvertToArrayStub(); - - $foo = [$objectA]; - $objectA->setFoo($foo); - - $out = $normalizer->ensureArray($objectA); - $this->assertEquals($data, $out); - } - - /** @test */ - public function itShouldConvertArrayableObjectToArrayAndIgnoreAttributes() - { - $normalizer = new Normalizer; - - $normalizer->setIgnoredAttributes(['foo']); - - $data = array('foo' => 'foo', 'bar' => 'bar'); - $object = new ArrayableStub($data); - - $this->assertEquals(['bar' => 'bar'], $normalizer->ensureArray($data)); - $this->assertEquals(['bar' => 'bar'], $normalizer->ensureArray($object)); - - $normalizer->addIgnoredAttribute('bar'); - - $this->assertEquals([], $normalizer->ensureArray($data)); - } - - - /** @test */ - public function itShouldIgnoreIgnoredClasses() - { - $normalizer = new Normalizer; - - $normalizer->setIgnoredAttributes(['foo']); - - $data = new \StdClass; - $data->foo = 'bar'; - $data->bar = 'baz'; - - $this->assertEquals(['bar' => 'baz'], $normalizer->ensureArray($data)); - } - - /** @test */ - public function itShouldConvertObjects() - { - $data = new \DOMDocument; - $data->loadXML('bar'); - - $normalizer = new Normalizer; - $this->assertSame($data, $normalizer->ensureBuildable($data)); - - - $data = new TraversableStub($asset = ['foo' => 'bar', 'bam' => 'baz']); - - $this->assertSame($asset, $normalizer->ensureArray($data)); - - $data = ['foo' => $dom = new \DOMDocument]; - $dom->loadXML('bar'); - - $normalizer = new Normalizer; - $this->assertSame($data, $normalizer->ensureBuildable($data)); - - } - - /** @test */ - public function itShouldConvertObjectsAndIgnoreCallableProperties() - { - $obj = new ConvertToArrayStub; - - $this->assertTrue(is_callable($obj->getBaz(), 'stub getter should return callable')); - - $normalizer = new Normalizer; - $this->assertEquals(['foo' => 'foo', 'bar' => 'bar'], $normalizer->ensureArray($obj)); - } - - /** @test */ - public function itShouldIgnoredNonArrayables() - { - $normalizer = new Normalizer; - $this->assertNull($normalizer->ensureArray('string')); - } - - /** @test */ - public function itShouldEnsureBuildable() - { - $normalizer = new Normalizer; - $xml = new \DOMDocument; - - $data = new TraversableStub($asset = ['foo' => 'bar', 'bam' => 'baz']); - - $this->assertSame($xml, $normalizer->ensureBuildable($xml)); - $this->assertSame($asset, $normalizer->ensureBuildable($data)); - $this->assertNull($normalizer->ensureBuildable(null)); - } -} diff --git a/lucid/xml/tests/Normalizer/Stubs/ArrayableStub.php b/lucid/xml/tests/Normalizer/Stubs/ArrayableStub.php deleted file mode 100644 index 2d2fa65..0000000 --- a/lucid/xml/tests/Normalizer/Stubs/ArrayableStub.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer\Stubs; - -/** - * @class ConvertToArrayStub - */ -class ArrayableStub extends ConvertToArrayStub -{ - protected $data; - - public function __construct(array $data) - { - $this->data = $data; - } - public function toArray() - { - return $this->data; - } -} diff --git a/lucid/xml/tests/Normalizer/Stubs/ConvertToArrayStub.php b/lucid/xml/tests/Normalizer/Stubs/ConvertToArrayStub.php deleted file mode 100644 index d4cc459..0000000 --- a/lucid/xml/tests/Normalizer/Stubs/ConvertToArrayStub.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer\Stubs; - -/** - * @class ConvertToArrayStub - */ -class ConvertToArrayStub -{ - /** - * foo - * - * @var string - */ - protected $foo = 'foo'; - - /** - * bar - * - * @var string - */ - protected $bar = 'bar'; - - protected $baz; - - public function __construct() - { - $this->baz = function () { - }; - } - - /** - * getFoo - * - * @param mixed $param - * @access public - * @return mixed - */ - public function getBar() - { - return $this->bar; - } - - /** - * getFoo - * - * @param mixed $param - * @access public - * @return mixed - */ - public function getFoo() - { - return $this->foo; - } - - public function getBaz() - { - return $this->baz; - } - - public function getAttributes($param) - { - return array('attributes'); - } - - public function setBar($bar) - { - $this->bar = $bar; - } - - public function setFoo($foo) - { - $this->foo = $foo; - } -} diff --git a/lucid/xml/tests/Normalizer/Stubs/NestedPropertyStub.php b/lucid/xml/tests/Normalizer/Stubs/NestedPropertyStub.php deleted file mode 100644 index 337c230..0000000 --- a/lucid/xml/tests/Normalizer/Stubs/NestedPropertyStub.php +++ /dev/null @@ -1,26 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer\Stubs; - -/** - * @class NestedPropertyStub - * @package Lucid\Xml\Tests\Normalizer\Stubs - * @version $Id$ - */ -class NestedPropertyStub -{ - public $baz; - public function __construct() - { - $this->baz = new ConvertToArrayStub; - } -} diff --git a/lucid/xml/tests/Normalizer/Stubs/SinglePropertyStub.php b/lucid/xml/tests/Normalizer/Stubs/SinglePropertyStub.php deleted file mode 100644 index f297438..0000000 --- a/lucid/xml/tests/Normalizer/Stubs/SinglePropertyStub.php +++ /dev/null @@ -1,17 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer\Stubs; - -class SinglePropertyStub -{ - public $baz = 'bazvalue'; -} diff --git a/lucid/xml/tests/Normalizer/Stubs/TraversableStub.php b/lucid/xml/tests/Normalizer/Stubs/TraversableStub.php deleted file mode 100644 index 966cfa3..0000000 --- a/lucid/xml/tests/Normalizer/Stubs/TraversableStub.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests\Normalizer\Stubs; - -/** - * @class TraversableStub - * @package Normalizer\Stubs - * @version $Id$ - */ -class TraversableStub implements \IteratorAggregate -{ - protected $data; - - public function __construct(array $data = []) - { - $this->data = $data; - } - - public function getIterator() - { - return new \ArrayIterator($this->data); - } -} diff --git a/lucid/xml/tests/ParserTest.php b/lucid/xml/tests/ParserTest.php deleted file mode 100644 index ff16378..0000000 --- a/lucid/xml/tests/ParserTest.php +++ /dev/null @@ -1,367 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests; - -use Lucid\Xml\Parser; -use Lucid\Xml\Dom\DOMElement; -use Lucid\Xml\Dom\DOMDocument; -use Lucid\Xml\Loader\LoaderInterface; - -/** - * @class ParserTest - * - * @package Lucid\Xml\Tests - * @version $Id$ - * @author Thomas Appel - */ -class ParserTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $parser = new Parser($this->mockLoader()); - $this->assertInstanceof('\Lucid\Xml\Parser', $parser); - - $parser = new Parser; - $this->assertInstanceof('\Lucid\Xml\Parser', $parser); - } - - /** @test */ - public function itShouldParseAXmlString() - { - $xml = 'test'; - $parser = new Parser; - - $data = $parser->parse($xml); - - $this->assertEquals(['root' => ['data' => 'test']], $data); - } - - /** @test */ - public function itShouldMakeThings() - { - $xml = ' - - guff - more guff - - '; - $parser = new Parser; - $parser->setIndexAttributeKey('key'); - $parser->setIndexKey('item'); - - $data = $parser->parse($xml); - } - /** @test */ - public function itShouldParseAXmlFile() - { - $file = __DIR__.DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR.'test.xml'; - $parser = new Parser; - $data = $parser->parse($file); - - $this->assertEquals(['root' => ['data' => 'test']], $data); - } - - /** @test */ - public function itShouldParseDom() - { - $parser = new Parser; - - $dom = new DOMDocument; - $element = $dom->createElement('foo'); - $element->appendDomElement(new DOMElement('bar', 'baz')); - $dom->appendChild($element); - - $this->assertEquals(['foo' => ['bar' => 'baz']], $parser->parseDom($dom)); - } - - /** @test */ - public function itShouldParseDomElements() - { - $parser = new Parser; - - $dom = new DOMDocument; - $element = $dom->createElement('foo'); - $element->appendDomElement(new DOMElement('bar', 'baz')); - - $this->assertEquals(['bar' => 'baz'], $parser->parseDomElement($element)); - } - - /** @test */ - public function itShouldThrowExceptionIfDOMisEmpty() - { - $parser = new Parser; - $dom = new DOMDocument; - - try { - $parser->parseDom($dom); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('DOM has no root element', $e->getMessage()); - return; - } - - $this->fail('you lose'); - } - - /** @test */ - public function itShouldRespectPluralizedParentKeys() - { - $parser = new Parser; - - $parser->setPluralizer(function ($string) { - return $string . 's'; - }); - - - $xmlString = '1'; - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['aas' => [1]]], $data); - - $parser->setIndexKey('item'); - - $this->assertSame('item', $parser->getIndexKey()); - - $xmlString = '1'; - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['aas' => [1]]], $data); - } - - /** @test */ - public function itShouldParseAttributesAsKeys() - { - $parser = new Parser; - $parser->setMergeAttributes(true); - - $xmlString = 'bar'; - $data = $parser->parse($xmlString); - $this->assertEquals(['data' => ['foo' => ['id' => 1, 'value' => 'bar']]], $data); - } - - /** @test */ - public function itShouldParseAttributesAsArray() - { - $parser = new Parser; - $parser->setMergeAttributes(false); - - $xmlString = 'bar'; - $data = $parser->parse($xmlString); - $this->assertEquals(['data' => ['foo' => ['@attributes' => ['id' => 1], 'value' => 'bar']]], $data); - } - - /** @test */ - public function attributeKeyNameShouldBeSettable() - { - $parser = new Parser; - $parser->setMergeAttributes(false); - $parser->setAttributesKey('attrs'); - - $xmlString = 'bar'; - $data = $parser->parse($xmlString); - $this->assertEquals(['data' => ['foo' => ['attrs' => ['id' => 1], 'value' => 'bar']]], $data); - } - - /** @test */ - public function itIsSetInnerTextAsValueKey() - { - $parser = new Parser; - - $xmlString = 'barfoo'; - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['foo' => ['inner' => 'foo', 'value' => 'bar']]], $data); - } - - /** @test */ - public function itShouldNormalizeKeys() - { - $parser = new Parser; - $parser->setKeyNormalizer(function ($key) { - return strtr($key, ['-' => '_']); - }); - - $xmlString = 'foo'; - $data = $parser->parse($xmlString); - $this->assertEquals(['data' => ['test_name' => 'foo']], $data); - } - - /** @test */ - public function itShouldHandleRegularDomDocuments() - { - $parser = new Parser; - - $dom = new \DOMDocument; - $dataNode = $dom->createElement('data'); - $dom->appendChild($dataNode); - - $data = $parser->parseDom($dom); - - $this->assertEquals(['data' => null], $data); - } - - /** @test */ - public function itShouldParseItemsAndRespectArrayNotaion() - { - $xmlString = - ' - - a - b - c - - '; - - $parser = new Parser; - $parser->setIndexKey('item'); - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['items' => ['a', 'b', 'c']]], $data); - - $parser = new Parser; - $parser->setPluralizer(function ($string) { - return $string . 's'; - }); - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['items' => ['a', 'b', 'c']]], $data); - } - - /** @test */ - public function itShouldContinueIndexOnParsingMixedListStructure() - { - $xmlString = - ' - - a - b - c - d - e - - '; - - $parser = new Parser; - $parser->setPluralizer(function ($string) { - return $string . 's'; - }); - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['items' => ['a', 'b', 'c', 'bla' => 'd', 'e']]], $data); - - $parser = new Parser; - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['items' => ['item' => ['a', 'b', 'c', 'e'], 'bla' => 'd']]], $data); - } - - /** @test */ - public function itShouldParseMessedUpKeys() - { - $xmlString = - ' - - a - b - c - d - e - - '; - - $parser = new Parser; - $parser->setPluralizer(function ($string) { - return $string . 's'; - }); - - $data = $parser->parse($xmlString); - - $this->assertEquals(['data' => ['items' => [ 2 => 'a', 5 => 'b', 44 => 'c', 'bla' => 'd', 9 => 'e']]], $data); - - $xml = - ' - guff - more guff - '; - $parser = new Parser; - $parser->setIndexAttributeKey('key'); - $parser->setIndexKey('item'); - - $data = $parser->parse($xml); - - $this->assertEquals(['data' => [22 => 'guff', 2 => 'more guff']], $data); - - $xml = - ' - guff - more guff - '; - $parser = new Parser; - $parser->setIndexKey('item'); - - $data = $parser->parse($xml); - - $this->assertEquals(['data' => [22 => 'guff', 2 => 'more guff']], $data); - } - - /** @test */ - public function staticHelperTest() - { - $dom = new DOMDocument; - - $elementParent = $dom->createElement('data'); - $elementChild = $dom->createElement('foo', 'bar'); - $elementParent->appendChild($elementChild); - - $this->assertEquals(['foo' => 'bar'], Parser::getPhpValue($elementParent)); - $this->assertNull(Parser::getPhpValue('')); - $this->assertSame(10, Parser::getPhpValue('10')); - $this->assertSame(1.2, Parser::getPhpValue('1.2')); - $this->assertTrue(Parser::getPhpValue('true')); - $this->assertFalse(Parser::getPhpValue('false')); - $this->assertSame(3840, Parser::getPhpValue('0xF00')); - $this->assertSame('0xNaN', Parser::getPhpValue('0xNaN')); - } - - /** @test */ - public function itIsExpectedThat() - { - $xml = - ' - - 1 - 2 - 3 - - '; - - $parser = new Parser; - $parser->setPluralizer(function ($string) { - return $string . 's'; - }); - - $result = $parser->parse($xml); - } - - private function mockLoader() - { - return $this->getMockbuilder('\Lucid\Xml\Loader\LoaderInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/lucid/xml/tests/SimpleXMLElementTest.php b/lucid/xml/tests/SimpleXMLElementTest.php deleted file mode 100644 index 3f4706f..0000000 --- a/lucid/xml/tests/SimpleXMLElementTest.php +++ /dev/null @@ -1,136 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests; - -use Lucid\Xml\Dom\DOMDocument; -use Lucid\Xml\SimpleXMLElement; - -/** - * @class simpleXMLElementTest - * @package Lucid\Xml - * @version $Id$ - */ -class SimpleXmlElementTest extends \PHPUnit_Framework_TestCase -{ - /** @test */ - public function itShouldBeInstantiable() - { - $this->assertInstanceof('\SimpleXMLElement', new SimpleXMLElement('')); - } - - /** @test */ - public function itShouldParseAttributesAsArray() - { - $data = - ' - '; - $xml = new SimpleXMLElement($data); - - $this->assertSame(['id' => 12, 'entry' => 'foo'], $xml->attributesAsArray()); - } - - /** @test */ - public function itShouldConvertToPhpValues() - { - $xml = new SimpleXMLElement('12'); - - $this->assertTrue(is_int($xml->phpValue())); - - $xml = new SimpleXMLElement('12.1'); - - $this->assertTrue(is_float($xml->phpValue())); - - $xml = new SimpleXMLElement('0xff0000'); - - $this->assertTrue(is_int($xml->phpValue())); - - $xml = new SimpleXMLElement('0xNaN'); - - $this->assertTrue(is_string($xml->phpValue())); - - $xml = new SimpleXMLElement('true'); - - $this->assertTrue($xml->phpValue()); - - $xml = new SimpleXMLElement('false'); - - $this->assertFalse($xml->phpValue()); - } - - /** @test */ - public function itShouldAppendCdataSections() - { - - $xml = new SimpleXMLElement(''); - - $xml->addCDATASection('string'); - - $this->assertXmlStringEqualsXmlString('', $xml->asXML()); - - $xml = new SimpleXMLElement(''); - $xml->addCDATASection(new \SimpleXMLElement('string')); - - $this->assertXmlStringEqualsXmlString('string]]>', $xml->asXML()); - - $xml = new SimpleXMLElement(''); - $dom = new \DOMDocument; - $dom->loadXML('string'); - $xml->addCDATASection($dom); - - $this->assertXmlStringEqualsXmlString('string]]>', $xml->asXML()); - - $xml = new SimpleXMLElement(''); - $dom = new DOMDocument; - $dom->loadXML('string'); - $xml->addCDATASection($dom->xpath('data')->item(0)); - - $this->assertXmlStringEqualsXmlString('string]]>', $xml->asXML()); - - - $xml = new SimpleXMLElement(''); - - try { - $xml->addCDATASection([]); - } catch (\InvalidArgumentException $e) { - return $this->assertTrue(true); - } - } - - /** @test */ - public function itShouldAppendHTMLString() - { - $xml = new SimpleXMLElement(''); - - $xml->appendChildFromHtmlString('link'); - - $this->assertXmlStringEqualsXmlString('link', $xml->asXML()); - } - - - /** @test */ - public function itShouldAppendXmlStrings() - { - $xml = new SimpleXMLElement(''); - $xml->appendChildFromXmlString('bar'); - - $this->assertXmlStringEqualsXmlString('bar', $xml->asXML()); - } - - /** @test */ - public function itShouldAppendChildNodes() - { - $xml = new SimpleXMLElement(''); - $xml->appendChildNode(new \SimpleXMLElement('bar')); - - $this->assertXmlStringEqualsXmlString('bar', $xml->asXML()); - } -} diff --git a/lucid/xml/tests/WriterTest.php b/lucid/xml/tests/WriterTest.php deleted file mode 100644 index c99db0a..0000000 --- a/lucid/xml/tests/WriterTest.php +++ /dev/null @@ -1,538 +0,0 @@ - - * - * For full copyright and license information, please refer to the LICENSE file - * that was distributed with this package. - */ - -namespace Lucid\Xml\Tests; - -use Lucid\Xml\Writer; -use Lucid\Xml\Normalizer\Normalizer; -use Lucid\Xml\Normalizer\NormalizerInterface; - -/** - * @class WriterTest - * @package Lucid\Xml - * @version $Id$ - */ -class WriterTest extends \PHPUnit_Framework_TestCase -{ - - /** @test */ - public function itShouldBeInstantiable() - { - $writer = new Writer($this->mockNormalizer()); - $this->assertInstanceof('\Lucid\Xml\Writer', $writer); - - $w = new Writer(new Normalizer); - $w->useKeyAsValue('value'); - $args = [ - 'foo' => ['@attributes' => ['bar' => 'baz'], 'value' => 'tab'] - ]; - } - - - /** @test */ - public function itSouldDumpAnXmlString() - { - $writer = new Writer($n = $this->mockNormalizer()); - - $n->method('ensureBuildable')->with([])->willReturn([]); - $xml = $writer->dump([]); - - $this->assertXmlStringEqualsXmlString('', $xml); - - $writer = new Writer($n = $this->mockNormalizer()); - - $n->method('ensureBuildable')->with($args = ['bar' => 'baz'])->willReturn($args); - - $n->method('normalize')->willReturnCallback(function ($arg) { - return $arg; - }); - - $xml = $writer->dump($args, 'foo'); - $this->assertXmlStringEqualsXmlString( - ' - baz - ', - $xml - ); - } - - /** @test */ - public function itShouldWriteToADOMDocument() - { - $writer = new Writer($n = $this->mockNormalizer()); - - $n->method('ensureBuildable')->with([])->willReturn([]); - $xml = $writer->writeToDom([]); - - $this->assertInstanceof('DOMDocument', $xml); - } - - /** @test */ - public function itShouldInflectPlurals() - { - $args = [ - 'tags' => ['mysql', 'postgres'] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $writer->setInflector(function ($value) { - return strrpos($value, 's') === (strlen($value) - 1) ? substr($value, 0, -1) : $value; - }); - - $xml = $writer->dump($args); - $this->assertXmlStringEqualsXmlString( - ' - - mysql - postgres - - ', - $xml - ); - } - - /** @test */ - public function itShouldMappAttributes() - { - $args = [ - 'foo' => ['id' => 10, 'val' => 'value'] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $writer->addMappedAttribute('foo', 'id'); - - $xml = $writer->dump($args); - $this->assertXmlStringEqualsXmlString( - ' - - value - - ', - $xml - ); - - $writer = new Writer($this->getNormalizerMock()); - - $args = [ - 'bar' => ['id' => 10, 'val' => 'value'] - ]; - - $writer->addMappedAttribute('*', 'id'); - $xml = $writer->dump($args); - $this->assertXmlStringEqualsXmlString( - ' - - value - - ', - $xml - ); - } - - /** @test */ - public function itShouldMappAttributesFromAttributesMap() - { - $args = [ - 'foo' => ['soma' => true, 'val' => 'value'] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $writer->setAttributeMap([ - 'foo' => ['soma'] - ]); - - $xml = $writer->dump($args); - $this->assertXmlStringEqualsXmlString( - ' - - value - - ', - $xml - ); - } - - /** @test */ - public function itShouldIgnoreInvalidAttributeContent() - { - $args = [ - 'foo' => ['id' => [1, 2]] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $writer->setAttributeMap([ - 'foo' => ['id'] - ]); - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - - 1 - 2 - - - ', - $xml - ); - } - - /** @test */ - public function itShouldAddTypeToStringTypes() - { - $args = [ - 'foo' => ['value' => '2'] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - 2 - - ', - $xml - ); - - $args = [ - 'foo' => ['value' => 'true'] - ]; - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - true - - ', - $xml - ); - - $args = [ - 'foo' => ['value' => 'link'] - ]; - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - link]]> - - ', - $xml - ); - } - - /** @test */ - public function itShouldUseValueKeys() - { - $args = [ - 'foo' => ['@attributes' => ['id' => 10], 'value' => 'value'] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $writer->useKeyAsValue('value'); - - $xml = $writer->dump($args); - $this->assertXmlStringEqualsXmlString( - ' - value - ', - $xml - ); - } - - /** @test */ - public function itShouldUseIndexKeys() - { - $args = [ - 'foo' => [0, 1] - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - 0 - 1 - - ', - $xml - ); - - $writer->useKeyAsIndex('i'); - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - 0 - 1 - - ', - $xml - ); - } - - /** @test */ - public function itShouldShouldUseParentKeyAsIndexIfNoneSpecified() - { - $writer = new Writer($this->getNormalizerMock()); - - $writer->useKeyAsIndex(null); - - $args = [ - 'foo' => [0, 1] - ]; - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - 0 - 1 - ', - $xml - ); - } - - /** @test */ - public function itShouldConvertXmlElements() - { - $args = [ - 'foo' => new \DOMElement('bar', 'baz') - ]; - - $writer = new Writer($this->getNormalizerMock()); - - $xml = $writer->dump($args); - - $this->assertXmlStringEqualsXmlString( - ' - - baz - - ', - $xml - ); - } - - /** @test */ - public function itShouldNotParseInvalidKeyNames() - { - $writer = new Writer($this->getNormalizerMock()); - - try { - $writer->useKeyAsIndex($str = '%%adssad'); - } catch (\InvalidArgumentException $e) { - $this->assertSame($str . ' is an invalid node name', $e->getMessage()); - return; - } catch (\Exception $e) { - $this->fail($e->getMessage()); - } - - $this->fail('failed'); - - } - - /** @test */ - public function itShouldChangeThisTestName() - { - $writer = new Writer($this->getNormalizerMock()); - - try { - $writer->useKeyAsValue($str = '%%adssad'); - } catch (\InvalidArgumentException $e) { - $this->assertSame($str . ' is an invalid node name', $e->getMessage()); - return; - } catch (\Exception $e) { - $this->fail($e->getMessage()); - } - - $this->fail('failed'); - - } - - /** @test */ - public function itShouldGetCorretBooleans() - { - $data = ['foo' => true, 'bar' => false]; - $writer = new Writer($this->getNormalizerMock()); - - $xml = $writer->dump($data); - - $this->assertXmlStringEqualsXmlString( - ' - true - false - ', - $xml - ); - } - - /** @test */ - public function itShouldGetCorrectNumbers() - { - $data = ['foo' => 12, 'bar' => 1.2]; - $writer = new Writer($this->getNormalizerMock()); - - $xml = $writer->dump($data); - - $this->assertXmlStringEqualsXmlString( - ' - 12 - 1.2 - ', - $xml - ); - } - - /** @test */ - public function itShouldWriteSimpleValues() - { - $writer = new Writer($this->getNormalizerMock()); - $xml = $writer->dump(null); - - $this->assertXmlStringEqualsXmlString('', $xml); - - $xml = $writer->dump('foo'); - - $this->assertXmlStringEqualsXmlString('foo', $xml); - } - - /** - * @test - * @dataProvider indexedDataProvider - */ - public function itShouldWriteArrayStructures(array $data, $xml, $valueKey = null) - { - $writer = new Writer($this->getNormalizerMock()); - - if (null !== $valueKey) { - $writer->useKeyAsValue($valueKey); - } - - $this->assertXmlStringEqualsXmlString($xml, $writer->dump($data)); - } - - public function indexedDataProvider() - { - return [ - [ - [1, 2, 3, 4], - '1234' - ], - [ - [['nv' => 'bb', '@attrs' => ['index' => '22']]], - 'bb', 'nv' - ], - [ - [['@attrs' => ['index' => '22'], 'value' => 'bb']], - 'bb', 'value' - ], - [ - [33 => 'foo', 1 => 'bar', 0 => 'baz'], - 'foobarbaz', 'value' - ] - ]; - } - - /** @test */ - public function itShouldParseSimpleXmlObjects() - { - $writer = new Writer($this->getNormalizerMock()); - $xml = simplexml_load_string('bar'); - - $data = ['data' => $xml]; - - $xml = $writer->dump($data); - $this->assertXmlStringEqualsXmlString( - ' - - bar - - ', - $xml - ); - } - - /** @test */ - public function itShouldDoWiredStuff() - { - $dom = new \DOMDocument; - $el = $dom->createElement('foo', 'bar'); - $dom->appendChild($el); - - $writer = new Writer($this->getNormalizerMock()); - - $data = ['slam' => $dom]; - - $xml = $writer->dump($data); - $this->assertXmlStringEqualsXmlString( - ' - - bar - - ', - $xml - ); - } - - /** @test */ - public function itIsExpectedItWillIgnoreEmptyNodes() - { - $writer = new Writer(new Normalizer); - - $data = ['test' => ['foo' => null]]; - - $xml = $writer->dump($data, 'data'); - - $this->assertXmlStringEqualsXmlString('', $xml); - } - - protected function getNormalizerMock() - { - $n = $this->mockNormalizer(); - - $n->method('ensureBuildable')->willReturnCallback(function ($arg) { - return $arg; - }); - - $n->method('normalize')->willReturnCallback(function ($arg) { - return $arg; - }); - - return $n; - } - - private function mockNormalizer() - { - return $this->getMockbuilder('\Lucid\Xml\Normalizer\NormalizerInterface') - ->disableOriginalConstructor() - ->getMock(); - } -} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c37851b..fbd1e57 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,41 +10,17 @@ stopOnFailure="true" syntaxCheck="true"> - - lucid/cache/tests - - - lucid/common/tests - - - lucid/config/tests - - - lucid/di/tests - - - lucid/signal/tests - - - lucid/filesystem/tests - - - lucid/hash/tests - - - lucid/http/tests - - - lucid/mux/tests - - - lucid/template/tests - - - lucid/writer/tests - - - lucid/xml/tests + + ./tests + + + ./vendor + ./tests + + + ./src + + diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..ea75636 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +Cache/ diff --git a/src/Exception/MatchException.php b/src/Exception/MatchException.php new file mode 100644 index 0000000..82ae58d --- /dev/null +++ b/src/Exception/MatchException.php @@ -0,0 +1,27 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Exception; + +/** + * @class MatchException + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class MatchException extends \RuntimeException +{ + public static function noRouteMatch($request) + { + return new self(sprintf('No route found for requested resource "%s".', $request->getPath())); + } +} diff --git a/src/Exception/MissingValueException.php b/src/Exception/MissingValueException.php new file mode 100644 index 0000000..1fefdfa --- /dev/null +++ b/src/Exception/MissingValueException.php @@ -0,0 +1,25 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Exception; + +use UnexpectedValueException; + +/** + * @class MissingValueException + * + * @package Lucid\Mux\Exception + * @version $Id$ + * @author iwyg + */ +class MissingValueException extends UnexpectedValueException +{ +} diff --git a/src/Exception/ParserException.php b/src/Exception/ParserException.php new file mode 100644 index 0000000..1040474 --- /dev/null +++ b/src/Exception/ParserException.php @@ -0,0 +1,29 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Exception; + +use LogicException; + +/** + * @class ParserException + * + * @package Lucid\Mux\Exception + * @version $Id$ + * @author iwyg + */ +class ParserException extends LogicException +{ + public static function nestedOptional($var) + { + return new self(sprintf('Nested optional variable {%s?} has no default value.', $var)); + } +} diff --git a/lucid/signal/tests/Stubs/NamedEvent.php b/src/Exception/ResolverException.php similarity index 53% rename from lucid/signal/tests/Stubs/NamedEvent.php rename to src/Exception/ResolverException.php index feff0f3..69bfaa9 100644 --- a/lucid/signal/tests/Stubs/NamedEvent.php +++ b/src/Exception/ResolverException.php @@ -1,7 +1,7 @@ * @@ -9,17 +9,17 @@ * that was distributed with this package. */ -namespace Lucid\Signal\Tests\Stubs; +namespace Lucid\Mux\Exception; -use Lucid\Signal\Event; +use RuntimeException; /** - * @class NamedEvent + * @class ResolverException * - * @package Lucid\Signal\Tests\Stubs + * @package Lucid\Mux\Exception * @version $Id$ * @author iwyg */ -class NamedEvent extends Event +class ResolverException extends RuntimeException { } diff --git a/src/Handler/ContainerAwareResolverInterface.php b/src/Handler/ContainerAwareResolverInterface.php new file mode 100644 index 0000000..c7dcc5d --- /dev/null +++ b/src/Handler/ContainerAwareResolverInterface.php @@ -0,0 +1,33 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +use Interop\Container\ContainerInterface; + +/** + * @interface ContainerAwareResolverInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface ContainerAwareResolverInterface extends ResolverInterface +{ + /** + * Sets a container. + * + * @param ContainerInterface $container + * + * @return void + */ + public function setContainer(ContainerInterface $container); +} diff --git a/src/Handler/Dispatcher.php b/src/Handler/Dispatcher.php new file mode 100644 index 0000000..df9c2de --- /dev/null +++ b/src/Handler/Dispatcher.php @@ -0,0 +1,53 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +use Lucid\Mux\Matcher\ContextInterface as Match; + +/** + * @class Dispatcher + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Dispatcher implements DispatcherInterface +{ + private $resolver; + + private $mapper; + + /** + * Constructor. + * + * @param ParserInterface $parser + * @param ParameterMapperInterface $mapper + */ + public function __construct(ResolverInterface $resolver = null, ParameterMapperInterface $mapper = null) + { + $this->resolver = $resolver ?: new Resolver; + $this->mapper = $mapper ?: new PassParameterMapper; + } + + /** + * {@inheritdoc} + */ + public function dispatch(Match $context) + { + $args = $this->mapper->map( + $handler = $this->resolver->resolve($context->getHandler()), + $context->getVars() + ); + + return $handler->invokeArgs($args); + } +} diff --git a/src/Handler/DispatcherInterface.php b/src/Handler/DispatcherInterface.php new file mode 100644 index 0000000..03abf99 --- /dev/null +++ b/src/Handler/DispatcherInterface.php @@ -0,0 +1,33 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +use Lucid\Mux\Matcher\ContextInterface; + +/** + * @interface DispatcherInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface DispatcherInterface +{ + /** + * Dispatches a handler from a match context. + * + * @param ContextInterface $context + * + * @return void + */ + public function dispatch(ContextInterface $context); +} diff --git a/lucid/template/src/ComposeableInterface.php b/src/Handler/ParameterMapperInterface.php similarity index 51% rename from lucid/template/src/ComposeableInterface.php rename to src/Handler/ParameterMapperInterface.php index 9b64c93..d029617 100644 --- a/lucid/template/src/ComposeableInterface.php +++ b/src/Handler/ParameterMapperInterface.php @@ -1,7 +1,7 @@ * @@ -9,24 +9,24 @@ * that was distributed with this package. */ -namespace Lucid\Template; +namespace Lucid\Mux\Handler; /** - * @class ComposeableInterface + * @class ParameterMapperInterface * - * @package Lucid\Template + * @package Lucid\Mux * @version $Id$ * @author iwyg */ -interface ComposeableInterface +interface ParameterMapperInterface { /** - * compose + * map * - * @param mixed $template + * @param callable $handler * @param array $parameters * - * @return void + * @return array */ - public function compose($template, array $parameters = []); + public function map(Reflector $handler, array $parameters); } diff --git a/src/Handler/ParserInterface.php b/src/Handler/ParserInterface.php new file mode 100644 index 0000000..1ce1408 --- /dev/null +++ b/src/Handler/ParserInterface.php @@ -0,0 +1,24 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +/** + * @interface ParserInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface ParserInterface +{ + public function parse(ReferenceInterface $handler); +} diff --git a/lucid/writer/src/Object/CommentBlock.php b/src/Handler/PassParameterMapper.php similarity index 50% rename from lucid/writer/src/Object/CommentBlock.php rename to src/Handler/PassParameterMapper.php index 50708f0..c92f967 100644 --- a/lucid/writer/src/Object/CommentBlock.php +++ b/src/Handler/PassParameterMapper.php @@ -1,7 +1,7 @@ * @@ -9,24 +9,22 @@ * that was distributed with this package. */ -namespace Lucid\Writer\Object; - -use Lucid\Writer\WriterInterface; +namespace Lucid\Mux\Handler; /** - * @class CommentBlock + * @class ParameterMapper * - * @package Lucid\Writer + * @package Lucid\Mux * @version $Id$ * @author iwyg */ -class CommentBlock extends DocBlock +class PassParameterMapper implements ParameterMapperInterface { /** * {@inheritdoc} */ - protected function openBlock(WriterInterface $writer) + public function map(Reflector $handler, array $parameters) { - return $writer->writeln('/*'); + return array_values($parameters); } } diff --git a/lucid/cache/src/Client/MySql.php b/src/Handler/ReferenceInterface.php similarity index 60% rename from lucid/cache/src/Client/MySql.php rename to src/Handler/ReferenceInterface.php index bf16369..667eef0 100644 --- a/lucid/cache/src/Client/MySql.php +++ b/src/Handler/ReferenceInterface.php @@ -1,7 +1,7 @@ * @@ -9,15 +9,15 @@ * that was distributed with this package. */ -namespace Lucid\Cache\Client; +namespace Lucid\Mux; /** - * @class MySqlClient + * @interface ReferenceInterface * - * @package Lucid\Cache + * @package Lucid\Mux * @version $Id$ * @author iwyg */ -class MySql extends AbstractSqlClient +interface ReferenceInterface { } diff --git a/src/Handler/Reflector.php b/src/Handler/Reflector.php new file mode 100644 index 0000000..2a64215 --- /dev/null +++ b/src/Handler/Reflector.php @@ -0,0 +1,270 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +/** + * @class Reflector + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Reflector +{ + /** @var int */ + const C_TYPE_ERROR = 0; + + /** @var int */ + const C_TYPE_CLOSURE = 1; + + /** @var int */ + const C_TYPE_FUNCTION = 2; + + /** @var int */ + const C_TYPE_INSTANCE_METHOD = 3; + + /** @var int */ + const C_TYPE_STATIC_METHOD = 4; + + /** @var int */ + const C_TYPE_INVOKED_OBJECT = 5; + + /** @var int */ + private $type; + + /** @var callable */ + private $handler; + + /** @var \Reflector **/ + private $reflector; + + /** + * Constructor. + * + * @param callable $handler + */ + public function __construct(callable $handler, array $args = []) + { + $this->handler = $handler; + } + + /** + * getReflector + * + * @return ReflectionFunctionAbstract + */ + public function getReflector() + { + if (null === $this->reflector) { + $this->reflector = $this->doGetReflector(); + } + + return $this->reflector; + } + + /** + * invokeArgs + * + * @param array $args + * + * @return mixed + */ + public function invokeArgs(array $args) + { + return call_user_func_array($this->handler, $args); + } + + /** + * isFunction + * + * @return boolean + */ + public function isFunction() + { + return static::C_TYPE_FUNCTION === $this->getType(); + } + + /** + * isClosure + * + * @return boolean + */ + public function isClosure() + { + return static::C_TYPE_CLOSURE === $this->getType(); + } + + /** + * isMethod + * + * @return boolean + */ + public function isMethod() + { + return $this->isInstanceMethod() || $this->isStaticMethod(); + } + + /** + * isInstanceMethod + * + * @return boolean + */ + public function isInstanceMethod() + { + return static::C_TYPE_INSTANCE_METHOD === $this->getType(); + } + + /** + * isStaticMethod + * + * @return boolean + */ + public function isStaticMethod() + { + return static::C_TYPE_STATIC_METHOD === $this->getType(); + } + + /** + * isInvokedObject + * + * @return boolean + */ + public function isInvokedObject() + { + return static::C_TYPE_INVOKED_OBJECT === $this->getType(); + } + + /** + * getType + * + * @return int + */ + public function getType() + { + if (null === $this->type) { + $this->type = $this->getCallableType($this->handler); + } + + return $this->type; + } + + /** + * getCallableType + * + * @param callable $method + * + * @return int + */ + private function getCallableType(callable $method) + { + if ($method instanceof \Closure) { + return static::C_TYPE_CLOSURE; + } + + $callable = $this->explodeCallable($method); + + if (1 === count($callable)) { + if (is_object($callable[0])) { + return static::C_TYPE_INVOKED_OBJECT; + } + return static::C_TYPE_FUNCTION; + } else { + if (is_object($callable[0])) { + return static::C_TYPE_INSTANCE_METHOD; + } + + if (is_string($callable[0])) { + return static::C_TYPE_STATIC_METHOD; + } + } + + return static::C_TYPE_ERROR; + } + + /** + * getClass + * + * @return string + */ + private function getClass() + { + $parts = $this->explodeCallable($this->handler); + + if ($this->isStaticMethod()) { + return $parts[0]; + } + + return get_class($parts[0]); + } + + /** + * getMethod + * + * @return string + */ + private function getMethod() + { + if (is_array($this->handler) && 2 === sizeof($this->handler)) { + return $this->handler[1]; + } + + if ($this->isStaticMethod()) { + list (, $method) = explode('::', $this->handler); + + return $method; + } + + return ''; + } + + /** + * explodeCallable + * + * @param callable $method + * + * @return array + */ + private function explodeCallable(callable $method) + { + if (is_array($method)) { + return $method; + } + + if (is_string($method)) { + return explode('::', $method); + } + + return [$method]; + } + + /** + * doGetReflector + * + * @return ReflectionMethod + */ + private function doGetReflector() + { + switch ($this->getType()) { + case static::C_TYPE_FUNCTION: + case static::C_TYPE_CLOSURE: + return new \ReflectionFunction($this->handler); + case static::C_TYPE_STATIC_METHOD: + case static::C_TYPE_INSTANCE_METHOD: + return new \ReflectionMethod($this->getClass(), $this->getMethod()); + case static::C_TYPE_INVOKED_OBJECT: + return new \ReflectionMethod($this->getClass(), '__invoke'); + case static::C_TYPE_ERROR: + throw new \Exception; + break; + } + } +} diff --git a/src/Handler/Resolver.php b/src/Handler/Resolver.php new file mode 100644 index 0000000..54e9623 --- /dev/null +++ b/src/Handler/Resolver.php @@ -0,0 +1,151 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +use RuntimeException; +use Interop\Container\ContainerInterface; +use Lucid\Mux\Exception\ResolverException; + +/** + * @class ControllerResolver + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Resolver implements ContainerAwareResolverInterface +{ + /** @var ContainerInterface */ + private $container; + + /** + * Constructor. + * + * @param ContainerInterface $container + */ + public function __construct(ContainerInterface $container = null) + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + } + + /** + * {@inheritdoc} + */ + public function resolve($controller) + { + if (is_callable($callable = $this->findHandler($controller))) { + return new Reflector($callable); + }; + + throw new ResolverException('No routing handler could be found.'); + } + + /** + * resolveHandlerAndMethod + * + * @param string $handler + * + * @return array + */ + protected function resolveHandlerAndMethod($handler) + { + list ($handler, $method) = array_pad(explode('@', $handler), 2, null); + + return [$handler, $method]; + } + + /** + * findHandler + * + * @param mixed $handler + * + * @return callable + */ + private function findHandler($handler) + { + // if the handler is callable, return it immediately: + if (is_callable($handler)) { + return $handler; + } + + if (!is_string($handler)) { + throw new ResolverException(sprintf('Cannot resolver handler of type "%s".', gettype($handler))); + } + + list ($handler, $method) = $this->resolveHandlerAndMethod($handler); + + // if the service parameter is registererd as service, return the + // service object and its method as callable: + if ($service = $this->getService($handler)) { + return null === $method ? $service : [$service, $method]; + } + + if (!class_exists($handler)) { + return [$handler, $method]; + } + $err = null; + + set_error_handler(function ($errno, $msg) { + throw $this->newResolverException($msg); + }); + + try { + $resolvedHandler = [new $handler, $method]; + } catch (ResolverException $e) { + $err = $e; + } catch (\Throwable $t) { + $err = $this->newResolverException($t->getMessage()); + } + + restore_error_handler(); + + if (null !== $err) { + throw $err; + } + + return $resolvedHandler; + } + + /** + * @param string $id + * + * @return mixed + */ + private function getService($id) + { + if (null !== $this->container && $this->container->has($id)) { + return $this->container->get($id); + } + + return null; + } + + /** + * newResolverException + * + * @param string $msg + * + * @return ResolverException + */ + private function newResolverException($msg) + { + return new ResolverException('Can\'t resolve handler: '. $msg); + } +} diff --git a/lucid/cache/src/SectionableInterface.php b/src/Handler/ResolverInterface.php similarity index 50% rename from lucid/cache/src/SectionableInterface.php rename to src/Handler/ResolverInterface.php index 83fd1a8..33942b4 100644 --- a/lucid/cache/src/SectionableInterface.php +++ b/src/Handler/ResolverInterface.php @@ -1,7 +1,7 @@ * @@ -9,23 +9,23 @@ * that was distributed with this package. */ -namespace Lucid\Cache; +namespace Lucid\Mux\Handler; /** - * @interface Sectionable + * @interface ResolverInterface * - * @package Lucid\Cache + * @package Lucid\Mux * @version $Id$ * @author iwyg */ -interface SectionableInterface +interface ResolverInterface { /** - * Creates a cache section. + * resolve * - * @param string $section + * @param mixed $handler * - * @return CacheInterface + * @return Lucid\Mux\Handler\Reflector */ - public function section($section); + public function resolve($handler); } diff --git a/src/Handler/StrictParameterMapper.php b/src/Handler/StrictParameterMapper.php new file mode 100644 index 0000000..cbccabb --- /dev/null +++ b/src/Handler/StrictParameterMapper.php @@ -0,0 +1,84 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +use UnexpectedValueException; +use Lucid\Mux\Exception\MissingValueException; + +/** + * @class StrictParameterMapper + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class StrictParameterMapper implements ParameterMapperInterface +{ + /** + * types + * + * @var TypeMapCollectionInterface + */ + private $types; + + /** + * Constructor. + * + * @param TypeMapCollectionInterface $types + */ + public function __construct(TypeMapCollectionInterface $types = null) + { + $this->types = $types ?: new TypeMapCollection; + } + + /** + * {@inheritdoc} + * + * @throws UnexpectedValueException if required type cannot be mapped. + * @throws UnexpectedValueException if a none optional parameter is + * missing + * + * @return array + */ + public function map(Reflector $handler, array $parameters) + { + $params = []; + + foreach ($handler->getReflector()->getParameters() as $param) { + if (null !== ($class = $param->getClass())) { + if (!$this->types->has($class = $class->getName())) { + throw new MissingValueException( + sprintf('Cannot map class "%s" to parameter "{$%s}".', $class, $param->getName()) + ); + } + + $params[$param->getName()] = $this->types->get($class); + continue; + } + + if (array_key_exists($param->getName(), $parameters)) { + $params[$param->getName()] = $parameters[$param->getName()]; + continue; + } + + if (!$param->isOptional()) { + throw new MissingValueException( + sprintf('Missing non optional parameter "{$%s}".', $param->getName()) + ); + } + + $params[$param->getName()] = null; + } + + return array_values($params); + } +} diff --git a/src/Handler/TypeMapCollection.php b/src/Handler/TypeMapCollection.php new file mode 100644 index 0000000..496c962 --- /dev/null +++ b/src/Handler/TypeMapCollection.php @@ -0,0 +1,87 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +/** + * @class TypeMapCollection + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class TypeMapCollection implements TypeMapCollectionInterface +{ + /** + * Constructor. + * + * @param array $mappers + */ + public function __construct(array $mappers = []) + { + $this->set($mappers); + } + + /** + * {@inheritdoc} + */ + public function set(array $mappers) + { + $this->mappers = []; + array_map([$this, 'add'], $mappers); + } + + /** + * {@inheritdoc} + */ + public function add(TypeMapperInterface $mapper) + { + $this->mappers[$this->sanitize($mapper->getType())] = $mapper; + } + + /** + * {@inheritdoc} + */ + public function has($type) + { + return isset($this->mappers[$this->sanitize($type)]); + } + + /** + * {@inheritdoc} + */ + public function get($type) + { + return $this->getMapper($type)->getObject(); + } + + /** + * {@inheritdoc} + */ + public function getMapper($type) + { + if ($this->has($type)) { + return $this->mappers[$this->sanitize($type)]; + } + } + + /** + * Sanitize class name. + * + * @param string $type + * + * @return string + */ + private function sanitize($type) + { + return '\\'.ltrim($type, '\\'); + } +} diff --git a/src/Handler/TypeMapCollectionInterface.php b/src/Handler/TypeMapCollectionInterface.php new file mode 100644 index 0000000..5fd8922 --- /dev/null +++ b/src/Handler/TypeMapCollectionInterface.php @@ -0,0 +1,67 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +/** + * @interface TypeMapCollectionInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface TypeMapCollectionInterface +{ + /** + * Sets an array of typemapper objects. + * + * @param array $typeMappers + * + * @return void + */ + public function set(array $typeMappers); + + /** + * Add a typemapper object to the collection. + * + * @param TypeMapperInterface $typeMapper + * + * @return void + */ + public function add(TypeMapperInterface $typeMapper); + + /** + * Check if a mapper for a given type exists. + * + * @param string $type + * + * @return boolean + */ + public function has($type); + + /** + * Get the object for a given type. + * + * @param string $type + * + * @return Object + */ + public function get($type); + + /** + * Get the mapper for a given type. + * + * @param string $type + * + * @return TypeMapperInterface + */ + public function getMapper($type); +} diff --git a/src/Handler/TypeMapperInterface.php b/src/Handler/TypeMapperInterface.php new file mode 100644 index 0000000..4ccc177 --- /dev/null +++ b/src/Handler/TypeMapperInterface.php @@ -0,0 +1,36 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Handler; + +/** + * @class TypeMapperInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface TypeMapperInterface +{ + /** + * getType + * + * @return string + */ + public function getType(); + + /** + * getObject + * + * @return Object + */ + public function getObject(); +} diff --git a/src/Loader/LoaderInterface.php b/src/Loader/LoaderInterface.php new file mode 100644 index 0000000..33c0866 --- /dev/null +++ b/src/Loader/LoaderInterface.php @@ -0,0 +1,24 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Loader; + +/** + * @interface LoaderInterface + * + * @package Lucid\Mux\Loader + * @version $Id$ + * @author iwyg + */ +interface LoaderInterface +{ + public function loadRoutes($routes); +} diff --git a/src/Loader/PhpLoader.php b/src/Loader/PhpLoader.php new file mode 100644 index 0000000..482b544 --- /dev/null +++ b/src/Loader/PhpLoader.php @@ -0,0 +1,175 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Loader; + +use Lucid\Mux\Routes; +use Lucid\Resource\LocatorInterface; +use Lucid\Mux\RouteCollectionBuilder; +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Loader\LoaderInterface; +use Lucid\Resource\Loader\AbstractFileLoader; +use Lucid\Resource\Exception\LoaderException; + +/** + * @class PhpLoader + * + * @package Lucid\Mux\Cache\Loader + * @version $Id$ + * @author iwyg + */ +class PhpLoader extends AbstractFileLoader implements LoaderInterface +{ + /** + * Constructor. + * + * @param LocatorInterface $locator + * @param RouteCollectionInterface $routes + */ + public function __construct(LocatorInterface $locator, RouteCollectionInterface $routes = null) + { + parent::__construct($locator); + $this->builder = new RouteCollectionBuilder($routes ?: new Routes); + } + + /** + * {@inheritdoc} + */ + public function loadRoutes($routes) + { + $this->load($routes); + + return $this->builder->getCollection(); + } + + /** + * {@inheritdoc} + */ + protected function getExtensions() + { + return ['php']; + } + + /** + * {@inheritdoc} + */ + protected function doLoad($file) + { + if (!is_array($routes = include $file)) { + throw new LoaderException('Return value must be array.'); + } + + $this->addRoutes($routes); + + return $this->builder->getCollection(); + } + + /** + * loadRoutes + * + * @param array $routes + * + * @return void + */ + private function addRoutes(array $routes) + { + foreach ($routes as $name => $route) { + if ((bool)$gkeys = $this->getGroupKeys($route)) { + $req = isset($route['requirements']) ? $route['requirements'] : []; + if (null === $prefix = isset($route['pattern']) ? $route['pattern'] : $name) { + continue; + } + foreach ($gkeys as $i) { + $this->loadGroup($prefix, $req, $route[$i]); + } + continue; + } + + $this->addRoute($name, $route); + } + } + + /** + * loadGroup + * + * @param string $prefix + * @param array $requirements + * @param array $routes + * + * @return void + */ + private function loadGroup($prefix, array $requirements, array $routes) + { + $this->builder->group($prefix, $requirements); + $this->addRoutes($routes); + $this->builder->endGroup(); + } + /** + * isGroup + * + * @param mixed $index + * @param array $group + * + * @return void + */ + private function getGroupKeys(array $group) + { + return array_filter(array_keys($group), function ($i) { + return is_int($i); + }); + } + + /** + * addRoute + * + * @param string $name + * @param array $route + * + * @return void + */ + private function addRoute($name, array $route) + { + extract($this->defaults($route)); + + $requirements[RouteCollectionBuilder::K_NAME] = $name; + + $this->builder->addRoute($method, $pattern, $handler, $requirements, $defaults); + } + + /** + * loadImports + * + * @param array $routes + * + * @return void + */ + private function loadImports(array $routes) + { + } + + /** + * defaults + * + * @param array $route + * + * @return array + */ + private function defaults(array $route) + { + return array_merge([ + 'method' => 'GET', + 'pattern' => null, + 'handler' => null, + 'defaults' => [], + 'requirements' => [], + ], $route); + } +} diff --git a/src/Matcher/Context.php b/src/Matcher/Context.php new file mode 100644 index 0000000..f860fe9 --- /dev/null +++ b/src/Matcher/Context.php @@ -0,0 +1,129 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Matcher; + +use Lucid\Mux\Request\ContextInterface as RequestContext; + +/** + * @class Context + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Context implements ContextInterface +{ + /** @var int */ + private $type; + + /** @var string */ + private $name; + + /** @var RequestContext */ + private $request; + + /** @var mixed */ + private $handler; + + /** @var array */ + private $vars; + + /** + * Constructor. + * + * @param int $type + * @param string $name + * @param string $url + * @param mixed $handler + * @param array $vars + */ + public function __construct($type, $name, RequestContext $request, $handler, array $vars = []) + { + $this->type = $type; + $this->name = $name; + $this->request = $request; + $this->handler = $handler; + $this->vars = $vars; + } + + /** + * {@inheritdoc} + */ + public function isMatch() + { + return RequestMatcherInterface::MATCH === $this->type; + } + + /** + * {@inheritdoc} + */ + public function isHostMissmatch() + { + return RequestMatcherInterface::NOMATCH_HOST === $this->type; + } + + /** + * {@inheritdoc} + */ + public function isMethodMissmatch() + { + return RequestMatcherInterface::NOMATCH_METHOD === $this->type; + } + + /** + * {@inheritdoc} + */ + public function isSchemeMissMatch() + { + return RequestMatcherInterface::NOMATCH_SCHEME === $this->type; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->name; + } + + /** + * {@inheritdoc} + */ + public function getRequest() + { + return $this->request; + } + + /** + * {@inheritdoc} + */ + public function getPath() + { + return $this->request->getPath(); + } + + /** + * {@inheritdoc} + */ + public function getHandler() + { + return $this->handler; + } + + /** + * {@inheritdoc} + */ + public function getVars() + { + return $this->vars; + } +} diff --git a/src/Matcher/ContextInterface.php b/src/Matcher/ContextInterface.php new file mode 100644 index 0000000..a1dbb23 --- /dev/null +++ b/src/Matcher/ContextInterface.php @@ -0,0 +1,81 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Matcher; + +use Lucid\Mux\Request\ContextInterface as RequestContext; + +/** + * @interface ContextInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface ContextInterface +{ + /** + * isMatch + * + * @return bool + */ + public function isMatch(); + + /** + * @return bool + */ + public function isHostMissmatch(); + + /** + * @return bool + */ + public function isMethodMissmatch(); + + /** + * @return bool + */ + public function isSchemeMissMatch(); + + /** + * getName + * + * @return string + */ + public function getName(); + + /** + * Get the request Context. + * + * @return string + */ + public function getRequest(); + + /** + * getPath + * + * @return string + */ + public function getPath(); + + /** + * getHandler + * + * @return mixed|string + */ + public function getHandler(); + + /** + * getParameters + * + * @return array + */ + public function getVars(); +} diff --git a/src/Matcher/MatcherTrait.php b/src/Matcher/MatcherTrait.php new file mode 100644 index 0000000..1ceac45 --- /dev/null +++ b/src/Matcher/MatcherTrait.php @@ -0,0 +1,123 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Matcher; + +use Lucid\Mux\RouteInterface; +use Lucid\Mux\RouteContextInterface as RouteContext; +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Request\ContextInterface as Request; + +/** + * @trait MatcherTrait + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +trait MatcherTrait +{ + /** + * reduce + * + * @param RouteCollectionInterface $routes + * @param RequestContext $context + * + * @return RouteCollectionInterface + */ + private function filterByMethodAndScheme(RouteCollectionInterface $routes, Request $context) + { + return $routes->findByMethod($context->getMethod())->findByScheme($context->getScheme()); + } + + /** + * getMatchFailureReason + * + * @param array $nomatch + * @param Request $request + * + * @return int + */ + private function getMatchFailureReason(array $nomatch, Request $request) + { + $path = $request->getPath(); + + $reduce = array_filter($nomatch, function ($route) use ($path) { + return (bool)preg_match_all($route->getContext()->getRegex(), $path); + }); + + foreach ($reduce as $name => $route) { + if (!$this->matchHost($route->getContext(), $request, $route->getHost())) { + return RequestMatcherInterface::NOMATCH_HOST; + } + + if (!$route->hasScheme($request->getScheme())) { + return RequestMatcherInterface::NOMATCH_SCHEME; + } + + if (!$route->hasMethod($request->getMethod())) { + return RequestMatcherInterface::NOMATCH_METHOD; + } + } + + return RequestMatcherInterface::NOMATCH; + } + + private function matchHost(RouteContext $ctx, Request $request, $host = null) + { + if (null === $host) { + return true; + } + + return (bool)preg_match_all($ctx->getHostRegex(), $request->getHost()); + } + + /** + * getMatchedParams + * + * @param RouteInterface $route + * @param array $matches + * + * @return array + */ + private function getMatchedVars(RouteInterface $route, array $matches) + { + $vars = $route->getContext()->getVars(); + $params = array_merge( + $route->getDefaults(), + array_map( + [$this, 'getValue'], + array_intersect_key( + $matches, + $t = array_combine($vars, array_pad([], count($vars), null)) + ) + ) + ); + + return array_intersect_key($params, $t); + } + + /** + * getValue + * + * @param string $val + * + * @return mixed string|int|float + */ + private function getValue(array $val) + { + if (is_numeric($val[0])) { + return 0 + $val[0]; + } + + return $val[0]; + } +} diff --git a/src/Matcher/RequestMatcher.php b/src/Matcher/RequestMatcher.php new file mode 100644 index 0000000..5906256 --- /dev/null +++ b/src/Matcher/RequestMatcher.php @@ -0,0 +1,76 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Matcher; + +use Lucid\Mux\RouteInterface; +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Request\ContextInterface as Request; +use Lucid\Mux\Cache\CachedCollectionInterface; +use Lucid\Mux\Matcher\Context as MatchContext; + +/** + * @class RequestMatcher + * + * @package Lucid\Routing\Matcher + * @version $Id$ + * @author iwyg + */ +class RequestMatcher implements RequestMatcherInterface +{ + use MatcherTrait; + + /** + * matchRequest + * + * @param Request $request + * + * @return MatchContext + */ + public function matchRequest(Request $request, RouteCollectionInterface $routes) + { + $path = $request->getPath(); + $filtered = $this->filterByMethodAndScheme($routes, $request); + + if ($filtered instanceof CachedCollectionInterface && 0 !== count($r = $route->findByStaticPath($path))) { + $filtered = $r; + } + + $nomatch = array_diff_key($routes->all(), $filtered->all()); + + foreach ($filtered->all() as $name => $route) { + $rctx = $route->getContext(); + + // does it match host? + if (!$this->matchHost($rctx, $request, $route->getHost())) { + $nomatch[$name] = $route; + continue; + } + + // does it match static path? + if (0 !== strpos($path, $rctx->getStaticPath())) { + continue; + } + + // does it match pattern described on the route? + if ((bool)preg_match_all($rctx->getRegex(), $path, $matches)) { + $vars = $this->getMatchedVars($route, $matches); + $handler = $route->getHandler(); + + return new MatchContext(self::MATCH, $name, $request, $handler, $vars); + } + + $nomatch[$name] = $route; + } + + return new MatchContext($this->getMatchFailureReason($nomatch, $request), null, $request, null); + } +} diff --git a/src/Matcher/RequestMatcherInterface.php b/src/Matcher/RequestMatcherInterface.php new file mode 100644 index 0000000..4d312ea --- /dev/null +++ b/src/Matcher/RequestMatcherInterface.php @@ -0,0 +1,49 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Matcher; + +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Request\ContextInterface as Request; + +/** + * RequestMatcherInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface RequestMatcherInterface +{ + /** @var int */ + const MATCH = 1; + + /** @var int */ + const NOMATCH = -1; + + /** @var int */ + const NOMATCH_METHOD = -2; + + /** @var int */ + const NOMATCH_HOST = -3; + + /** @var int */ + const NOMATCH_SCHEME = -4; + + /** + * matchRequest + * + * @param RequestContextInterface $context + * + * @return ContextInterface + */ + public function matchRequest(Request $context, RouteCollectionInterface $routes); +} diff --git a/lucid/cache/src/Driver/SqLiteDriver.php b/src/Parser/Delimiter.php similarity index 57% rename from lucid/cache/src/Driver/SqLiteDriver.php rename to src/Parser/Delimiter.php index ded4f70..955f93c 100644 --- a/lucid/cache/src/Driver/SqLiteDriver.php +++ b/src/Parser/Delimiter.php @@ -1,7 +1,7 @@ * @@ -9,15 +9,17 @@ * that was distributed with this package. */ -namespace Lucid\Cache\Driver; +namespace Lucid\Mux\Parser; + +use Lucid\Mux\Parser\TokenInterface as TI; /** - * @class SqLiteDriver + * @class Delimiter * - * @package Lucid\Cache\Driver + * @package Lucid\Mux * @version $Id$ * @author iwyg */ -class SqLiteDriver extends AbstractSqlDriver +class Delimiter extends Token { } diff --git a/src/Parser/ParserInterface.php b/src/Parser/ParserInterface.php new file mode 100644 index 0000000..cf503b4 --- /dev/null +++ b/src/Parser/ParserInterface.php @@ -0,0 +1,40 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Parser; + +use Lucid\Mux\RouteInterface; +use Lucid\Mux\RouteContextInterface as ContextInterface; + +/** + * @interface ParserInterface + * + * @package Lucid\Mux\Parser + * @version $Id$ + * @author iwyg + */ +interface ParserInterface +{ + /** @var string */ + const SEPARATORS = '/.;:-_~+*=|'; + + /** @var string */ + const EXP_DELIM = '#'; + + /** + * parse + * + * @param RouteInterface $route + * + * @return ContextInterface + */ + public static function parse(RouteInterface $route); +} diff --git a/src/Parser/Standard.php b/src/Parser/Standard.php new file mode 100644 index 0000000..5fe7c57 --- /dev/null +++ b/src/Parser/Standard.php @@ -0,0 +1,296 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Parser; + +use Lucid\Mux\RouteContext; +use Lucid\Mux\RouteInterface; +use Lucid\Mux\Parser\ParserInterface as Ps; +use Lucid\Mux\RouteContextInterface as ContextInterface; + +/** + * @class Standard + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +abstract class Standard +{ + /** @var string */ + const VAR_REGEXP = <<<'REGEX' +(?: + (?P%1$s)? + %2$s + (?P\w+) + \??%3$s + (?P%1$s)? +) +REGEX; + + /** @var string */ + const L_DELIM = '{'; + + /** @var string */ + const R_DELIM = '}'; + + /** + * Parses a route object. + * + * @param RouteInterface $route + * + * @return RouteContextInterface + */ + public static function parse(RouteInterface $route) + { + extract(self::transpilePattern($route->getPattern(), false, $route->getConstraints(), $route->getDefaults())); + $host = self::parseHostVars($route); + + return new RouteContext($staticPath, $expression, $tokens, $host['expression'], $host['tokens']); + } + + /** + * Transpiles the the given pattern into a useful format. + * + * @param RouteInterface $route + * @param string $pattern + * @param bool $isHost + * + * @return array + */ + public static function transpilePattern($pattern, $host = false, array $requirements = [], array $defaults = []) + { + $tokens = self::tokenizePattern($pattern, $host, $requirements, $defaults); + + $staticPath = !$tokens[0] instanceof Variable ? $tokens[0]->value : '/'; + + $regex = self::transpileMatchRegex($tokens); + + return self::getCompact($staticPath, $regex, $tokens); + } + + /** + * tokenizePattern + * + * @param mixed $pattern + * @param mixed $isHost + * @param array $requirements + * @param array $defaults + * + * @return array + */ + public static function tokenizePattern($pattern, $isHost = false, array $requirements = [], array $defaults = []) + { + // left pad pattern with separator + if (!$isHost && false === self::isSeparator(substr($pattern, 0, 1))) { + $pattern = '/'.$pattern; + } + + list ($path, ) = $splt = array_pad(preg_split('#\{\w+\??\}#', $pattern), 1, '/'); + + if (2 > count($splt)) { + return [new Text($path)]; + } + + $separator = $isHost ? '.' : '/'; + $matchDel = join('|', array_map('preg_quote', str_split(Ps::SEPARATORS), str_split( + str_repeat(Ps::EXP_DELIM, strlen(Ps::SEPARATORS)) + ))); + + $expr = Ps::EXP_DELIM.sprintf( + self::VAR_REGEXP, + $matchDel, + preg_quote(self::L_DELIM, Ps::EXP_DELIM), + preg_quote(self::R_DELIM, Ps::EXP_DELIM) + ).Ps::EXP_DELIM.'x'; + + preg_match_all($expr, $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); + + $pos = 0; + $plen = strlen($pattern); + $nlen = count($matches) - 1; + $tokens = []; + + foreach ($matches as $i => $match) { + // right hand delimiter, not always present + $rdel = (!isset($match['rdel']) || -1 === $match['rdel'][1]) ? null : $match['rdel']; + $ldel = -1 === $match['ldel'][1] ? null : $match['ldel']; + $var = -1 === $match['var'][1] ? null : $match['var']; + $opt = 0 === strcmp('?', substr($match[0][0], -2, 1)); + + $default = isset($defaults[$var[0]]) ? $defaults[$var[0]] : null; + $constraint = isset($requirements[$var[0]]) ? $requirements[$var[0]] : null; + + $offset = $match[0][1]; + + // add preceeding text + if (0 !== strlen($str = substr($pattern, $pos, $offset - $pos))) { + $text = new Text($str, $lt = self::lastToken($tokens)); + self::pushTokens($text, $lt, $tokens); + } + + // add left hand delimiter + if (null !== $ldel) { + $delim = new Delimiter($ldel[0], $lt = self::lastToken($tokens)); + self::pushTokens($delim, $lt, $tokens); + } + + // add variable + $tVar = new Variable($var[0], !$opt, $constraint, $lt = self::lastToken($tokens), null, $separator); + self::pushTokens($tVar, $lt, $tokens); + + // add right hand delimiter + if (null !== $rdel) { + $delim = new Delimiter($rdel[0], $lt = self::lastToken($tokens)); + self::pushTokens($delim, $lt, $tokens); + } + + // update current position + $pos = $offset + strlen($match[0][0]); + + // add trailing text. + if ($nlen === $i && $plen !== $pos) { + $tail = substr($pattern, -($plen - $pos)); + + if (0 !== strlen($tail)) { + $t = new Text($tail, $lt = self::lastToken($tokens)); + self::pushTokens($t, $lt, $tokens); + } + } + } + + return $tokens; + } + + /** + * Checks if given string is a url delimiter. + * + * @param string $test + * + * @return bool + */ + public static function isSeparator($test) + { + return 1 === strlen($test) && false !== strpos(Ps::SEPARATORS, $test); + } + + /** + * Transpiles tokens to a regex. + * + * @param array $tokens + * + * @return string + */ + public static function transpileMatchRegex(array $tokens) + { + $regex = []; + + foreach ($tokens as $token) { + $var = $token instanceof Variable ? $token : ($token instanceof Delimiter ? $token->next : null); + + if (null !== $var && $var instanceof Variable && null !== ($optgrp = self::makeOptGrp($var))) { + $regex[] = $optgrp; + break; + } + + $regex[] = $token; + } + + return implode('', $regex); + } + + /** + * Recursively iterates over tailing optional variables. + * + * @param Variable $var + * + * @return string will reuturn `null` if no matches are found. + */ + private static function makeOptGrp(Variable $var) + { + if ($var->required || (null !== $var->next && !$var->next instanceof Variable)) { + return; + } + + $optgrp = null !== $var->next ? self::makeOptGrp($var->next) : ''; + + $p = $var->prev instanceof Delimiter ? $var->prev : ''; + + return sprintf('(?:%s%s%s)?', $p, $var, $optgrp); + } + + /** + * pushTokens + * + * @param TokenInterface $token + * @param TokenInterface $prev + * @param array $tokens + * + * @return void + */ + private static function pushTokens(TokenInterface $token, TokenInterface $prev = null, array &$tokens = []) + { + if (null !== $prev) { + $prev->next = $token; + } + + $token->prev = $prev; + + $tokens[] = $token; + } + + /** + * lastToken + * + * @param array $tokens + * + * @return TokenInterface + */ + private static function lastToken(array $tokens) + { + if ($token = end($tokens)) { + return $token; + } + + return null; + } + + /** + * parseHostVars + * + * @param RouteInterface $route + * + * @return array + */ + private static function parseHostVars(RouteInterface $route) + { + if (null === $host = $route->getHost()) { + return ['expression' => null, 'tokens' => []]; + } + + return self::transpilePattern($host, true, $route->getConstraints(), $route->getDefaults()); + } + + /** + * getCompact + * + * @param string $staticPath + * @param string $expression + * @param array $vars + * @param array $tokens + * + * @return array + */ + private static function getCompact($staticPath, $expression, array $tokens = []) + { + return compact('staticPath', 'expression', 'tokens'); + } +} diff --git a/lucid/cache/src/Client/SqLite.php b/src/Parser/Text.php similarity index 56% rename from lucid/cache/src/Client/SqLite.php rename to src/Parser/Text.php index cf1518e..fb9849d 100644 --- a/lucid/cache/src/Client/SqLite.php +++ b/src/Parser/Text.php @@ -1,7 +1,7 @@ * @@ -9,15 +9,17 @@ * that was distributed with this package. */ -namespace Lucid\Cache\Client; +namespace Lucid\Mux\Parser; + +use Lucid\Mux\Parser\TokenInterface as TI; /** - * @class SqLiteClient + * @class Text * - * @package Lucid\Cache + * @package Lucid\Mux\Parser * @version $Id$ * @author iwyg */ -class SqLite extends AbstractSqlClient +class Text extends Token { } diff --git a/src/Parser/Token.php b/src/Parser/Token.php new file mode 100644 index 0000000..2c70fef --- /dev/null +++ b/src/Parser/Token.php @@ -0,0 +1,53 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Parser; + +/** + * @class Token + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +abstract class Token implements TokenInterface +{ + /** @var string */ + public $value; + + /** @var TI */ + public $prev; + + /** @var TI */ + public $next; + + /** + * __construct + * + * @param mixed $value + * @param TI $prev + * @param TI $next + */ + public function __construct($value, TokenInterface $prev = null, TokenInterface $next = null) + { + $this->value = $value; + $this->prev = $prev; + $this->next = $next; + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return preg_quote($this->value, ParserInterface::EXP_DELIM); + } +} diff --git a/lucid/cache/src/Driver/MySqlDriver.php b/src/Parser/TokenInterface.php similarity index 57% rename from lucid/cache/src/Driver/MySqlDriver.php rename to src/Parser/TokenInterface.php index 22bb7e7..3c4c371 100644 --- a/lucid/cache/src/Driver/MySqlDriver.php +++ b/src/Parser/TokenInterface.php @@ -1,7 +1,7 @@ * @@ -9,15 +9,16 @@ * that was distributed with this package. */ -namespace Lucid\Cache\Driver; +namespace Lucid\Mux\Parser; /** - * @class MySqlDriver + * @interface TokenInterface * - * @package Lucid\Cache\Driver + * @package Lucid\Routing * @version $Id$ * @author iwyg */ -class MySqlDriver extends AbstractSqlDriver +interface TokenInterface { + public function __toString(); } diff --git a/src/Parser/Variable.php b/src/Parser/Variable.php new file mode 100644 index 0000000..cc2eb95 --- /dev/null +++ b/src/Parser/Variable.php @@ -0,0 +1,77 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Parser; + +use Lucid\Mux\Parser\TokenInterface as TI; + +/** + * @class Variable + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Variable extends Token +{ + /** @var bool */ + public $required; + + /** @var string */ + public $regex; + + /** @var string */ + private $constraint; + + /** @var string */ + private $default; + + /** + * Constructor. + * + * @param string $name the variable name + * @param bool $required the variable is requiered + * @param string $constraint a regex constraint. + * @param TokenInterface $prev the previous token + * @param TokenInterface $next the next token + */ + public function __construct($name, $required = true, $constr = null, TI $prev = null, TI $next = null, $def = '/') + { + $this->required = $required; + $this->constraint = $constr; + $this->default = $def; + + parent::__construct($name, $prev, $next); + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return $this->getRegex(); + } + + /** + * getRegex + * + * @return string + */ + public function getRegex() + { + if (null === $this->constraint) { + $delim = $this->next instanceof Delimiter ? (string)$this->next : ''; + $this->constraint = sprintf('[^%s%s]++', preg_quote($this->default, ParserInterface::EXP_DELIM), $delim); + } + + return $this->regex = sprintf('(?P<%s>%s)', $this->value, $this->constraint); + } +} diff --git a/src/Request/Context.php b/src/Request/Context.php new file mode 100644 index 0000000..66762cf --- /dev/null +++ b/src/Request/Context.php @@ -0,0 +1,141 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +use Psr\Http\Message\ServerRequestInterface; + +/** + * @class Context + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Context implements ContextInterface +{ + /** @var string */ + private $path; + + /** @var string */ + private $method; + + /** @var string */ + private $host; + + /** @var string */ + private $scheme; + + /** @var string */ + private $query; + + /** @var int */ + private $port; + + /** + * Constructor. + * + * @param string $base + * @param string $path + * @param string $method + * @param string $query + * @param string $host + * @param string $scheme + * @param int $port + */ + public function __construct( + $path = '/', + $method = 'GET', + $query = '', + $host = 'localhost', + $scheme = 'http', + $port = 80 + ) { + $this->path = $path; + $this->method = $method; + $this->query = ltrim($query ?: '', '?&'); + $this->host = trim($host, '/'); + $this->scheme = $scheme; + $this->port = (int)$port; + } + + /** + * {@inheritdoc} + */ + public function getPath() + { + return $this->path; + } + + /** + * {@inheritdoc} + */ + public function getMethod() + { + return $this->method; + } + + /** + * {@inheritdoc} + */ + public function getQueryString() + { + return $this->query; + } + + /** + * {@inheritdoc} + */ + public function getHost() + { + return $this->host; + } + + /** + * {@inheritdoc} + */ + public function getScheme() + { + return $this->scheme; + } + + /** + * getHttpPort + * + * @return int + */ + public function getHttpPort() + { + return $this->port; + } + + /** + * fromPsrRequest + * + * @param ServerRequestInterface $request + * + * @return ServerRequestInterface + */ + public static function fromPsrRequest(ServerRequestInterface $request) + { + $uri = $request->getUri(); + $server = $request->getServerParams(); + + return new self( + $uri->getPath(), + $request->getMethod(), + $uri->getQuery(), + $uri->getHost(), + $uri->getScheme(), + $uri->getPort() ?: (isset($server['SERVER_PORT']) ? $server['SERVER_PORT'] : 80) + ); + } +} diff --git a/src/Request/ContextInterface.php b/src/Request/ContextInterface.php new file mode 100644 index 0000000..adba5c3 --- /dev/null +++ b/src/Request/ContextInterface.php @@ -0,0 +1,58 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +/** + * @interface ContextInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface ContextInterface +{ + + /** + * Get the request URI. + * + * @return string + */ + public function getPath(); + + /** + * Get the request method. + * + * @return string + */ + public function getMethod(); + + /** + * Get the requests query string if any. + * + * @return string + */ + public function getQueryString(); + + /** + * Get the requests host name. + * + * @return string + */ + public function getHost(); + + /** + * Get the request scheme. + * + * @return string + */ + public function getScheme(); +} diff --git a/src/Request/PassResponseMapper.php b/src/Request/PassResponseMapper.php new file mode 100644 index 0000000..48ec174 --- /dev/null +++ b/src/Request/PassResponseMapper.php @@ -0,0 +1,34 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +/** + * @class PassResponseMapper + * + * @package Lucid\Routing\Http + * @version $Id$ + * @author iwyg + */ +class PassResponseMapper implements ResponseMapperInterface +{ + /** + * {@inheritdoc} + * + * Will just pass the input data. + * + * @return mixed the input value. + */ + public function mapResponse($response) + { + return $response; + } +} diff --git a/src/Request/ResponseMapperInterface.php b/src/Request/ResponseMapperInterface.php new file mode 100644 index 0000000..3123430 --- /dev/null +++ b/src/Request/ResponseMapperInterface.php @@ -0,0 +1,32 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +/** + * @class ResponseMapperInterface + * + * @package Lucid\Routing\Http + * @version $Id$ + * @author iwyg + */ +interface ResponseMapperInterface +{ + /** + * Maps a given input to a response. + * + * @param mixed $response the input response data + * + * @return mixed depending on the implementation, typically a response + * object. + */ + public function mapResponse($response); +} diff --git a/src/Request/UrlGenerator.php b/src/Request/UrlGenerator.php new file mode 100644 index 0000000..5faf259 --- /dev/null +++ b/src/Request/UrlGenerator.php @@ -0,0 +1,332 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +use Lucid\Mux\RouteInterface; +use Lucid\Mux\RouterInterface; +use Lucid\Mux\RouteContextInterface; +use Lucid\Mux\Request\ContextInterface as RequestContextInterface; +use Lucid\Mux\Request\Context as RequestContext; +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Parser\Text; +use Lucid\Mux\Parser\Variable; + +/** + * @class UrlGenerator + * + * @package Lucid\Routing + * @version $Id$ + * @author iwyg + */ +class UrlGenerator implements UrlGeneratorInterface +{ + /** + * noEncode + * + * @var array + */ + private static $noEncode = [ + '%2F' => '/', + '%40' => '@', + '%3A' => ':', + '%3B' => ';', + '%2C' => ',', + '%3D' => '=', + '%2B' => '+', + '%21' => '!', + '%2A' => '*', + '%7C' => '|', + ]; + + /** + * routes + * + * @var RouteCollectionInterface + */ + private $routes; + + /** + * request + * + * @var RequestContextInterface + */ + private $request; + + /** + * Constructor. + * + * @param RouteCollectionInterface $routes + * @param RequestContextInterface $request + * + * @return void + */ + public function __construct(RouteCollectionInterface $routes = null, RequestContextInterface $request = null) + { + $this->routes = $routes; + $this->setRequestContext($request ?: new RequestContext); + } + + /** + * setRequestContext + * + * @param RequestContextInterface $request + * + * @return void + */ + public function setRequestContext(RequestContextInterface $request) + { + $this->request = $request; + } + + /** + * {@inheritdoc} + */ + public function getRequestContext() + { + return $this->request; + } + + /** + * setRoutes + * + * @param RouteCollectionInterface $request + * + * @return void + */ + public function setRoutes(RouteCollectionInterface $routes) + { + $this->routes = $routes; + } + + /** + * {@inheritdoc} + */ + public function currentUrl($type = self::RELATIVE_PATH) + { + if (null !== ($path = $this->currentPath($type))) { + return $path . $this->getQueryString($this->getRequest()); + } + + return $path; + } + + /** + * {@inheritdoc} + */ + public function currentPath($type = self::RELATIVE_PATH) + { + $rel = $this->getReqPath($this->getRequest()); + + if ($type === self::RELATIVE_PATH) { + return $rel; + } elseif ($type === self::ABSOLUTE_PATH) { + return $this->getSchemeAndHost($this->getRequest()).'/'.trim($rel, '/'); + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function generate($name, array $parameters = [], $host = null, $type = self::RELATIVE_PATH) + { + if (null === $this->routes || !$route = $this->routes->get($name)) { + throw new \InvalidArgumentException(sprintf('A route with name "%s" could not be found.', $name)); + } + + return $this->compilePath($route, $parameters, $host ?: $route->getHost(), $type, $name); + } + + /** + * Get the current request context. + * + * Gets the current set `RequestContext` object or creates a new + * instance of `RequestContext` + * + * @return RequestContextInterface + */ + private function getRequest() + { + return null === $this->request ? $this->request = new RequestContext : $this->request; + } + + /** + * Compiles a Route instance into a readable path or url. + * + * @param RouteInterface $route the route + * @param array $parameters route parameters + * @param string $host the host name. + * @param int $type the path type + * + * @throws \InvalidArgumentException if `$route` requires a `$host` and + * none is given. + * @throws \InvalidArgumentException if a required parameter by `$route` is + * amiss. + * + * @return string + */ + private function compilePath(RouteInterface $route, array $parameters, $host, $type, $name) + { + if (static::RELATIVE_PATH === $type && null !== $route->getHost()) { + throw new \InvalidArgumentException( + sprintf('Can\'t create relative path because route "%s" requires a deticated hostname.', $name) + ); + } + + $context = $route->getContext(); + $prefix = static::ABSOLUTE_PATH === $type ? $this->getPathPrefix($route, $host) : ''; + + if (0 === count($vars = $context->getVars())) { + return $this->getPrefixed($context->getStaticPath(), $prefix); + } + + $parts = []; + $vars = $this->getRouteVars($context, $parameters); + + foreach ($context->getTokens() as $token) { + if ($token instanceof Variable) { + if ($token->required && !isset($vars[$token->value])) { + throw new \InvalidArgumentException(sprintf('{%s} is a required parameter.', $token->getValue())); + } + $parts[] = $vars[$token->value]; + } else { + $parts[] = $token->value; + } + } + + return $this->getPrefixed(implode('', $parts), $prefix); + } + + /** + * getRouteVars + * + * @param RouteContextInterface $context + * @param array $parameters + * + * @return array + */ + private function getRouteVars(RouteContextInterface $context, array $parameters) + { + return array_merge( + array_combine(array_values($v = $context->getVars()), array_fill(1, count($v), null)), + $parameters + ); + } + + /** + * getPathPrefix + * + * @param RouteInterface $route + * @param array $parameters + * @param mixed $host + * + * @return mixed + */ + private function getPathPrefix(RouteInterface $route, $host = null) + { + $context = $route->getContext(); + + if (null === $host) { + $host = $route->getHost() ? $route->getHost() : $this->request->getHost(); + } + + if (null !== $route->getHost() && !(bool)preg_match($context->getHostRegex(), $host)) { + throw new \InvalidArgumentException('Host requirement does not match given host.'); + } + + return sprintf('%s://%s', $this->getRouteProtocol($route, $this->getRequest()), $host); + } + + /** + * getRouteProtocol + * + * @param RouteInterface $route + * @param RequestContextInterface $request + * + * @return string + */ + private function getRouteProtocol(RouteInterface $route, RequestContextInterface $request) + { + $requestScheme = $request->getScheme(); + + $schemes = $route->getSchemes(); + + if (in_array($requestScheme, $schemes)) { + return $requestScheme; + } + + return current($schemes) ?: 'http'; + } + + /** + * getSchemeAndHost + * + * @return string + */ + private function getSchemeAndHost($req) + { + $port = $req->getHttpPort(); + $host = in_array($port, [80, 443]) ? $req->getHost() : $req->getHost() . ':' . $port; + + return $req->getScheme() . '://' . $host; + } + + /** + * getReqPath + * + * @param RequestContextInterface $req + * + * @return string + */ + private function getReqPath(RequestContextInterface $req) + { + return $req->getPath(); + } + + /** + * getPathAndQuery + * + * @param RequestContextInterface $req + * + * @return string + */ + private function getPathAndQuery(RequestContextInterface $req) + { + return $req->getReqPath($req).$this->getQueryString($req); + } + + /** + * getQueryString + * + * @param RequestContextInterface $req + * + * @return string + */ + private function getQueryString(RequestContextInterface $req) + { + return $req->getQueryString() ? '?'.$req->getQueryString() : ''; + } + + /** + * getPrefied + * + * @param string $path + * @param string $prefix + * + * @return string + */ + private function getPrefixed($path, $prefix) + { + return $prefix.'/'.trim(strtr(rawurlencode($path), static::$noEncode), '/'); + } +} diff --git a/src/Request/UrlGeneratorInterface.php b/src/Request/UrlGeneratorInterface.php new file mode 100644 index 0000000..885e6eb --- /dev/null +++ b/src/Request/UrlGeneratorInterface.php @@ -0,0 +1,86 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Request; + +use Lucid\Mux\RouteCollectionInterface; +use Lucid\Mux\Request\ContextInterface as Request; + +/** + * @interface UrlGeneratorInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface UrlGeneratorInterface +{ + /** @var int */ + const RELATIVE_PATH = 0; + + /** @var int */ + const ABSOLUTE_PATH = 1; + + /** + * Set the current request context. + * + * @param RequestContextInterface $request the request context. + * + * @return void + */ + public function setRequestContext(Request $request); + + /** + * Get the current request context. + * + * @return RequestContextInterface the request context, `NULL` if none. + */ + public function getRequestContext(); + + /** + * Set the routecollection needed for url generation by name. + * + * @param RouteCollectionInterface $routes the route collection. + * + * @return void + */ + public function setRoutes(RouteCollectionInterface $request); + + /** + * Gets the current url ommitting the query string. + * + * @param int $type the path format type + * + * @return string the current url, `NULL` if `$type` is invalid. + */ + public function currentPath($type = self::RELATIVE_PATH); + + /** + * Gets the current url including the query string. + * + * @param int $type the path format type + * + * @return string the current url, `NULL` if `$type` is invalid. + */ + public function currentUrl($type = self::RELATIVE_PATH); + + /** + * Generates a readable path or url from a given route name. + * + * @param string $name the route name + * @param array $parameters parameters required by the route object + * @param string $host host name required by the route object. + * @param int $type the path format type + * + * @return string + */ + public function generate($name, array $parameters = [], $host = null, $type = self::RELATIVE_PATH); +} diff --git a/src/Route.php b/src/Route.php new file mode 100644 index 0000000..9ae3cc1 --- /dev/null +++ b/src/Route.php @@ -0,0 +1,251 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use Closure; +use LogicException; + +/** + * @class + * @see RouteInterface + * @see Serializable + */ +class Route implements RouteInterface +{ + /** @var string */ + private $pattern; + + /** @var string|callable */ + private $handler; + + /** @var array */ + private $methods; + + /** @var string */ + private $host; + + /** @var array */ + private $defaults; + + /** @var array */ + private $constraints; + + /** @var array */ + private $schemes; + + /** @var RouteContextInterface */ + private $context; + + /** @var array */ + private static $keys = [ + 'pattern', 'handler', 'methods', 'host', + 'defaults', 'constraints', 'schemes', 'context', + ]; + + /** + * Constructor. + * + * @param string $pattern the route pattern + * @param mixed $handler the route handler + * @param string|array $methods the supported http methods. + * @param string $host the required host + * @param array $defaults default parameters + * @param array $constraints parameter constraints + * @param array $schemes supported url schemes + */ + public function __construct( + $pattern, + $handler, + array $methods = null, + $host = null, + array $defaults = null, + array $constraints = null, + array $schemes = null + ) { + $this->pattern = $pattern; + $this->handler = $handler; + $this->host = $host; + $this->defaults = $defaults ?: []; + $this->constraints = $constraints ?: []; + + $this->setMethods($methods ?: ['GET']); + $this->setSchemes($schemes ?: ['http', 'https']); + } + + /** + * {@inheritdoc} + */ + public function getMethods() + { + return $this->methods; + } + + /** + * {@inheritdoc} + */ + public function hasMethod($method) + { + return in_array(strtoupper($method), $this->methods); + } + + /** + * {@inheritdoc} + */ + public function getHandler() + { + return $this->handler; + } + + /** + * {@inheritdoc} + */ + public function getSchemes() + { + return $this->schemes; + } + + /** + * {@inheritdoc} + */ + public function hasScheme($scheme) + { + return in_array(strtolower($scheme), $this->schemes); + } + + /** + * {@inheritdoc} + */ + public function getPattern() + { + return $this->pattern; + } + + /** + * {@inheritdoc} + */ + public function getHost() + { + return $this->host; + } + + /** + * {@inheritdoc} + */ + public function getDefaults() + { + return $this->defaults; + } + + /** + * {@inheritdoc} + */ + public function getDefault($var) + { + return isset($this->defaults[$var]) ? $this->defaults[$var] : null; + } + + /** + * {@inheritdoc} + */ + public function getConstraints() + { + return $this->constraints; + } + + /** + * {@inheritdoc} + */ + public function getConstraint($param) + { + return isset($this->constraints[$param]) ? $this->constraints[$param] : null; + } + + /** + * {@inheritdoc} + */ + public function getContext() + { + if (null === $this->context) { + $this->context = call_user_func($this->getParserFunc(), $this); + } + + return $this->context; + } + + /** + * Serializes the route + * + * @return string the serialized data of this route. + */ + public function serialize() + { + if ($this->getHandler() instanceof Closure) { + throw new LogicException('Cannot serialize handler.'); + } + + return serialize(array_combine(static::$keys, array_map(function ($key) { + return call_user_func([$this, 'get'.ucfirst($key)]); + }, static::$keys))); + } + + /** + * Unserializes the route + * + * @param string $data + * + * @return void. + */ + public function unserialize($data) + { + $data = unserialize($data); + + foreach (static::$keys as $key) { + $this->{$key} = $data[$key]; + } + } + + /** + * Get the callable that parses the route into tokens. + * + * @return callable + */ + protected function getParserFunc() + { + return __NAMESPACE__.'\Parser\Standard::parse'; + } + + /** + * Sets supported request methods. + * + * @param mixed $methods string or array of strings containing accepted + * methods. + * + * @return void + */ + private function setMethods(array $methods) + { + $this->methods = array_keys(array_change_key_case(array_flip($methods), CASE_UPPER)); + } + + /** + * Sets the schemes. + * + * @param mixed $schemes string or array of strings containing accepted + * schemes. + * + * @return void + */ + private function setSchemes(array $schemes) + { + $this->schemes = array_keys(array_change_key_case(array_flip($schemes), CASE_LOWER)); + } +} diff --git a/src/RouteCollectionBuilder.php b/src/RouteCollectionBuilder.php new file mode 100644 index 0000000..02eb7ad --- /dev/null +++ b/src/RouteCollectionBuilder.php @@ -0,0 +1,400 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use SplStack; +use LogicException; + +/** + * @class RouteCollectionBuilder + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class RouteCollectionBuilder +{ + /** @var string */ + const K_NAME = 'route'; + + /** @var string */ + const K_HOST = 'host'; + + /** @var string */ + const K_SCHEME = 'schemes'; + + /** @var array */ + private static $keys = [self::K_NAME, self::K_HOST, self::K_SCHEME]; + + /** @var array */ + private $groups; + + /** @var RouteContextInterface */ + private $routes; + + /** + * Constructor. + */ + public function __construct(RouteCollectionInterface $routes = null) + { + $this->routes = $routes ?: $this->newRouteCollection(); + $this->groups = new SplStack; + } + + /** + * getCollection + * + * @return RouteCollectionInterface + */ + public function getCollection() + { + return $this->routes; + } + + /** + * Adds a GET route to the collection. + * + * @param string $pattern + * @param string $handler + * @param array $requirements + * @param array $defaults + * + * @return void + */ + public function get($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('GET', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a HEAD route to the collection + * + * @see RouteCollectionBuilder#get() + */ + public function head($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('HEAD', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a POST route to the collection + * + * @see RouteCollectionBuilder#get() + */ + public function post($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('POST', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a PUT route to the collection + * + * @see RouteCollectionBuilder#get() + */ + public function put($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('PUT', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a PATCH route to the collection + * + * @see RouteCollectionBuilder#get() + */ + public function patch($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('PATCH', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a DELETE route to the collection + * + * @see RouteCollectionBuilder#get() + */ + public function delete($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('DELETE', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a route to the collection that handles all available request + * methods. + * + * @see RouteCollectionBuilder#get() + */ + public function any($pattern, $handler, array $requirements = [], array $defaults = []) + { + $this->addRoute('GET|HEAD|POST|PUT|PATCH|DELETE', $pattern, $handler, $requirements, $defaults); + } + + /** + * Adds a route to the collection that handles the given request + * methods. + * + * @param string $methods methods seperated by a pipe |. + * @param string $pattern + * @param string $handler + * @param array $requirements + * @param array $defaults + * + * @return void + */ + public function addRoute($methods, $pattern, $handler, array $requirements = [], array $defaults = []) + { + list ($name, $host, $schemes, $constraints) = $this->parseRequirements( + $this->extendRequirements($requirements), + $methods + ); + + $route = new Route( + $this->prefixPattern($pattern), + $handler, + is_array($methods) ? $methods : explode('|', $methods), + $host, + $defaults, + $constraints, + is_array($schemes) ? $schemes : explode('|', $schemes) + ); + + $this->routes->add($name, $route); + } + + /** + * Starts a new entry point for grouping routes. + * + * @param string $prefix + * @param array $requirements + * + * @return void + */ + public function group($prefix, array $requirements = [], callable $groupConstructor = null) + { + $this->enterGroup($prefix, $requirements); + + if (null !== $groupConstructor) { + call_user_func($groupConstructor, $this); + $this->leaveGroup(); + } + } + + /** + * endGroup + * + * @return void + */ + public function endGroup() + { + $this->leaveGroup(); + } + + /** + * parseRequirements + * + * @param array $requirements + * @param string $methods + * + * @return array + */ + private function parseRequirements(array $rq, $methods) + { + $keys = []; + $constr = array_filter($this->mergeDefaults($rq, $methods), function ($val, $key) use (&$keys, $methods) { + if (!in_array($key, self::$keys)) { + return true; + } + $keys[$key] = $val; + + return false; + }, ARRAY_FILTER_USE_BOTH); + + extract($keys); + + return [${self::K_NAME}, ${self::K_HOST}, ${self::K_SCHEME}, $constr]; + } + + /** + * mergeDefaults + * + * @param array $given + * + * @return array + */ + private function mergeDefaults(array $given, $methods) + { + $defaults = array_merge(array_combine(self::$keys, array_pad([], count(self::$keys), null)), $given); + + if (null === $defaults[self::K_SCHEME]) { + $defaults[self::K_SCHEME] = 'http|https'; + } + + if (null === $defaults[self::K_NAME]) { + $defaults[self::K_NAME] = $this->generateName($methods); + } + + return $defaults; + } + + /** + * generateName + * + * @param string $methods + * + * @return string + */ + private function generateName($methods) + { + return uniqid('route_' . strtr($methods, ['|' => '_']) . '_'); + } + + /** + * newRouteCollection + * + * @return RouteCollectionInterface + */ + private function newRouteCollection() + { + $class = $this->getCollectionClass(); + + if (!is_subclass_of($class, $i = __NAMESPACE__.'\RouteCollectionInterface')) { + throw new LogicException("Routecollection class must implement $i."); + } + + return new $class; + } + + /** + * getCollectionClass + * + * @return void + */ + private function getCollectionClass() + { + return Routes::class; + } + + /** + * prefixPattern + * + * @param mixed $pattern + * + * @return string + */ + private function prefixPattern($pattern) + { + $d = '/'; + + if (!$this->hasGroups()) { + return $d.trim($pattern, $d); + } + + $prefix = $this->getCurrentGroup()->getPrefix(); + + return rtrim(($d === $prefix ? $prefix : (rtrim($prefix, $d).$d)) . trim($pattern, $d), $d); + } + + /** + * extendRequirements + * + * @param mixed $requirements + * + * @return void + */ + private function extendRequirements(array $requirements) + { + if (!$this->hasGroups()) { + return $requirements; + } + + return array_merge($this->getCurrentGroup()->getRequirements(), $requirements); + } + + /** + * enterGroup + * + * @return RouteBuilder + */ + private function enterGroup($prefix, array $requirements) + { + $group = new RouteGroup($prefix, $requirements, $this->getParentGroup()); + $this->pushGroup($group); + + return $this; + } + + /** + * getParentGroup + * + * @return null|GroupDefinition + */ + private function getParentGroup() + { + if ($this->hasGroups()) { + return $this->groups->top(); + } + } + + /** + * leaveGroup + * + * @return RouteBuilder + */ + private function leaveGroup() + { + if ($this->hasGroups()) { + $this->popGroup(); + } + + return $this; + } + + /** + * pushGroup + * + * @param array $group + * + * @return void + */ + private function pushGroup(RouteGroup $group) + { + $this->groups->push($group); + } + + /** + * popGroup + * + * @return RouteGroup + */ + private function popGroup() + { + return $this->groups->pop(); + } + + /** + * getCurrentGroup + * + * @return RouteGroup + */ + private function getCurrentGroup() + { + return $this->groups->top(); + } + + /** + * hasGroups + * + * @return bool + */ + private function hasGroups() + { + return $this->groups->count() > 0; + } +} diff --git a/src/RouteCollectionInterface.php b/src/RouteCollectionInterface.php new file mode 100644 index 0000000..717149c --- /dev/null +++ b/src/RouteCollectionInterface.php @@ -0,0 +1,84 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +/** + * @interface RouteCollectionInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface RouteCollectionInterface +{ + /** + * add + * + * @param string $routeName + * @param RouteInterface $route + * + * @return void + */ + public function add($routeName, RouteInterface $route); + + /** + * Remove a route by its name. + * + * @param string $routeName + * + * @return void + */ + public function remove($routeName); + + /** + * Get a route by name. + * + * @param string $routeName the name of the route. + * + * @return Route + */ + public function get($routeName); + + /** + * Should check if a route exists with the given name. + * + * @param string $routeName the name of the route. + * + * @return bool + */ + public function has($routeName); + + /** + * Get all registered routes as array. + * + * @return [string => RouteInterface] + */ + public function all(); + + /** + * findByMethod + * + * @param string $method + * + * @return RouteCollectionInterface + */ + public function findByMethod($method); + + /** + * findByScheme + * + * @param mixed $scheme + * + * @return RouteCollectionInterface + */ + public function findByScheme($scheme); +} diff --git a/src/RouteContext.php b/src/RouteContext.php new file mode 100644 index 0000000..565aaa3 --- /dev/null +++ b/src/RouteContext.php @@ -0,0 +1,184 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use Serializable; +use Lucid\Mux\Parser\Variable; +use Lucid\Mux\Parser\TokenInterface; +use Lucid\Mux\Parser\ParserInterface; + +/** + * @class RouteContext + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class RouteContext implements RouteContextInterface, Serializable +{ + /** @var string */ + private $staticPath; + + /** @var string */ + private $regex; + + /** @var array */ + private $vars; + + /** @var array */ + private $tokens; + + /** @var string */ + private $hostRegex; + + /** @var array */ + private $hostVars; + + /** @var array */ + private $hostTokens; + + /** + * Construtor. + * + * @param string $staticPath + * @param string $regex + * @param array $vars + * @param string $hostRegex + * @param array $hostVars + */ + public function __construct($staticPath, $regex, array $tokens = [], $hostRegex = null, array $hostTokens = []) + { + $this->staticPath = $staticPath; + $this->regex = $regex; + $this->tokens = $tokens; + $this->hostRegex = $hostRegex; + $this->hostTokens = $hostTokens; + + $this->vars = $this->filterVars($tokens); + $this->hostVars = $this->filterVars($hostTokens); + } + + /** + * {@inheritdoc} + */ + public function getRegex($raw = false) + { + return $raw ? $this->regex : self::wrapRegex($this->regex); + } + + /** + * {@inheritdoc} + */ + public function getStaticPath() + { + return $this->staticPath; + } + + /** + * {@inheritdoc} + */ + public function getTokens() + { + return $this->tokens; + } + + /** + * {@inheritdoc} + */ + public function getHostTokens() + { + return $this->tokens; + } + + /** + * {@inheritdoc} + */ + public function getVars() + { + return $this->vars; + } + + /** + * {@inheritdoc} + */ + public function getHostRegex($raw = false) + { + return $raw ? $this->hostRegex : self::wrapRegex($this->hostRegex); + } + + /** + * {@inheritdoc} + */ + public function getHostVars() + { + return $this->hostParams; + } + + /** + * serialize + * + * @return string + */ + public function serialize() + { + return serialize([ + 'static_path' => $this->staticPath, + 'regex' => $this->regex, + 'tokens' => $this->tokens, + 'vars' => $this->vars, + 'host_regex' => $this->hostRegex, + 'host_vars' => $this->hostVars, + 'host_tokens' => $this->hostTokens + ]); + } + + /** + * unserialize + * + * @param mixed $data + * + * @return void + */ + public function unserialize($data) + { + $data = unserialize($data); + + $this->staticPath = $data['static_path']; + $this->regex = $data['regex']; + $this->tokens = $data['tokens']; + $this->vars = $data['vars']; + $this->hostRegex = $data['host_regex']; + $this->hostVars = $data['host_vars']; + $this->hostTokens = $data['host_tokens']; + } + + /** + * wrapRegex + * + * @param string $regex + * + * @return string + */ + private static function wrapRegex($regex) + { + return sprintf('%1$s^%2$s$%1$ss', ParserInterface::EXP_DELIM, $regex); + } + + private function filterVars(array $tokens) + { + return array_values(array_map(function (Variable $token) { + return $token->value; + }, array_filter($tokens, function (TokenInterface $token) { + return $token instanceof Variable; + }))); + } +} diff --git a/src/RouteContextInterface.php b/src/RouteContextInterface.php new file mode 100644 index 0000000..a5d8f91 --- /dev/null +++ b/src/RouteContextInterface.php @@ -0,0 +1,77 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +/** + * @interface RouteExpressionInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface RouteContextInterface +{ + /** + * getRegexp + * + * @var bool $raw + * + * @return string + */ + public function getRegex($raw = false); + + /** + * getStaticPath + * + * @return string + */ + public function getStaticPath(); + + /** + * getParameters + * + * @return array + */ + public function getVars(); + + /** + * getTokens + * + * + * @return array `Lucid\Mux\Parser\TokenInterface[]` + */ + public function getTokens(); + + /** + * getHostRegexp + * + * + * @var bool $raw + * + * @return string + */ + public function getHostRegex($raw = false); + + /** + * getHostParameters + * + * @return array + */ + public function getHostVars(); + + /** + * getHostTokens + * + * @return array `Lucid\Mux\Parser\TokenInterface[]` + */ + public function getHostTokens(); +} diff --git a/src/RouteGroup.php b/src/RouteGroup.php new file mode 100644 index 0000000..500ecd1 --- /dev/null +++ b/src/RouteGroup.php @@ -0,0 +1,122 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +/** + * Helper class for generating RoutCollection objects. + * + * @class RouteGroup + * + * @package Lucid\Mux + * @version $Id$ + * @author Thomas Appel + */ +class RouteGroup +{ + /** @var string */ + private $prefix; + + /** @var parent */ + private $parent; + + /** @var array */ + private $requirements; + + /** + * Constructor. + * + * @param string $prefix the group prefix. + * @param array $requirements the group requirements. + * @param RouteGroup $parent the parent group. + */ + public function __construct($prefix, array $requirements, RouteGroup $parent = null) + { + $this->parent = $parent; + + $this->setPrefix($prefix); + $this->setRequirements($requirements); + } + + /** + * Tell if the group has a parent group. + * + * @return boolean + */ + public function hasParent() + { + return null !== $this->parent; + } + + /** + * Get the group prefix. + * + * @return string the prefix. + */ + public function getPrefix() + { + return $this->prefix; + } + + /** + * Get group requirements + * + * @return array the group requirements + */ + public function getRequirements() + { + return $this->requirements; + } + + /** + * Set the group prefix + * + * @return void + */ + private function setPrefix($prefix) + { + if (0 === strlen($prefix)) { + throw new \InvalidArgumentException('Group prefix may not be empty.'); + } + + $prefix = '/'.trim($prefix, '/'); + + $this->prefix = $this->hasParent() ? $this->parent->getPrefix() . $prefix : $prefix; + } + + /** + * Set the group requirements + * + * @return void + */ + private function setRequirements(array $requirements) + { + $requirements = $this->filterRequirements($requirements); + + $this->requirements = $this->hasParent() ? + array_merge($this->parent->getRequirements(), $requirements) : + $requirements; + } + + /** + * Filter group requirements. + * + * @param array $requirements + * + * @return array the filtered requirements. + */ + private function filterRequirements(array $requirements) + { + $keys = ['host', 'schemes']; + + return array_intersect_key($requirements, array_flip($keys)); + } +} diff --git a/src/RouteInterface.php b/src/RouteInterface.php new file mode 100644 index 0000000..61ec699 --- /dev/null +++ b/src/RouteInterface.php @@ -0,0 +1,123 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use Serializable; + +/** + * @interface RouteInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface RouteInterface extends Serializable +{ + /** + * Get the supported methods + * + * @return array a list of supported http methods + */ + public function getMethods(); + + /** + * Tell if the route supports a given http method. + * + * @param string $method + * + * @return boolean `TRUE` if the given method is supported, otherwise `FALSE`. + */ + public function hasMethod($method); + + /** + * Get the route patter. + * + * @return string the route pattern + */ + public function getPattern(); + + /** + * Get the host + * + * @return string the host name, `NULL` if none. + */ + public function getHost(); + + /** + * Get the default parameters. + * + * @return array Array with default route parameters. + */ + public function getDefaults(); + + /** + * Gets a value from the default parameters + * + * @param string $var the parameter name. + * + * @return mixed the parameter value. + */ + public function getDefault($var); + + /** + * Get parameter constraints. + * + * @return array Associative array containing parameter constrains. + */ + public function getConstraints(); + + /** + * Get a parameter constraint by its parameter name. + * + * @return array Associative array containing route constrains. + * + * @return string A constraint expression, typically a regular expression. + */ + public function getConstraint($param); + + /** + * Get the route handler + * + * @return mixed the route handler or its identifyer. + */ + public function getHandler(); + + /** + * Gets the supported url schemes. + * + * @return array a list of supported url schemes + */ + public function getSchemes(); + + /** + * Tell if a given scheme is supported by this route. + * + * @param string $scheme + * + * @return boolean `TRUE` if the given scheme is supported, otherwise `FALSE`. + */ + public function hasScheme($scheme); + + /** + * Get the route context. + * + * If the context doesn't exists, the route will create a new context + * object. + * The context object contains infomation about required variables and the + * regexp matching patter. + * + * @see RouteContextInterface + * + * @return RouteContextInterface the route context object. + */ + public function getContext(); +} diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 0000000..8fd5398 --- /dev/null +++ b/src/Router.php @@ -0,0 +1,253 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use SplStack; +use InvalidArgumentException; +use Lucid\Mux\Request\UrlGenerator; +use Lucid\Mux\Matcher\RequestMatcher; +use Lucid\Mux\Exception\MatchException; +use Lucid\Mux\Request\PassResponseMapper; +use Lucid\Mux\Matcher\Context as MatchContext; +use Lucid\Mux\Request\Context as RequestContext; +use Lucid\Mux\Request\UrlGeneratorInterface as Url; +use Lucid\Mux\Handler\Dispatcher as HandlerDispatcher; +use Lucid\Mux\Matcher\RequestMatcherInterface as Matcher; +use Lucid\Mux\Handler\DispatcherInterface as Dispatcher; +use Lucid\Mux\Request\ResponseMapperInterface as ResponseMapper; +use Lucid\Mux\Matcher\ContextInterface as MatchContextInterface; +use Lucid\Mux\Request\ContextInterface as RequestContextInterface; + +/** + * @class Router + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Router implements RouterInterface +{ + /** @var RouteCollectionInterface */ + private $routes; + + /** @var mixed */ + private $matcher; + + /** @var HandlerDispatcherInterface */ + private $dispatcher; + + /** @var ResponseMapperInterface */ + private $mapper; + + /** @var UrlGeneratorInterface */ + private $url; + + /** @var SplStack */ + private $routeStack; + + /** + * Construtor. + * + * @param RouteCollectionInterface $routes + * @param Matcher $matcher + * @param Dispatcher $dispatcher + * @param ResponseMapper $mapper + * @param Url $url + */ + public function __construct( + RouteCollectionInterface $routes, + Matcher $matcher = null, + Dispatcher $dispatcher = null, + ResponseMapper $mapper = null, + Url $url = null + ) { + $this->routes = $routes; + $this->matcher = $matcher ?: new RequestMatcher; + $this->dispatcher = $dispatcher ?: new HandlerDispatcher; + $this->mapper = $mapper ?: new PassResponseMapper; + $this->url = $url ?: new UrlGenerator($this->routes); + $this->routeStack = new SplStack; + } + + /** + * {@inheritdoc} + */ + public function match(RequestContextInterface $request) + { + return $this->matcher->matchRequest($request, $this->routes); + } + + /** + * {@inheritdoc} + */ + public function dispatch(RequestContextInterface $request) + { + if (($match = $this->match($request)) && $match->isMatch()) { + return $this->dispatchMatch($match); + } + + throw MatchException::noRouteMatch($request); + } + + /** + * Dispatches a request. + * + * @param RequestContextInterface $request + * @param MatchContextInterface $match + * + * @return mixed the request response. + */ + public function dispatchMatch(MatchContextInterface $match) + { + // store the previous request context. + $request = $this->url->getRequestContext(); + + $this->url->setRequestContext($match->getRequest()); + $this->routeStack->push($match->getName()); + + $response = $this->mapper->mapResponse($this->dispatcher->dispatch($match)); + + $this->routeStack->pop(); + + // restore the previous request context. + if (null !== $request) { + $this->url->setRequestContext($request); + } + + return $response; + } + + /** + * {@inheritdoc} + */ + public function route($name, array $vars = [], array $options = []) + { + $options = $this->getOptions($options); + + $rel = 'localhost' === $options['host'] ? true : false; + + $path = $this->getUrl($name, $options['host'], $vars, $rel); + $request = $this->createRequestContextFromOptions($path, $options); + + return $this->dispatchMatch($this->createMatchContextFromParameters($vars, $name, $request)); + } + + /** + * {@inheritdoc} + */ + public function getFirstRoute() + { + if (null === ($name = $this->getFirstRouteName())) { + return; + } + + if ($this->routes->has($name)) { + return $this->routes->get($name); + } + } + + /** + * {@inheritdoc} + */ + public function getFirstRouteName() + { + if (0 < $this->routeStack->count()) { + return $this->routeStack->bottom(); + } + } + + /** + * {@inheritdoc} + */ + public function getCurrentRoute() + { + if (null === ($name = $this->getCurrentRouteName())) { + return; + } + + if ($this->routes->has($name)) { + return $this->routes->get($name); + } + } + + /** + * {@inheritdoc} + */ + public function getCurrentRouteName() + { + if (0 < $this->routeStack->count()) { + return $this->routeStack->top(); + } + } + + /** + * {@inheritdoc} + */ + public function getUrl($name, $host = null, array $vars = [], $rel = true) + { + $rel = $rel ? $type = Url::RELATIVE_PATH : Url::ABSOLUTE_PATH; + + return $this->url->generate($name, $vars, $host, $rel); + } + + /** + * createRequestContextFromOptions + * + * @param array $options + * + * @return RequestContextInterface + */ + private function createRequestContextFromOptions($path, array $options) + { + return new RequestContext( + $path, + $options['method'], + $options['query'], + $options['host'], + $options['scheme'], + $options['port'] + ); + } + + /** + * createMatchContextFromParameters + * + * @param array $parameters + * @param string $url + * + * @return MatchContextInterface + */ + private function createMatchContextFromParameters(array $vars, $name, $request) + { + $handler = $this->routes->get($name)->getHandler(); + + return new MatchContext(Matcher::MATCH, $name, $request, $handler, $vars); + } + + /** + * getOptions + * + * @param array $options + * + * @return array + */ + private function getOptions(array $options) + { + return array_merge([ + 'method' => 'GET', + 'host' => 'localhost', + 'port' => 80, + 'query' => '', + 'scheme' => 'http' + ], $options); + } +} diff --git a/src/RouterInterface.php b/src/RouterInterface.php new file mode 100644 index 0000000..d76a018 --- /dev/null +++ b/src/RouterInterface.php @@ -0,0 +1,99 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +use Lucid\Mux\Matcher\ContextInterface as MatchContext; +use Lucid\Mux\Request\ContextInterface as RequestContext; + +/** + * @interface MultiplexerInterface + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +interface RouterInterface +{ + /** + * dispatch + * + * @param ContextInterface $context + * + * @return void + */ + public function dispatch(RequestContext $context); + + /** + * Dispatches a match. + * + * @param MatchContextInterface $match + * + * @return mixed the request response. + */ + public function dispatchMatch(MatchContext $match); + + /** + * match + * + * @param ContextInterface $context + * + * @return Lucid\Mux\Matcher\ContextInterface + */ + public function match(RequestContext $context); + + /** + * route + * + * @param RouteInterface $route + * + * @return void + */ + public function route($name, array $parameters = [], array $options = []); + + /** + * Get the first route that's been dispatched. + * + * @return RouteInterface the route object, `NULL` if none. + */ + public function getFirstRoute(); + + /** + * Get the first route name that's been dispatched. + * + * @return string the route name, `NULL` if none. + */ + public function getFirstRouteName(); + + /** + * Get the current dispatched route. + * + * @return RouteInterface a route object. + */ + public function getCurrentRoute(); + + /** + * Get the current dispatched route name. + * + * @return string the route name. + */ + public function getCurrentRouteName(); + + /** + * Generates a http url for a given route name. + * + * @param string $name + * @param string $host + * @param array $vars + * @param bool $rel + */ + public function getUrl($name, $host = null, array $vars = [], $rel = true); +} diff --git a/src/Routes.php b/src/Routes.php new file mode 100644 index 0000000..fbdd0eb --- /dev/null +++ b/src/Routes.php @@ -0,0 +1,150 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux; + +/** + * @class RouteCollection + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class Routes implements RouteCollectionInterface +{ + /** @var array */ + protected $routes; + + /** @var array */ + private $methodIndex; + + /** @var array */ + private $schemeIndex; + + /** + * Constructor. + * + * @param array $routes `string[RouteInterface[]]` + */ + public function __construct(array $routes = []) + { + $this->methodIndex = []; + $this->schemeIndex = []; + $this->setRoutes($routes); + } + + public function all() + { + return $this->routes; + } + + /** + * {@inheritdoc} + */ + public function add($routeName, RouteInterface $route) + { + if (!is_string($routeName)) { + throw new \InvalidArgumentException('Routename must be string.'); + } + + $this->routes[$routeName] = &$route; + + foreach ($route->getMethods() as $method) { + $this->methodIndex[$method][$routeName] = true; + } + + foreach ($route->getSchemes() as $scheme) { + $this->schemeIndex[$scheme][$routeName] = true; + } + } + + /** + * {@inheritdoc} + */ + public function remove($routeName) + { + if (!$this->has($routeName)) { + return; + } + + $route = $this->get($routeName); + + foreach ($route->getMethods() as $m) { + unset($this->methodIndex[$m][$routeName]); + } + + foreach ($route->getSchemes() as $s) { + unset($this->schemeIndex[$s][$routeName]); + } + + unset($this->routes[$routeName]); + } + + /** + * {@inheritdoc} + */ + public function has($name) + { + return isset($this->routes[$name]); + } + + /** + * {@inheritdoc} + */ + public function get($routeName) + { + return $this->routes[$routeName]; + } + + /** + * {@inheritdoc} + */ + public function findByMethod($method) + { + $method = strtoupper($method); + + if (!isset($this->methodIndex[$method])) { + return new self([]); + } + + return new self(array_intersect_key($this->routes, $this->methodIndex[$method])); + } + + /** + * {@inheritdoc} + */ + public function findByScheme($scheme) + { + $scheme = strtolower($scheme); + + if (!isset($this->schemeIndex[$scheme])) { + return new self([]); + } + + return new self(array_intersect_key($this->routes, $this->schemeIndex[$scheme])); + } + + /** + * Sets the initial route collection. + * + * @param array $routes + * + * @return void + */ + private function setRoutes(array $routes) + { + $this->routes = []; + + foreach ($routes as $name => $route) { + $this->add($name, $route); + } + } +} diff --git a/tests/Cache/RoutesTest.php b/tests/Cache/RoutesTest.php new file mode 100644 index 0000000..5db0f26 --- /dev/null +++ b/tests/Cache/RoutesTest.php @@ -0,0 +1,59 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Cache\Tests; + +use Lucid\Mux\Route; +use Lucid\Mux\Routes; +use Lucid\Mux\Cache\Routes as CachedRouteCollection; + +/** + * @class CachedRouteCollectionTest + * + * @package Lucid\Mux\Cache + * @version $Id$ + * @author iwyg + */ +class CachedRouteCollectionTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldBeInstantiable() + { + $this->assertTrue(true); + //$routes = new CachedRouteCollection(new Routes); + } + + ///** @test */ + //public function itIsExpectedThat() + //{ + // $c = new Routes; + + // foreach (range(1, 100) as $r) { + // if (0 === $r % 2) { + // $c->add('route_'.$r, new Route('/route/'.$r, 'action_'.$r, 'GET', null, [], [], 'https')); + // } else { + // $c->add('route_'.$r, new Route('/route/'.$r, 'action_'.$r, 'POST')); + // } + // } + + // $cached = new CachedRouteCollection($c); + + // $s1 = microtime(true); + // $cached->findByScheme('https'); + // $ss1 = microtime(true); + // $s2 = microtime(true); + // $c->findByScheme('https'); + // $ss2 = microtime(true); + + // $this->assertTrue(($ss1 - $s1) < ($ss2 - $s2)); + // $this->assertTrue(in_array('route_12', array_keys($cached->findByMethod('GET')->all()))); + //} +} diff --git a/tests/Handler/DispatcherTest.php b/tests/Handler/DispatcherTest.php new file mode 100644 index 0000000..679d5dd --- /dev/null +++ b/tests/Handler/DispatcherTest.php @@ -0,0 +1,64 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Matcher\Context; +use Lucid\Mux\Handler\Reflector; +use Lucid\Mux\Handler\Dispatcher; +use Lucid\Mux\Matcher\RequestMatcherInterface as M; + +class DispatcherTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldBeInstantiable() + { + $this->assertInstanceof('Lucid\Mux\Handler\DispatcherInterface', new Dispatcher); + } + + /** @test */ + public function itShouldDispatchHandler() + { + $called = false; + $ctx = new Context(M::MATCH, 'route', $this->mockRequest('/foo'), 'foo@bar', [], []); + + $dispatcher = new Dispatcher($res = $this->mockResolver()); + $res->method('resolve')->with('foo@bar')->willReturn(new Reflector(function () use (&$called) { + $called = true; + })); + + $dispatcher->dispatch($ctx); + $this->assertTrue($called); + } + + private function mockResolver() + { + return $this->getMockbuilder('Lucid\Mux\Handler\ResolverInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockReflector() + { + return $this->getMockbuilder('Ludic\Mux\Handler\Reflector') + ->disableOriginalConstructor()->getMock(); + } + + private function mockRequest($path = '/') + { + $r = $this->getMockbuilder('Lucid\Mux\Request\ContextInterface') + ->disableOriginalConstructor()->getMock(); + + $r->method('getPath')->willReturn($path); + + return $r; + } +} diff --git a/tests/Handler/PassParameterMapperTest.php b/tests/Handler/PassParameterMapperTest.php new file mode 100644 index 0000000..ef7f772 --- /dev/null +++ b/tests/Handler/PassParameterMapperTest.php @@ -0,0 +1,38 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Handler\PassParameterMapper; + +/** + * @class PassParameterMapperTest + * + * @package Lucid\Mux\Tests\Handler + * @version $Id$ + * @author iwyg + */ +class PassParameterMapperTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldPassParamsWithoutMapping() + { + $mapper = new PassParameterMapper; + + $r = $this->getMockBuilder('Lucid\Mux\Handler\Reflector') + ->disableOriginalConstructor() + ->getMock(); + + $res = $mapper->map($r, $p = ['a' => 1, 'b' => 2]); + + $this->assertSame(array_values($p), $res); + } +} diff --git a/tests/Handler/ReflectorTest.php b/tests/Handler/ReflectorTest.php new file mode 100644 index 0000000..13425ed --- /dev/null +++ b/tests/Handler/ReflectorTest.php @@ -0,0 +1,117 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Handler\Reflector; + +/** + * @class ReflectorTest + * + * @package Lucid\Mux\Tests\Handler + * @version $Id$ + * @author iwyg + */ +class ReflectorTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldBeTypeFunction() + { + $r = new Reflector('is_array'); + + $this->assertTrue($r->isFunction()); + } + + /** @test */ + public function itShouldBeTypeClosure() + { + $r = new Reflector(function () { + }); + + $this->assertTrue($r->isClosure()); + } + + /** @test */ + public function itShouldBeTypeMethod() + { + $r = new Reflector([$this, 'testMethod']); + + $this->assertTrue($r->isMethod()); + $this->assertTrue($r->isInstanceMethod()); + } + + /** @test */ + public function itShouldBeTypeStaticMethod() + { + $r = new Reflector(__CLASS__.'::staticTestMethod'); + + $this->assertTrue($r->isMethod()); + $this->assertTrue($r->isStaticMethod()); + + $rf = $r->getReflector(); + $res = $rf->invokeArgs($this, $args = [1, 2]); + $this->assertSame($res, $args); + + $r = new Reflector([__CLASS__, 'staticTestMethod']); + + $this->assertTrue($r->isMethod()); + $this->assertTrue($r->isStaticMethod()); + + $rf = $r->getReflector(); + $res = $rf->invokeArgs($this, $args = [3, 4]); + $this->assertSame($res, $args); + } + + /** @test */ + public function itShouldBeInvokedObjectType() + { + $obj = $this->getMock('InvokedObjMock', ['__invoke']); + $obj->method('__invoke')->willReturn('invoked'); + + $r = new Reflector($obj); + + $this->assertTrue($r->isInvokedObject()); + + $rf = $r->getReflector(); + $this->assertSame('invoked', $rf->invoke($obj)); + } + + /** @test */ + public function itShouldBeInvokable() + { + $r = new Reflector([$this, 'testMethod']); + + $this->assertTrue($r->invokeArgs([])); + } + + + /** @test */ + public function itShouldReturnRightReflector() + { + $r = new Reflector([$this, 'testMethod']); + + $this->assertInstanceof('ReflectionMethod', $r->getReflector()); + + $r = new Reflector('array_map'); + + $this->assertInstanceof('ReflectionFunction', $r->getReflector()); + } + + public function testMethod() + { + return true; + } + + public static function staticTestMethod(...$args) + { + return $args; + } +} diff --git a/tests/Handler/ResolverTest.php b/tests/Handler/ResolverTest.php new file mode 100644 index 0000000..0aea9a2 --- /dev/null +++ b/tests/Handler/ResolverTest.php @@ -0,0 +1,143 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Handler\Resolver; +use Lucid\Mux\Exception\ResolverException; +use Lucid\Mux\Tests\Handler\Stubs\SimpleHandler; + +/** + * @class ResolverTest + * + * @package Lucid\Mux\Tests\Handler + * @version $Id$ + * @author iwyg + */ +class ResolverTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldParseStaticHandlers() + { + $handlerStr = __CLASS__.'::fakeStaticHandle'; + $foulStr = __CLASS__.'::foulStaticHandle'; + + $resolver = new Resolver(); + $this->assertInstanceof('Lucid\Mux\Handler\Reflector', $resolver->resolve($handlerStr)); + + try { + $resolver->resolve($foulStr); + } catch (\RuntimeException $e) { + $this->assertSame('No routing handler could be found.', $e->getMessage()); + } + } + + /** @test */ + public function itShouldParseCallables() + { + $resolver = new Resolver(); + $this->assertInstanceof( + 'Lucid\Mux\Handler\Reflector', + $resolver->resolve( + function () { + return true; + } + ) + ); + } + + /** @test */ + public function itShouldParseNoneStaticHandlerAnnotation() + { + $handlerStr = __NAMESPACE__.'\Stubs\SimpleHandler@noneParamAction'; + + $resolver = new Resolver(); + + $handler = $resolver->resolve($handlerStr); + + $this->assertInstanceof('Lucid\Mux\Handler\Reflector', $handler); + } + + /** @test */ + public function itShouldParseHandlerAsService() + { + $container = $this->mockContainer(['handler' => $this, 'simple_handler' => new SimpleHandler]); + $resolver = new Resolver; + $resolver->setContainer($container); + + $this->assertInstanceof( + 'Lucid\Mux\Handler\Reflector', + $resolver->resolve('handler@fakeAction') + ); + $this->assertInstanceof( + 'Lucid\Mux\Handler\Reflector', + $resolver->resolve('simple_handler@noneParamAction') + ); + } + + /** @test */ + public function itShouldResolveInvokables() + { + $handler = $this->getMock(__NAMESPACE__.'\InvokableHandler', ['__invoke']); + $handler->method('__invoke')->willReturnCallback(function () { + return 'ok'; + }); + $container = $this->mockContainer(['handler' => $this, 'inv.handler' => $handler]); + + $resolver = new Resolver($container); + $this->assertInstanceOf('Lucid\Mux\Handler\Reflector', $resolver->resolve('inv.handler')); + } + + /** @test */ + public function itShouldThrowResolverExceptionIfHandlerIsNotInstantiable() + { + $resolver = new Resolver(); + $handler = 'Lucid\Mux\Tests\Handler\Stubs\AbstractHandler@handle'; + + try { + $resolver->resolve($handler); + } catch (ResolverException $e) { + $this->assertSame(0, strpos($e->getMessage(), 'Can\'t resolve handler: ')); + } + } + + private function mockContainer(array $services = []) + { + $container = $this->getMockbuilder('Interop\Container\ContainerInterface') + ->disableOriginalConstructor() + ->getMock(); + + $gmap = []; + + foreach ($services as $id => $service) { + $gmap[] = [$id, $service]; + } + + $hmap[] = [$this->any(), false]; + + $container->method('get')->will($this->returnValueMap($gmap)); + $container->method('has')->willReturnCallback(function ($key) use ($services) { + return isset($services[$key]); + }); + + return $container; + } + + public static function fakeStaticHandle() + { + return true; + } + + public function fakeAction() + { + return true; + } +} diff --git a/tests/Handler/StrictParameterMapperTest.php b/tests/Handler/StrictParameterMapperTest.php new file mode 100644 index 0000000..5cd8c0e --- /dev/null +++ b/tests/Handler/StrictParameterMapperTest.php @@ -0,0 +1,126 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Handler\StrictParameterMapper as Mapper; + +/** + * @class StrictParameterMapperTest + * + * @package Lucid\Mux\Tests\Handler + * @version $Id$ + * @author iwyg + */ +class StrictParameterMapperTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function shouldHandleStrictParams() + { + $r = $this->mockReflector(function ($b, $a) { + }); + + $mapper = new Mapper; + + $res = $mapper->map($r, ['a' => 'foo', 'b' => 'bar']); + + $this->assertSame(['bar', 'foo'], $res); + } + + /** @test */ + public function shouldThrowExceptionIfParamIsMissing() + { + $r = $this->mockReflector(function ($b, $a) { + }); + + $mapper = new Mapper; + + try { + $mapper->map($r, ['a' => 'foo']); + } catch (\UnexpectedValueException $e) { + $this->assertSame('Missing non optional parameter "{$b}".', $e->getMessage()); + + return; + } + + $this->fail('Test should have thrown exception.'); + } + + /** @test */ + public function shouldNotThrowExceptionIfParamIsMissingIsOptional() + { + $r = $this->mockReflector(function ($b, $a = null) { + }); + + $mapper = new Mapper; + $res = $mapper->map($r, ['b' => 'bar']); + + $this->assertSame(['bar', null], $res); + } + + /** @test */ + public function itShouldAskTypeMapper() + { + $r = $this->mockReflector(function (\PHPUnit_Framework_TestCase $val) { + }); + + $mapper = new Mapper($tm = $this->mockTypes()); + + $tm->method('has')->with('PHPUnit_Framework_TestCase')->willReturn(true); + $tm->method('get')->with('PHPUnit_Framework_TestCase')->willReturn($this); + + $res = $mapper->map($r, []); + + $this->assertSame([$this], $res); + } + + /** @test */ + public function itShouldThrowIfTypeIsMissing() + { + $r = $this->mockReflector(function (\PHPUnit_Framework_TestCase $val) { + }); + + $mapper = new Mapper($tm = $this->mockTypes()); + + $tm->method('has')->with('PHPUnit_Framework_TestCase')->willReturn(false); + + try { + $mapper->map($r, []); + } catch (\UnexpectedValueException $e) { + $this->assertSame('Cannot map class "PHPUnit_Framework_TestCase" to parameter "{$val}".', $e->getMessage()); + return; + } + + $this->fail('Test should have thrown exception.'); + } + + protected function mockTypes() + { + return $this->getMockbuilder('Lucid\Mux\Handler\TypeMapCollectionInterface') + ->disableOriginalConstructor()->getMock(); + } + + protected function mockTypeMapper() + { + return $this->getMockbuilder('Lucid\Mux\Hander\TypeMapperInterface') + ->disableOriginalConstructor()->getMock(); + } + + protected function mockReflector(\Closure $fn) + { + $r = $this->getMockBuilder('Lucid\Mux\Handler\Reflector') + ->disableOriginalConstructor()->getMock(); + + $r->method('getReflector')->willReturn(new \ReflectionFunction($fn)); + + return $r; + } +} diff --git a/tests/Handler/Stubs/AbstractHandler.php b/tests/Handler/Stubs/AbstractHandler.php new file mode 100644 index 0000000..a382e25 --- /dev/null +++ b/tests/Handler/Stubs/AbstractHandler.php @@ -0,0 +1,14 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler\Stubs; + +/** + * @class Handler + * + * @package Lucid\Mux\Tests\Handler\Stubs + * @version $Id$ + * @author iwyg + */ +class SimpleHandler +{ + public function noneParamAction() + { + return true; + } +} diff --git a/tests/Handler/TypeMapCollectionTest.php b/tests/Handler/TypeMapCollectionTest.php new file mode 100644 index 0000000..9e3eb55 --- /dev/null +++ b/tests/Handler/TypeMapCollectionTest.php @@ -0,0 +1,45 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Handler; + +use Lucid\Mux\Handler\TypeMapCollection; + +/** + * @class TypeMapCollectionTest + * + * @package Lucid\Mux\Tests\Handler + * @version $Id$ + * @author iwyg + */ +class TypeMapCollectionTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldHandleTypeMapper() + { + $mapper = $this->mockMapper(); + $mapper->expects($this->any())->method('getType')->willReturn('stdClass'); + $mapper->expects($this->any())->method('getObject')->willReturn($obj = new \stdClass); + + $tc = new TypeMapCollection([$mapper]); + + $this->assertTrue($tc->has('stdClass')); + $this->assertSame($obj, $tc->get('stdClass')); + $this->assertTrue($mapper === $tc->getMapper('stdClass')); + $this->assertNull($tc->getMapper('someClass')); + } + + protected function mockMapper() + { + return $this->getMockBuilder('Lucid\Mux\Handler\TypeMapperInterface') + ->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/Loader/Fixures/faulty.php b/tests/Loader/Fixures/faulty.php new file mode 100644 index 0000000..7679934 --- /dev/null +++ b/tests/Loader/Fixures/faulty.php @@ -0,0 +1,3 @@ + [ + 'pattern' => '/', + 'method' => 'GET', + 'handler' => 'indexAction' + ], + 'backstage' => [ + 'requirements' => [ + RouteCollectionBuilder::K_SCHEME => 'https', + RouteCollectionBuilder::K_HOST => 'example.com' + ], + [ + 'users' => [ + 'pattern' => '/user/{id}', + 'method' => 'GET', + 'handler' => 'userAction' + ], + + 'affiliates' => [ + 'pattern' => '/affiliates/{id}', + 'method' => 'GET', + 'handler' => 'afltAction' + ], + ] + ] +]; diff --git a/tests/Loader/Fixures/route_groups.1.php b/tests/Loader/Fixures/route_groups.1.php new file mode 100644 index 0000000..fc2bc5d --- /dev/null +++ b/tests/Loader/Fixures/route_groups.1.php @@ -0,0 +1,20 @@ + [ + 'pattern' => '/', + 'method' => 'GET', + 'handler' => 'indexAction' + ], + 'backstage' => [ + [ + 'users' => [ + 'pattern' => '/user/{id}', + 'method' => 'GET', + 'handler' => 'userAction' + ] + ] + ] +]; diff --git a/tests/Loader/Fixures/route_groups.2.php b/tests/Loader/Fixures/route_groups.2.php new file mode 100644 index 0000000..85ab6d2 --- /dev/null +++ b/tests/Loader/Fixures/route_groups.2.php @@ -0,0 +1,21 @@ + [ + 'pattern' => '/', + 'method' => 'GET', + 'handler' => 'indexAction' + ], + 'backstage' => [ + 'pattern' => '/admin/area', + [ + 'users' => [ + 'pattern' => '/user/{id}', + 'method' => 'GET', + 'handler' => 'userAction' + ] + ] + ] +]; diff --git a/tests/Loader/Fixures/routes.0.php b/tests/Loader/Fixures/routes.0.php new file mode 100644 index 0000000..6f82e4e --- /dev/null +++ b/tests/Loader/Fixures/routes.0.php @@ -0,0 +1,14 @@ + [ + 'pattern' => '/', + 'method' => 'GET', + 'handler' => 'indexAction' + ], + 'users' => [ + 'pattern' => '/user/{id}', + 'method' => 'GET', + 'handler' => 'userAction' + ], +]; diff --git a/tests/Loader/PhpLoaderTest.php b/tests/Loader/PhpLoaderTest.php new file mode 100644 index 0000000..1ecef36 --- /dev/null +++ b/tests/Loader/PhpLoaderTest.php @@ -0,0 +1,104 @@ +mockLocator()); + + $this->assertInstanceOf('Lucid\Mux\RouteCollectionInterface', $routes = $loader->loadRoutes('routes.php')); + } + + /** @test */ + public function itShouldReturnInputCollection() + { + $loader = new PhpLoader($this->mockLocator(), $routes = new Routes); + + $this->assertSame($routes, $loader->loadRoutes('routes.php')); + } + + /** @test */ + public function itShouldLoadRoutes() + { + $loader = new PhpLoader($this->mockLocator()); + $routes = $loader->loadRoutes('routes.0.php'); + + $this->assertTrue($routes->has('index')); + } + + /** @test */ + public function itShouldLoadGroups() + { + $loader = new PhpLoader($this->mockLocator()); + $routes = $loader->loadRoutes('route_groups.0.php'); + + $this->assertTrue($routes->has('users')); + $this->assertTrue($routes->has('affiliates')); + + $this->assertSame('example.com', $routes->get('users')->getHost()); + $this->assertSame(['https'], $routes->get('affiliates')->getSchemes()); + + $this->assertSame('example.com', $routes->get('affiliates')->getHost()); + $this->assertSame(['https'], $routes->get('users')->getSchemes()); + + $loader = new PhpLoader($this->mockLocator()); + $routes = $loader->loadRoutes('route_groups.1.php'); + + $this->assertNull($routes->get('users')->getHost()); + $this->assertSame(['http', 'https'], $routes->get('users')->getSchemes()); + + $loader = new PhpLoader($this->mockLocator()); + $routes = $loader->loadRoutes('route_groups.2.php'); + + $this->assertNull($routes->get('users')->getHost()); + $this->assertSame(['http', 'https'], $routes->get('users')->getSchemes()); + } + + /** @test */ + public function itShouldThrowOnInvalidConfig() + { + $loader = new PhpLoader($this->mockLocator()); + + try { + $loader->loadRoutes('faulty.php'); + } catch (\Lucid\Resource\Exception\LoaderException $e) { + $this->assertEquals('Return value must be array.', $e->getMessage()); + return; + } + + $this->fail(); + } + + private function mockLocator() + { + $locator = $this->getMockbuilder('Lucid\Resource\LocatorInterface') + ->disableOriginalConstructor() + ->getMock(); + + $locator->method('locate')->willReturnCallback(function ($file) { + $collection = new Collection; + + if (file_exists($file = $this->fixure($file))) { + $collection->addFileResource($file); + } + + return $collection; + }); + + return $locator; + } + + private function fixure($file) + { + $prefix = __DIR__.DIRECTORY_SEPARATOR.'Fixures'.DIRECTORY_SEPARATOR; + + return $prefix.$file; + } +} diff --git a/tests/Matcher/ContextTest.php b/tests/Matcher/ContextTest.php new file mode 100644 index 0000000..1b378e0 --- /dev/null +++ b/tests/Matcher/ContextTest.php @@ -0,0 +1,67 @@ +assertInstanceOf( + 'Lucid\Mux\Matcher\ContextInterface', + new Context(Matcher::NOMATCH, null, $this->mockRequest(), null) + ); + } + + /** @test */ + public function contextWillReturnProperties() + { + $r = $this->mockRequest(); + $r->method('getPath')->willReturn('/'); + $ctx = new Context(Matcher::MATCH, 'index', $r, 'action', $vars = ['id' => 12]); + $this->assertTrue($ctx->isMatch()); + $this->assertEquals('/', $ctx->getPath()); + $this->assertEquals('index', $ctx->getName()); + $this->assertEquals('action', $ctx->getHandler()); + $this->assertSame($vars, $ctx->getVars()); + $this->assertTrue($r === $ctx->getRequest()); + } + + /** @test */ + public function itShouldBeMatchFailure() + { + $ctx = new Context(Matcher::NOMATCH, null, $this->mockRequest(), null); + $this->assertFalse($ctx->isMatch()); + } + + /** @test */ + public function itShouldBeMethodFailure() + { + $ctx = new Context(Matcher::NOMATCH_METHOD, null, $this->mockRequest(), null); + $this->assertTrue($ctx->isMethodMissmatch()); + } + + /** @test */ + public function itShouldBeHostFailure() + { + $ctx = new Context(Matcher::NOMATCH_HOST, null, $this->mockRequest(), null); + $this->assertTrue($ctx->isHostMissmatch()); + } + + /** @test */ + public function itShouldBeSchemeFailure() + { + $ctx = new Context(Matcher::NOMATCH_SCHEME, null, $this->mockRequest(), null); + $this->assertTrue($ctx->isSchemeMissmatch()); + } + + private function mockRequest() + { + return $this->getMockbuilder('Lucid\Mux\Request\ContextInterface') + ->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/Matcher/RequestMatcherTest.php b/tests/Matcher/RequestMatcherTest.php new file mode 100644 index 0000000..b497812 --- /dev/null +++ b/tests/Matcher/RequestMatcherTest.php @@ -0,0 +1,114 @@ +assertInstanceOf('Lucid\Mux\Matcher\RequestMatcherInterface', new Matcher); + } + + /** @test */ + public function itShouldMatchRequests() + { + $matcher = new Matcher; + + $routes = $this->mockRoutes(); + $routes->expects($this->once())->method('findByMethod')->willReturn($routes); + $routes->expects($this->once())->method('findByScheme')->willReturn($routes); + + $routes->method('all')->willReturn(['foo' => $route = new Route('/foo/{user}/{id}', 'action')]); + + $request = $this->mockRequest(); + $request->method('getMethod')->willReturn('GET'); + $request->method('getPath')->willReturn('/foo/bar/12.2'); + $request->method('getScheme')->willReturn('http'); + + $ret = $matcher->matchRequest($request, $routes); + + $this->assertTrue($ret->isMatch()); + } + + /** @test */ + public function itShouldReturnCorrectMissmatchReason() + { + $matcher = new Matcher(); + + $routes = new Routes; + + $routes->add('user.show', new Route('/user/{id}', 'showUserAction', ['GET'], 'example.com')); + $routes->add('user.delete', new Route('/user/{id}', 'deleteUserAction', ['DELETE'], 'example.{tld}')); + + $request = $this->mockRequest(); + + $request->method('getMethod')->willReturn('PATCH'); + $request->method('getPath')->willReturn('/user/12'); + $request->method('getScheme')->willReturn('http'); + $request->method('getHost')->willReturn('example.com'); + + $result = $matcher->matchRequest($request, $routes); + + $this->assertTrue($result->isMethodMissmatch(), 'Reason should be method missmatch.'); + + // replace delete + $routes = new Routes; + + $routes->add( + 'user_delete', + new Route('/user/{id}', 'deleteUserAction', ['DELETE'], 'example.org') + ); + + $request = $this->mockRequest(); + $request->method('getMethod')->willReturn('DELETE'); + $request->method('getPath')->willReturn('/user/12'); + $request->method('getScheme')->willReturn('http'); + $request->method('getHost')->willReturn('example.com'); + + $result = $matcher->matchRequest($request, $routes); + + $this->assertTrue($result->isHostMissmatch(), 'Reason should be host missmatch.'); + + // replace delete + $routes = new Routes; + + $routes->add( + 'index', + new Route('/', 'indexAction', ['GET'], null, null, null, ['https']) + ); + + $request = $this->mockRequest(); + $request->method('getMethod')->willReturn('GET'); + $request->method('getPath')->willReturn('/'); + $request->method('getScheme')->willReturn('http'); + + $result = $matcher->matchRequest($request, $routes); + + $this->assertTrue($result->isSchemeMissmatch(), 'Reason should be scheme missmatch.'); + } + + private function mockRoute() + { + return $this->getMockbuilder('Lucid\Mux\Route') + ->disableOriginalConstructor() + ->getMock(); + } + + private function mockRequest() + { + return $this->getMockbuilder('Lucid\Mux\Request\Context') + ->disableOriginalConstructor() + ->getMock(); + } + + private function mockRoutes() + { + return $this->getMockbuilder('Lucid\Mux\RouteCollectionInterface') + ->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/Parser/StandardTest.php b/tests/Parser/StandardTest.php new file mode 100644 index 0000000..56b8d71 --- /dev/null +++ b/tests/Parser/StandardTest.php @@ -0,0 +1,141 @@ +mockRoute(); + $r->method('getPattern')->willReturn('/foo/bar/{a}~bar;{b?}'); + + $t = Parser::tokenizePattern($r->getPattern(), false); + + $this->assertSame(7, count($t)); + + array_map(function ($thing) { + $this->assertInstanceOf('Lucid\Mux\Parser\TokenInterface', $thing); + }, $t); + } + + /** @test */ + public function parseShouldReturnRouteContext() + { + $r = $this->mockRoute(); + $r->method('getPattern')->willReturn('/foo/bar/{a?}'); + $this->prepareRoute($r, [], ['a' => '(\d+)']); + + $ctx = Parser::parse($r); + $this->assertInstanceOf('Lucid\Mux\RouteContextInterface', $ctx); + + $r = $this->mockRoute(); + $r->method('getPattern')->willReturn('{a}'); + $this->prepareRoute($r, [], ['a' => '(\d+)']); + + $ctx = Parser::parse($r); + $this->assertSame('/', $ctx->getStaticPath()); + } + + /** + * @test + * @dataProvider patternMatchProvider + */ + public function transpiledExpressionsShouldMatch($pattern, $host, array $matches, array $cns = [], array $def = []) + { + extract(Parser::transpilePattern($pattern, $host, $cns, $def)); + + $delim = ParserInterface::EXP_DELIM; + $regex = sprintf('%1$s^%2$s$%1$s', $delim, $expression); + + foreach ($matches as $m) { + list($path, $match) = array_pad((array)$m, 2, true); + + if ($match !== $matched = (bool)preg_match_all($regex, $path)) { + $reason = $match ? 'should' : 'shouldn\'t'; + $this->fail(sprintf('Regex %s %s match path %s', $regex, $reason, $path)); + } + + $this->assertSame($match, $matched); + } + } + + public function patternMatchProvider() + { + return [ + [ + 'foo/{bar}', false, + [['/foo/bar', true], ['/foo/baz', true], ['/foo/bar/str', false]] + ], + [ + '/foo/{bar?}', false, + [['/foo/bar', false], ['/foo/12', true]], + ['bar' => '\d+'] + ], + [ + '/foo/{bar?}{baz?}', false, + [['/foo/bar', false], ['/foo/12', true], ['/foo/12a', true]], + ['bar' => '\d+', 'baz' => '\w+'] + ], + [ + '/foo/{bar}/baz', false, + [['/foo/bar/baz', true], ['/foo/baz', false], ['/foo/12/baz', true]] + ], + [ + '/{file}.jpg', false, + [['/image.jpg', true], ['/image.gif', false]] + ], + [ + '/{file}.{ext}', false, + [['/image.jpeg', true], ['/image.jpg', true], ['/image.png', true], ['/image.gif', false]], + ['ext' => 'jpe?g|png'] + ], + ]; + } + + private function mockRoute() + { + return $this->getMockbuilder(Route::class) + ->disableOriginalConstructor() + ->getMock(); + } + + + /** + * prepareRoute + * + * @param Mock $route + * @param array $def + * @param array $const + * + * @return void + */ + private function prepareRoute($route, array $def = null, array $const = null) + { + if (null !== $def) { + $route->method('getDefaults')->willReturn($def); + $dmap = []; + foreach ($def as $key => $value) { + $dmap[] = [$key, $value]; + } + + $route->method('getDefault')->will($this->returnValueMap($dmap)); + } + + if (null !== $const) { + $route->method('getConstraints')->willReturn($const); + $cmap = []; + foreach ($const as $key => $value) { + $cmap[] = [$key, $value]; + } + + $route->method('getConstraint')->will($this->returnValueMap($cmap)); + } + } +} diff --git a/tests/Parser/VariableTest.php b/tests/Parser/VariableTest.php new file mode 100644 index 0000000..a65bd4d --- /dev/null +++ b/tests/Parser/VariableTest.php @@ -0,0 +1,18 @@ +assertSame('(?P[^/]++)', (string)$var); + + $var = new Variable('foo', true, '(\d+)'); + $this->assertSame('(?P(\d+))', (string)$var); + } +} diff --git a/tests/Request/ContextTest.php b/tests/Request/ContextTest.php new file mode 100644 index 0000000..06251f2 --- /dev/null +++ b/tests/Request/ContextTest.php @@ -0,0 +1,75 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + + +namespace Lucid\Mux\Tests\Request; + +use Lucid\Mux\Request\Context; +use Zend\Diactoros\ServerRequestFactory; +use Lucid\Mux\Tests\Request\Fixures\Server; + +/** + * @class ContextTest + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class ContextTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldBeInstantiable() + { + $this->assertInstanceOf('Lucid\Mux\Request\ContextInterface', $this->newContext()); + } + + /** @test */ + public function itIsExpectedThat() + { + $req = $this->mockZendRequest(['REQUEST_URI' => '/foo/bar?baz=bar', 'HTTP_HOST' => 'example.com']); + + $ctx = Context::fromPsrRequest($req); + + $this->assertSame('/foo/bar', $ctx->getPath()); + $this->assertSame('example.com', $ctx->getHost()); + $this->assertSame(80, $ctx->getHttpPort()); + $this->assertSame('http', $ctx->getScheme()); + + } + + private function newContext() + { + return new Context; + } + + private function mockZendRequest(array $server = [], array $get = [], array $request = []) + { + return ServerRequestFactory::fromGlobals(Server::mock($server)); + } + + private function mockPsrRequest() + { + $rq = $this->getMockbuilder('Psr\Http\Message\ServerRequestInterface') + ->disableOriginalConstructor() + ->getMock(); + + $rq->method('getUri')->willReturn($this->mockPsrUri()); + + return $rq; + } + + private function mockPsrUri() + { + return $this->getMockbuilder('Psr\Http\Message\UriInterface') + ->disableOriginalConstructor() + ->getMock(); + } +} diff --git a/tests/Request/Fixures/Server.php b/tests/Request/Fixures/Server.php new file mode 100644 index 0000000..2b217cf --- /dev/null +++ b/tests/Request/Fixures/Server.php @@ -0,0 +1,61 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests\Request\Fixures; + +/** + * @class Server + * + * @package Lucid\Mux\Tests\Request\Fixures + * @version $Id$ + * @author iwyg + */ +class Server +{ + public static function mock(array $server = []) + { + $time = microtime(true); + return array_merge([ + 'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,it;q=0.2', + 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch', + 'HTTP_DNT' => '1', + 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36', + 'HTTP_UPGRADE_INSECURE_REQUESTS' => '1', + 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'HTTP_CACHE_CONTROL' => 'max-age=0', + 'HTTP_CONNECTION' => 'keep-alive', + 'HTTP_HOST' => '192.168.99.100', + 'REDIRECT_STATUS' => 200, + 'SERVER_NAME' => 'default', + 'SERVER_PORT' => 80, + 'SERVER_ADDR' => '172.17.0.6', + 'REMOTE_PORT' => '51543', + 'REMOTE_ADDR' => '192.168.99.1', + 'SERVER_SOFTWARE' => 'nginx/1.9.9', + 'GATEWAY_INTERFACE' => 'CGI/1.1', + 'REQUEST_SCHEME' => 'http', + 'SERVER_PROTOCOL' => 'HTTP/1.1', + 'DOCUMENT_ROOT' => '/var/www/app/public', + 'DOCUMENT_URI' => '/index.php', + 'REQUEST_URI' => '/', + 'SCRIPT_NAME' => '/index.php', + 'CONTENT_LENGTH' => '', + 'CONTENT_TYPE' => '', + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => '', + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', + 'FCGI_ROLE' => 'RESPONDER', + 'PHP_SELF' => '/index.php', + 'REQUEST_TIME_FLOAT' => $time, + 'REQUEST_TIME' => (int)$time, + ], $server); + } +} diff --git a/tests/Request/UrlGeneratorTest.php b/tests/Request/UrlGeneratorTest.php new file mode 100644 index 0000000..eef793a --- /dev/null +++ b/tests/Request/UrlGeneratorTest.php @@ -0,0 +1,117 @@ +assertInstanceof('Lucid\Mux\Request\UrlGeneratorInterface', new UrlGenerator($this->mockRoutes())); + } + + /** + * @test + * @dataProvider generatorProvider + */ + public function itShouldGenerateUrls($name, $params, $host, $pattern, $rHost, $rDefaults, $expected) + { + $url = new UrlGenerator(); + $url->setRoutes($routes = $this->mockRoutes()); + + $route = new Route($pattern, 'action', ['GET'], $rHost, $rDefaults); + + $has = [ + [$name, true] + ]; + + $get = [ + [$name, $route] + ]; + + $routes->expects($this->any()) + ->method('has') + ->will($this->returnValueMap($has)); + $routes->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($get)); + + $this->assertSame($expected, $url->generate($name, $params, $host)); + } + + /** @test */ + public function itShouldGetCurrentUrl() + { + $url = new UrlGenerator(); + $url->setRoutes($routes = $this->mockRoutes()); + + $this->assertSame('/', $url->currentUrl()); + $this->assertSame('http://localhost/', $url->currentUrl(UrlGenerator::ABSOLUTE_PATH)); + + $url->setRequestContext(new RequestContext('foo/bar', 'GET', '?foo=bar', 'example.com/')); + + $this->assertSame('foo/bar?foo=bar', $url->currentUrl()); + $this->assertSame('http://example.com/foo/bar?foo=bar', $url->currentUrl(UrlGenerator::ABSOLUTE_PATH)); + + $this->assertNull($url->currentUrl('unknown_flag')); + } + + /** @test */ + public function itShouldGenerateFullPaths() + { + $url = new UrlGenerator( + new Routes(['foo' => $r = new Route('/foo', 'action', ['GET'], 'example.com')]) + ); + + $this->assertSame('http://example.com/foo', $url->generate('foo', [], null, UrlGenerator::ABSOLUTE_PATH)); + + $url = new UrlGenerator( + new Routes(['foo' => $r = new Route('/foo', 'action', ['GET'])]) + ); + + $this->assertSame( + 'http://example.com/foo', + $url->generate('foo', [], 'example.com', UrlGenerator::ABSOLUTE_PATH) + ); + + $url = new UrlGenerator( + new Routes(['foo' => $r = new Route('/foo', 'action', ['GET'])]) + ); + + $this->assertSame('http://localhost/foo', $url->generate('foo', [], null, UrlGenerator::ABSOLUTE_PATH)); + } + + /** @test */ + public function generateShouldFail() + { + $url = new UrlGenerator(); + $url->setRoutes($routes = $this->mockRoutes()); + $routes->method('get')->willReturn(null); + + try { + $url->generate('index'); + } catch (\InvalidArgumentException $e) { + $this->assertSame('A route with name "index" could not be found.', $e->getMessage()); + } + } + + public function generatorProvider() + { + return [ + ['index', [], null, '/', null, [], '/'], + ['route', [], null, '/{param?}', null, [], '/'], + ['route', ['id' => 12], null, '/{id}', null, [], '/12'], + ['route', ['param' => 'lenny'], null, '/path/to/{param}', null, [], '/path/to/lenny'], + ]; + } + + protected function mockRoutes() + { + return $this->getMock('Lucid\Mux\RouteCollectionInterface'); + } +} diff --git a/tests/RouteCollectionBuilderTest.php b/tests/RouteCollectionBuilderTest.php new file mode 100644 index 0000000..fabe609 --- /dev/null +++ b/tests/RouteCollectionBuilderTest.php @@ -0,0 +1,151 @@ +assertInstanceof('Lucid\Mux\RouteCollectionBuilder', new Builder); + } + + /** @test */ + public function itShouldAutoGenerateNames() + { + $builder = $this->newBuilder(); + $builder->get('/app/{user}/{area}', 'action', ['user' => '\w+', 'area' => '\d+']); + + $name = current(array_keys($builder->getCollection()->all())); + + $this->assertStringStartsWith('route_GET_', $name); + + $builder = $this->newBuilder(); + $builder->post('/app/{user}/{area}', 'action', $c = ['user' => '\w+', 'area' => '\d+']); + + $name = current(array_keys($builder->getCollection()->all())); + + $this->assertStringStartsWith('route_POST_', $name); + } + + /** @test */ + public function itShouldObtainDefaults() + { + $builder = $this->newBuilder(); + $builder->get('/{foo}', 'action', [Builder::K_NAME => 'index'], ['foo' => 'bar']); + + $this->assertSame('bar', $builder->getCollection()->get('index')->getDefault('foo')); + } + + /** @test */ + public function itShouldObtainConstraints() + { + $builder = $this->newBuilder(); + $builder->get('/{foo}', 'action', [Builder::K_NAME => 'index', 'foo' => '\w+']); + + $this->assertSame('\w+', $builder->getCollection()->get('index')->getConstraint('foo')); + } + + /** + * @test + * @dataProvider methodProvider + */ + public function itShouldCreateRoutesWithMethodShortCuts($method, $args, $expected) + { + $builder = $this->newBuilder(); + + call_user_func_array([$builder, $method], $args); + + $routes = $builder->getCollection(); + + $this->assertTrue((bool)($route = $routes->get($args[2][Builder::K_NAME]))); + + foreach ($expected as $m) { + $this->assertTrue($route->hasMethod($m)); + } + } + + /** @test */ + public function itShouldObtainDefaultSchemes() + { + $builder = $this->newBuilder(); + $builder->get('/', 'action'); + + $this->assertEquals(['http', 'https'], current($builder->getCollection()->all())->getSchemes()); + } + + /** @test */ + public function itShouldBuildGroups() + { + $builder = $this->newBuilder(); + + $builder->group('/secure', [Builder::K_SCHEME => 'https'], function ($builder) { + $builder->get('login', 'action_login', [Builder::K_NAME => 'login.index']); + $builder->delete('login', 'action_logout', [Builder::K_NAME => 'logout.action']); + }); + + $routes = $builder->getCollection(); + + $this->assertTrue((bool)($a = $routes->get('login.index'))); + $this->assertTrue((bool)($b = $routes->get('logout.action'))); + + // test for prepended path prefix + $this->assertSame('/secure/login', $a->getPattern()); + $this->assertSame('/secure/login', $b->getPattern()); + + // test for shared schemes + $this->assertSame(['https'], $a->getSchemes()); + $this->assertSame(['https'], $b->getSchemes()); + + + $builder = $this->newBuilder(); + $builder->group('/backstage', [Builder::K_SCHEME => 'https']); + $builder->get('users', 'action_users', [Builder::K_NAME => 'panel']); + $builder->endGroup(); + + $this->assertInstanceOf('Lucid\Mux\RouteInterface', $route = $builder->getCollection()->get('panel')); + $this->assertEquals('/backstage/users', $route->getPattern()); + } + + /** @test */ + public function itShouldPassGroupRequirements() + { + $builder = $this->newBuilder(); + + $builder->group('backstage', [Builder::K_HOST => 'example.com', Builder::K_SCHEME => 'https']); + $builder->addRoute('GET', 'foo', 'fooAction', [Builder::K_NAME => 'foo']); + $builder->endGroup(); + + $routes = $builder->getCollection(); + + $this->assertSame('example.com', $routes->get('foo')->getHost()); + $this->assertSame(['https'], $routes->get('foo')->getSchemes()); + } + + public function methodProvider() + { + return [ + ['get', ['/', 'action_get', [Builder::K_NAME => 'get']], ['GET']], + ['post', ['/', 'action_post', [Builder::K_NAME => 'post']], ['POST']], + ['put', ['/', 'action_put', [Builder::K_NAME => 'put']], ['PUT']], + ['head', ['/', 'action_head', [Builder::K_NAME => 'head']], ['HEAD']], + ['patch', ['/', 'action_patch', [Builder::K_NAME => 'patch']], ['PATCH']], + ['delete', ['/', 'action_delete', [Builder::K_NAME => 'delete']], ['DELETE']], + ['any', ['/', 'action_any', [Builder::K_NAME => 'any']], ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE']], + ]; + } + + protected function newBuilder() + { + return new Builder; + } + + protected function setUp() + { + if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.8.1', '<')) { + $this->markTestSkipped(sprintf('Unsupported HHVM version %s', HHVM_VERSION)); + } + } +} diff --git a/tests/RouteContextTest.php b/tests/RouteContextTest.php new file mode 100644 index 0000000..bd3a928 --- /dev/null +++ b/tests/RouteContextTest.php @@ -0,0 +1,39 @@ +assertInstanceOf('Lucid\Mux\RouteContextInterface', new Ctx('/foo', [])); + } + + /** @test */ + public function itShouldGetPath() + { + $ctx = new Ctx('/foo', ''); + $this->assertSame('/foo', $ctx->getStaticPath()); + } + + + /** @test */ + public function itShouldExtractVarsFromTokens() + { + $a = $this->mockToken('Lucid\Mux\Parser\Variable'); + $a->value = 'bar'; + $b = $this->mockToken(); + + $ctx = new Ctx('', '', $tokens = [$b, $a]); + + $this->assertSame(['bar'], $ctx->getVars()); + } + + private function mockToken($class = 'Lucid\Mux\Parser\TokenInterface') + { + return $this->getMockbuilder($class)->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/RouteTest.php b/tests/RouteTest.php new file mode 100644 index 0000000..a71fa08 --- /dev/null +++ b/tests/RouteTest.php @@ -0,0 +1,154 @@ + + * + * For full copyright and license information, please refer to the LICENSE file + * that was distributed with this package. + */ + +namespace Lucid\Mux\Tests; + +use Lucid\Mux\Route; + +/** + * @class RouteTest + * + * @package Lucid\Mux + * @version $Id$ + * @author iwyg + */ +class RouteTest extends \PHPUnit_Framework_TestCase +{ + /** @test */ + public function itShouldBeInstantiable() + { + $this->assertInstanceOf('Lucid\Mux\RouteInterface', $this->newRoute()); + } + + /** @test */ + public function itShouldGetPattern() + { + $route = new Route($pattern = '/user/{id}', 'action'); + $this->assertSame($pattern, $route->getPattern()); + } + + /** @test */ + public function itShouldBeSerializable() + { + $route = new Route( + $pattern = '/user/{id}', + $handler = 'action', + $methods = ['DELETE'], + $host = 'example.com', + $defaults = ['id' => 12], + $constraints = ['id' => '\d+'], + $schemes = ['https'] + ); + + $newRoute = unserialize(serialize($route)); + + $this->assertSame($pattern, $newRoute->getPattern()); + $this->assertSame($handler, $newRoute->getHandler()); + $this->assertSame($methods, $newRoute->getMethods()); + $this->assertSame($host, $newRoute->getHost()); + $this->assertSame($defaults, $newRoute->getDefaults()); + $this->assertSame($constraints, $newRoute->getConstraints()); + $this->assertSame($schemes, $newRoute->getSchemes()); + } + + /** @test */ + public function itShouldThrowIfSerializedAndAnderIsClosure() + { + $route = new Route('/', function () { + }); + + try { + serialize($route); + } catch (\LogicException $e) { + $this->assertEquals('Cannot serialize handler.', $e->getMessage()); + } + } + + /** @test */ + public function itShouldGetMethods() + { + $route = new Route('/', 'action', ['POST']); + + $this->assertTrue($route->hasMethod('post')); + + $route = new Route('/', 'action', $methods = ['get', 'head']); + + $this->assertTrue($route->hasMethod('get')); + $this->assertTrue($route->hasMethod('head')); + + $this->assertSame(['GET', 'HEAD'], $route->getMethods()); + } + + /** @test */ + public function itShouldGetSchemes() + { + $route = new Route('/', 'action'); + + $this->assertTrue($route->hasScheme('http')); + $this->assertTrue($route->hasScheme('https')); + + $route = new Route('/', 'action', null, null, null, null, ['https']); + $this->assertFalse($route->hasScheme('http')); + $this->assertTrue($route->hasScheme('HTTPS')); + + $route = new Route('/', 'action', null, null, null, null, null, ['HTTP', 'https']); + + $this->assertSame(['http', 'https'], $route->getSchemes()); + } + + /** @test */ + public function itShouldGetHost() + { + $route = new Route('/', 'action'); + $this->assertNull($route->getHost()); + + $route = new Route('/', 'action', null, $host = 'example.com'); + $this->assertSame($host, $route->getHost()); + } + + /** @test */ + public function itShouldGetDefaults() + { + $route = new Route('/', 'action', null, null, $def = ['foo' => 'bar']); + + $this->assertSame($def, $route->getDefaults()); + + $this->assertSame('bar', $route->getDefault('foo')); + } + + /** @test */ + public function itShouldGetConstraints() + { + $route = new Route('/', 'action', null, null, null, $const = ['foo' => 'bar']); + + $this->assertSame($const, $route->getConstraints()); + + $this->assertSame('bar', $route->getConstraint('foo')); + } + + /** @test */ + public function itShouldCallParser() + { + $route = $this->getMock(Route::class, ['getParserFunc'], ['/', 'action']); + $route->method('getParserFunc')->willReturnCallback(function () { + return function () { + return 'RouteContext'; + }; + }); + + $this->assertSame('RouteContext', $route->getContext()); + } + + private function newRoute($path = '/', $handler = 'action') + { + return new Route($path, $handler); + } +} diff --git a/tests/RouterTest.php b/tests/RouterTest.php new file mode 100644 index 0000000..3a2ab5f --- /dev/null +++ b/tests/RouterTest.php @@ -0,0 +1,179 @@ +assertInstanceOf('Lucid\Mux\RouterInterface', new Router($this->mockRoutes())); + } + + /** @test */ + public function itShouldGetUrlGenerator() + { + $router = new Router($this->mockRoutes(), null, null, null, $url = $this->mockUrl()); + + $url->expects($this->once())->method('generate')->with('foo', [], null, Url::RELATIVE_PATH); + $router->getUrl('foo'); + } + + /** @test */ + public function itShouldDispatchRoute() + { + $route = $this->mockRoute(); + $route->method('getMethods')->willReturn(['GET']); + $route->method('getSchemes')->willReturn(['http']); + + $router = new Router( + $routes = new Routes(['foo' => $route]), + null, + $dispatcher = $this->mockDispatcher(), + null, + $url = $this->mockUrl() + ); + + $url->expects($this->once())->method('generate')->willReturnCallback(function ($name) { + if ('foo' !== $name) { + $this->fail(); + } + + return '/foo/bar'; + }); + + $dispatcher->expects($this->once()) + ->method('dispatch')->willReturnCallback(function ($context) use ($router, $route) { + if ('/foo/bar' !== $context->getPath()) { + $this->fail(); + } + + /* + * @todo iwyg ; Fr 22 Jan 20:42:04 2016 --> + * create a explicit testcase + */ + $this->assertSame('foo', $router->getFirstRouteName()); + $this->assertSame('foo', $router->getCurrentRouteName()); + + return 'ok'; + }); + + $this->assertSame('ok', $router->route('foo')); + } + + /** @test */ + public function itShouldDispatchRequest() + { + $req = new Request('/'); + $router = new Router( + $routes = $this->mockRoutes(), + $matcher = $this->mockMatcher(), + $dispatcher = $this->mockDispatcher() + ); + + $dispatcher->expects($this->once())->method('dispatch')->with()->willReturnCallback(function () { + return 'ok'; + }); + + $matcher->expects($this->once())->method('matchRequest')->with($req, $routes) + ->willReturn($match = $this->mockMatch(true)); + + $match->method('getRequest')->willReturn($req); + + $this->assertSame('ok', $router->dispatch($req)); + } + + /** @test */ + public function itMapResponse() + { + $req = new Request('/'); + $router = new Router( + $routes = $this->mockRoutes(), + $matcher = $this->mockMatcher(), + $dispatcher = $this->mockDispatcher(), + $mapper = $this->mockMapper() + ); + + $dispatcher->expects($this->once())->method('dispatch')->with()->willReturnCallback(function () { + return 'ok'; + }); + + $matcher->expects($this->once())->method('matchRequest')->with($req, $routes) + ->willReturn($match = $this->mockMatch(true)); + + $match->method('getRequest')->willReturn($req); + + $mapper->expects($this->once())->method('mapResponse')->with('ok')->willReturn('ko'); + + $this->assertSame('ko', $router->dispatch($req)); + } + + /** @test */ + public function itShouldThrowOnNoneMatchingRequest() + { + $req = new Request('/foo'); + $router = new Router($routes = $this->mockRoutes(), $matcher = $this->mockMatcher()); + + $matcher->expects($this->once())->method('matchRequest')->with($req, $routes) + ->willReturn($this->mockMatch(false)); + + try { + $router->dispatch($req); + } catch (MatchException $e) { + $this->assertEquals('No route found for requested resource "/foo".', $e->getMessage()); + } + } + + private function mockMatch($matches = true) + { + $m = $this->getMockbuilder('Lucid\Mux\Matcher\Context') + ->disableOriginalConstructor()->getMock(); + + $m->method('isMatch')->willReturn($matches); + + return $m; + } + + private function mockMatcher() + { + return $this->getMockbuilder('Lucid\Mux\Matcher\RequestMatcherInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockRoutes() + { + return $this->getMockbuilder('Lucid\Mux\RouteCollectionInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockRoute() + { + return $this->getMockbuilder('Lucid\Mux\RouteInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockMapper() + { + return $this->getMockbuilder('Lucid\Mux\Request\ResponseMapperInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockDispatcher() + { + return $this->getMockbuilder('Lucid\Mux\Handler\DispatcherInterface') + ->disableOriginalConstructor()->getMock(); + } + + private function mockUrl() + { + return $this->getMockbuilder('Lucid\Mux\Request\UrlGenerator') + ->disableOriginalConstructor()->getMock(); + } +} diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php new file mode 100644 index 0000000..38d4e9f --- /dev/null +++ b/tests/RoutesTest.php @@ -0,0 +1,120 @@ +newRoutes(); + $this->assertInstanceof('Lucid\Mux\RouteCollectionInterface', $routes); + } + + /** @test */ + public function routesShouldBeAddable() + { + $routes = $this->newRoutes(); + $routes->add('foo', $r = $this->mockRoute()); + + $this->assertSame(['foo' => $r], $routes->all()); + } + + /** @test */ + public function itShouldFindARouteByName() + { + $ra = $this->mockRoute(); + $rb = $this->mockRoute(); + $routes = $this->newRoutes(['foo' => $ra, 'bar' => $rb]); + + $this->assertTrue($ra === $routes->get('foo')); + } + + /** @test */ + public function itShouldFindARoutesByMethod() + { + $ra = $this->mockRoute(['GET']); + $rb = $this->mockRoute(['POST']); + $routes = $this->newRoutes(['foo' => $ra, 'bar' => $rb]); + + $this->assertSame(['bar' => $rb], $routes->findByMethod('POST')->all()); + } + + /** @test */ + public function itShouldReturnEmptyCollection() + { + $routes = new Routes(['foo' => $this->mockRoute(['GET'], ['HTTPS'])]); + + $this->assertEmpty($routes->findByMethod('post')->all()); + $this->assertEmpty($routes->findByScheme('http')->all()); + } + + + /** @test */ + public function itShouldRemoveRoutes() + { + $ra = $this->mockRoute(['GET']); + $rb = $this->mockRoute(['POST']); + $routes = $this->newRoutes(['foo' => $ra, 'bar' => $rb]); + + $this->assertTrue($routes->has('bar')); + $routes->remove('bar'); + $this->assertFalse($routes->has('bar')); + + $routes->remove('bar'); + $this->assertTrue($routes->has('foo')); + + $routes->remove('foo'); + $this->assertFalse($routes->has('foo')); + } + + /** @test */ + public function itShouldThrowOnInvalidRouteName() + { + $routes = $this->newRoutes(); + try { + $routes->add(12, $this->mockRoute()); + } catch (\InvalidArgumentException $e) { + $this->assertEquals('Routename must be string.', $e->getMessage()); + } + } + + /** @test */ + public function itShouldFindARoutesByScheme() + { + + $ra = $this->mockRoute(['GET'], ['http']); + $rb = $this->mockRoute(['GET'], ['https', 'http']); + $routes = $this->newRoutes(['foo' => $ra, 'bar' => $rb]); + + $this->assertSame(['bar' => $rb], $routes->findByScheme('https')->all()); + } + + protected function mockRoute(array $methods = ['GET'], array $schemes = ['http', 'https']) + { + $route = $this->getMockBuilder('Lucid\Mux\RouteInterface') + ->setConstructorArgs([], ['/', 'handler', $methods]) + ->getMock(); + + + $route->method('getSchemes')->willReturn($schemes); + $route->method('getMethods')->willReturn($methods); + + $route->method('hasMethod')->willReturnCallback(function ($m) use ($methods) { + return in_array(strtoupper($m), $methods); + }); + + $route->method('hasScheme')->willReturnCallback(function ($s) use ($schemes) { + return in_array(strtoupper($s), $schemes); + }); + + return $route; + } + + protected function newRoutes($routes = []) + { + return new Routes($routes); + } +}