Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
349dcf97
Commit
349dcf97
authored
Nov 04, 2013
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved everything related to the routing to a reusable service provider
parent
e96f9be8
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
81 additions
and
53 deletions
+81
-53
src/Silex/Application.php
src/Silex/Application.php
+3
-20
src/Silex/Provider/LocaleServiceProvider.php
src/Silex/Provider/LocaleServiceProvider.php
+1
-5
src/Silex/Provider/Routing/LazyUrlMatcher.php
src/Silex/Provider/Routing/LazyUrlMatcher.php
+1
-1
src/Silex/Provider/Routing/RedirectableUrlMatcher.php
src/Silex/Provider/Routing/RedirectableUrlMatcher.php
+1
-1
src/Silex/Provider/RoutingServiceProvider.php
src/Silex/Provider/RoutingServiceProvider.php
+64
-0
src/Silex/Provider/SecurityServiceProvider.php
src/Silex/Provider/SecurityServiceProvider.php
+4
-1
tests/Silex/Tests/Application/UrlGeneratorTraitTest.php
tests/Silex/Tests/Application/UrlGeneratorTraitTest.php
+3
-11
tests/Silex/Tests/LazyUrlMatcherTest.php
tests/Silex/Tests/LazyUrlMatcherTest.php
+1
-1
tests/Silex/Tests/Provider/RoutingServiceProviderTest.php
tests/Silex/Tests/Provider/RoutingServiceProviderTest.php
+3
-13
No files found.
src/Silex/Application.php
View file @
349dcf97
...
@@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
...
@@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\GetResponseEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
use
Symfony\Component\HttpKernel\Event\PostResponseEvent
;
use
Symfony\Component\HttpKernel\EventListener\ResponseListener
;
use
Symfony\Component\HttpKernel\EventListener\ResponseListener
;
use
Symfony\Component\HttpKernel\EventListener\RouterListener
;
use
Symfony\Component\HttpKernel\Exception\HttpException
;
use
Symfony\Component\HttpKernel\Exception\HttpException
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpKernel\KernelEvents
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Request
;
...
@@ -29,7 +28,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
...
@@ -29,7 +28,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RequestContext
;
use
Silex\Api\BootableProviderInterface
;
use
Silex\Api\BootableProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Api\ControllerProviderInterface
;
use
Silex\Api\ControllerProviderInterface
;
...
@@ -37,7 +35,7 @@ use Silex\Api\ServiceProviderInterface;
...
@@ -37,7 +35,7 @@ use Silex\Api\ServiceProviderInterface;
use
Silex\EventListener\MiddlewareListener
;
use
Silex\EventListener\MiddlewareListener
;
use
Silex\EventListener\ConverterListener
;
use
Silex\EventListener\ConverterListener
;
use
Silex\EventListener\StringToResponseListener
;
use
Silex\EventListener\StringToResponseListener
;
use
Silex\Provider\Rout
er\LazyUrlMatch
er
;
use
Silex\Provider\Rout
ingServiceProvid
er
;
/**
/**
* The Silex framework class.
* The Silex framework class.
...
@@ -94,10 +92,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
...
@@ -94,10 +92,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$this
[
'dispatcher'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$this
[
'dispatcher'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$dispatcher
=
new
$app
[
'dispatcher_class'
]();
$dispatcher
=
new
$app
[
'dispatcher_class'
]();
$urlMatcher
=
new
LazyUrlMatcher
(
function
()
use
(
$app
)
{
return
$app
[
'url_matcher'
];
});
$dispatcher
->
addSubscriber
(
new
RouterListener
(
$urlMatcher
,
$app
[
'request_context'
],
$app
[
'logger'
],
$app
[
'request_stack'
]));
if
(
isset
(
$app
[
'exception_handler'
]))
{
if
(
isset
(
$app
[
'exception_handler'
]))
{
$dispatcher
->
addSubscriber
(
$app
[
'exception_handler'
]);
$dispatcher
->
addSubscriber
(
$app
[
'exception_handler'
]);
}
}
...
@@ -125,24 +119,13 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
...
@@ -125,24 +119,13 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return
new
RequestStack
();
return
new
RequestStack
();
});
});
$this
[
'request_context'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
$context
=
new
RequestContext
();
$context
->
setHttpPort
(
$app
[
'request.http_port'
]);
$context
->
setHttpsPort
(
$app
[
'request.https_port'
]);
return
$context
;
});
$this
[
'url_matcher'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
return
new
RedirectableUrlMatcher
(
$app
[
'routes'
],
$app
[
'request_context'
]);
});
$this
[
'request.http_port'
]
=
80
;
$this
[
'request.http_port'
]
=
80
;
$this
[
'request.https_port'
]
=
443
;
$this
[
'request.https_port'
]
=
443
;
$this
[
'debug'
]
=
false
;
$this
[
'debug'
]
=
false
;
$this
[
'charset'
]
=
'UTF-8'
;
$this
[
'charset'
]
=
'UTF-8'
;
$this
->
register
(
new
RoutingServiceProvider
());
foreach
(
$values
as
$key
=>
$value
)
{
foreach
(
$values
as
$key
=>
$value
)
{
$this
[
$key
]
=
$value
;
$this
[
$key
]
=
$value
;
}
}
...
...
src/Silex/Provider/LocaleServiceProvider.php
View file @
349dcf97
...
@@ -14,7 +14,7 @@ namespace Silex\Provider;
...
@@ -14,7 +14,7 @@ namespace Silex\Provider;
use
Silex\Api\ServiceProviderInterface
;
use
Silex\Api\ServiceProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Provider\Locale\LocaleListener
;
use
Silex\Provider\Locale\LocaleListener
;
use
Silex\Provider\Rout
er
\LazyUrlMatcher
;
use
Silex\Provider\Rout
ing
\LazyUrlMatcher
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
/**
/**
...
@@ -44,8 +44,4 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr
...
@@ -44,8 +44,4 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr
{
{
$dispatcher
->
addSubscriber
(
$app
[
'locale.listener'
]);
$dispatcher
->
addSubscriber
(
$app
[
'locale.listener'
]);
}
}
public
function
boot
(
Application
$app
)
{
}
}
}
src/Silex/Provider/Rout
er
/LazyUrlMatcher.php
→
src/Silex/Provider/Rout
ing
/LazyUrlMatcher.php
View file @
349dcf97
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
* file that was distributed with this source code.
*/
*/
namespace
Silex\Provider\Rout
er
;
namespace
Silex\Provider\Rout
ing
;
use
Symfony\Component\Routing\RequestContext
as
SymfonyRequestContext
;
use
Symfony\Component\Routing\RequestContext
as
SymfonyRequestContext
;
use
Symfony\Component\Routing\Matcher\UrlMatcherInterface
;
use
Symfony\Component\Routing\Matcher\UrlMatcherInterface
;
...
...
src/Silex/RedirectableUrlMatcher.php
→
src/Silex/
Provider/Routing/
RedirectableUrlMatcher.php
View file @
349dcf97
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
* file that was distributed with this source code.
*/
*/
namespace
Silex
;
namespace
Silex
\Provider\Routing
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\HttpFoundation\RedirectResponse
;
use
Symfony\Component\Routing\Matcher\RedirectableUrlMatcher
as
BaseRedirectableUrlMatcher
;
use
Symfony\Component\Routing\Matcher\RedirectableUrlMatcher
as
BaseRedirectableUrlMatcher
;
...
...
src/Silex/Provider/
UrlGenerator
ServiceProvider.php
→
src/Silex/Provider/
Routing
ServiceProvider.php
View file @
349dcf97
...
@@ -12,14 +12,20 @@
...
@@ -12,14 +12,20 @@
namespace
Silex\Provider
;
namespace
Silex\Provider
;
use
Silex\Api\ServiceProviderInterface
;
use
Silex\Api\ServiceProviderInterface
;
use
Silex\Api\EventListenerProviderInterface
;
use
Silex\Provider\Routing\RedirectableUrlMatcher
;
use
Silex\Provider\Routing\LazyUrlMatcher
;
use
Symfony\Component\Routing\Generator\UrlGenerator
;
use
Symfony\Component\Routing\Generator\UrlGenerator
;
use
Symfony\Component\Routing\RequestContext
;
use
Symfony\Component\HttpKernel\EventListener\RouterListener
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
/**
/**
* Symfony Routing component Provider
for URL generation
.
* Symfony Routing component Provider.
*
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
*/
class
UrlGeneratorServiceProvider
implements
Service
ProviderInterface
class
RoutingServiceProvider
implements
ServiceProviderInterface
,
EventListener
ProviderInterface
{
{
public
function
register
(
\Pimple
$app
)
public
function
register
(
\Pimple
$app
)
{
{
...
@@ -28,5 +34,31 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface
...
@@ -28,5 +34,31 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface
return
new
UrlGenerator
(
$app
[
'routes'
],
$app
[
'request_context'
]);
return
new
UrlGenerator
(
$app
[
'routes'
],
$app
[
'request_context'
]);
});
});
$app
[
'url_matcher'
]
=
$app
->
share
(
function
()
use
(
$app
)
{
return
new
RedirectableUrlMatcher
(
$app
[
'routes'
],
$app
[
'request_context'
]);
});
$app
[
'request_context'
]
=
$app
->
share
(
function
()
use
(
$app
)
{
$context
=
new
RequestContext
();
$context
->
setHttpPort
(
isset
(
$app
[
'request.http_port'
])
?
$app
[
'request.http_port'
]
:
80
);
$context
->
setHttpsPort
(
isset
(
$app
[
'request.https_port'
])
?
$app
[
'request.https_port'
]
:
443
);
return
$context
;
});
$app
[
'routing.listener'
]
=
$app
->
share
(
function
()
use
(
$app
)
{
$urlMatcher
=
new
LazyUrlMatcher
(
function
()
use
(
$app
)
{
return
$app
[
'url_matcher'
];
});
return
new
RouterListener
(
$urlMatcher
,
$app
[
'request_context'
],
$app
[
'logger'
],
$app
[
'request_stack'
]);
});
}
public
function
subscribe
(
\Pimple
$app
,
EventDispatcherInterface
$dispatcher
)
{
$dispatcher
->
addSubscriber
(
$app
[
'routing.listener'
]);
}
}
}
}
src/Silex/Provider/SecurityServiceProvider.php
View file @
349dcf97
...
@@ -120,7 +120,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
...
@@ -120,7 +120,10 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
$app
[
'security.channel_listener'
]
=
$app
->
share
(
function
(
$app
)
{
$app
[
'security.channel_listener'
]
=
$app
->
share
(
function
(
$app
)
{
return
new
ChannelListener
(
return
new
ChannelListener
(
$app
[
'security.access_map'
],
$app
[
'security.access_map'
],
new
RetryAuthenticationEntryPoint
(
$app
[
'request.http_port'
],
$app
[
'request.https_port'
]),
new
RetryAuthenticationEntryPoint
(
isset
(
$app
[
'request.http_port'
])
?
$app
[
'request.http_port'
]
:
80
,
isset
(
$app
[
'request.https_port'
])
?
$app
[
'request.https_port'
]
:
443
),
$app
[
'logger'
]
$app
[
'logger'
]
);
);
});
});
...
...
tests/Silex/Tests/Application/UrlGeneratorTraitTest.php
View file @
349dcf97
...
@@ -12,7 +12,7 @@
...
@@ -12,7 +12,7 @@
namespace
Silex\Tests\Application
;
namespace
Silex\Tests\Application
;
use
Silex\Application
;
use
Silex\Application
;
use
Silex\Provider\
UrlGenerator
ServiceProvider
;
use
Silex\Provider\
Routing
ServiceProvider
;
/**
/**
* UrlGeneratorTrait test cases.
* UrlGeneratorTrait test cases.
...
@@ -25,7 +25,7 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
...
@@ -25,7 +25,7 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
{
{
public
function
testUrl
()
public
function
testUrl
()
{
{
$app
=
$this
->
create
Application
();
$app
=
new
UrlGenerator
Application
();
$app
[
'url_generator'
]
=
$translator
=
$this
->
getMockBuilder
(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
)
->
disableOriginalConstructor
()
->
getMock
();
$app
[
'url_generator'
]
=
$translator
=
$this
->
getMockBuilder
(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
)
->
disableOriginalConstructor
()
->
getMock
();
$translator
->
expects
(
$this
->
once
())
->
method
(
'generate'
)
->
with
(
'foo'
,
array
(),
true
);
$translator
->
expects
(
$this
->
once
())
->
method
(
'generate'
)
->
with
(
'foo'
,
array
(),
true
);
$app
->
url
(
'foo'
);
$app
->
url
(
'foo'
);
...
@@ -33,17 +33,9 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
...
@@ -33,17 +33,9 @@ class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
public
function
testPath
()
public
function
testPath
()
{
{
$app
=
$this
->
create
Application
();
$app
=
new
UrlGenerator
Application
();
$app
[
'url_generator'
]
=
$translator
=
$this
->
getMockBuilder
(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
)
->
disableOriginalConstructor
()
->
getMock
();
$app
[
'url_generator'
]
=
$translator
=
$this
->
getMockBuilder
(
'Symfony\Component\Routing\Generator\UrlGeneratorInterface'
)
->
disableOriginalConstructor
()
->
getMock
();
$translator
->
expects
(
$this
->
once
())
->
method
(
'generate'
)
->
with
(
'foo'
,
array
(),
false
);
$translator
->
expects
(
$this
->
once
())
->
method
(
'generate'
)
->
with
(
'foo'
,
array
(),
false
);
$app
->
path
(
'foo'
);
$app
->
path
(
'foo'
);
}
}
public
function
createApplication
()
{
$app
=
new
UrlGeneratorApplication
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
return
$app
;
}
}
}
tests/Silex/Tests/LazyUrlMatcherTest.php
View file @
349dcf97
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
namespace
Silex\Tests
;
namespace
Silex\Tests
;
use
Silex\Provider\Rout
er
\LazyUrlMatcher
;
use
Silex\Provider\Rout
ing
\LazyUrlMatcher
;
/**
/**
* LazyUrlMatcher test case.
* LazyUrlMatcher test case.
...
...
tests/Silex/Tests/Provider/
UrlGenerator
ServiceProviderTest.php
→
tests/Silex/Tests/Provider/
Routing
ServiceProviderTest.php
View file @
349dcf97
...
@@ -12,22 +12,20 @@
...
@@ -12,22 +12,20 @@
namespace
Silex\Tests\Provider
;
namespace
Silex\Tests\Provider
;
use
Silex\Application
;
use
Silex\Application
;
use
Silex\Provider\
UrlGenerator
ServiceProvider
;
use
Silex\Provider\
Routing
ServiceProvider
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Request
;
/**
/**
*
UrlGenerator
Provider test cases.
*
Routing
Provider test cases.
*
*
* @author Igor Wiedler <igor@wiedler.ch>
* @author Igor Wiedler <igor@wiedler.ch>
*/
*/
class
UrlGenerator
ServiceProviderTest
extends
\PHPUnit_Framework_TestCase
class
Routing
ServiceProviderTest
extends
\PHPUnit_Framework_TestCase
{
{
public
function
testRegister
()
public
function
testRegister
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
->
bind
(
'hello'
);
...
@@ -43,8 +41,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -43,8 +41,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
->
bind
(
'hello'
);
...
@@ -62,8 +58,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -62,8 +58,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
$app
->
get
(
'/hello/{name}'
,
function
(
$name
)
{})
->
bind
(
'hello'
);
->
bind
(
'hello'
);
...
@@ -81,8 +75,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -81,8 +75,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
$app
->
get
(
'/insecure'
,
function
()
{})
$app
->
get
(
'/insecure'
,
function
()
{})
->
bind
(
'insecure_page'
)
->
bind
(
'insecure_page'
)
->
requireHttp
();
->
requireHttp
();
...
@@ -101,8 +93,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -101,8 +93,6 @@ class UrlGeneratorServiceProviderTest extends \PHPUnit_Framework_TestCase
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
register
(
new
UrlGeneratorServiceProvider
());
$app
->
get
(
'/secure'
,
function
()
{})
$app
->
get
(
'/secure'
,
function
()
{})
->
bind
(
'secure_page'
)
->
bind
(
'secure_page'
)
->
requireHttps
();
->
requireHttps
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment