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
9ce84e86
Commit
9ce84e86
authored
Jan 07, 2013
by
Dave Marshall
Committed by
Fabien Potencier
Jan 10, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Service based controller resolver
parent
7df600f1
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
241 additions
and
0 deletions
+241
-0
src/Silex/Provider/ServiceControllerServiceProvider.php
src/Silex/Provider/ServiceControllerServiceProvider.php
+31
-0
src/Silex/ServiceControllerResolver.php
src/Silex/ServiceControllerResolver.php
+69
-0
tests/Silex/Tests/ServiceControllerResolverRouterTest.php
tests/Silex/Tests/ServiceControllerResolverRouterTest.php
+44
-0
tests/Silex/Tests/ServiceControllerResolverTest.php
tests/Silex/Tests/ServiceControllerResolverTest.php
+97
-0
No files found.
src/Silex/Provider/ServiceControllerServiceProvider.php
0 → 100644
View file @
9ce84e86
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex\Provider
;
use
Silex\Application
;
use
Silex\ServiceProviderInterface
;
use
Silex\ServiceControllerResolver
;
class
ServiceControllerServiceProvider
implements
ServiceProviderInterface
{
public
function
register
(
Application
$app
)
{
$app
[
'resolver'
]
=
$app
->
share
(
$app
->
extend
(
'resolver'
,
function
(
$resolver
,
$app
)
{
return
new
ServiceControllerResolver
(
$resolver
,
$app
);
}));
}
public
function
boot
(
Application
$app
)
{
// noop
}
}
src/Silex/ServiceControllerResolver.php
0 → 100644
View file @
9ce84e86
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex
;
use
Silex\Application
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpKernel\Controller\ControllerResolverInterface
;
/**
* Enables name_of_service:method_name syntax for declaring controllers.
*
* @link http://silex.sensiolabs.org/doc/cookbook/controllers_as_services.html
*/
class
ServiceControllerResolver
implements
ControllerResolverInterface
{
const
SERVICE_PATTERN
=
"/[A-Za-z0-9\._\-]+:[a-zA-Z_
\x7f
-
\xff
][a-zA-Z0-9_
\x7f
-
\xff
]*/"
;
protected
$resolver
;
protected
$app
;
/**
* Constructor.
*
* @param ControllerResolverInterface $resolver A ControllerResolverInterface instance to delegate to
* @param Application $app An Application instance
*/
public
function
__construct
(
ControllerResolverInterface
$resolver
,
Application
$app
)
{
$this
->
resolver
=
$resolver
;
$this
->
app
=
$app
;
}
/**
* {@inheritdoc}
*/
public
function
getController
(
Request
$request
)
{
$controller
=
$request
->
attributes
->
get
(
'_controller'
,
null
);
if
(
!
is_string
(
$controller
)
||
!
preg_match
(
static
::
SERVICE_PATTERN
,
$controller
))
{
return
$this
->
resolver
->
getController
(
$request
);
}
list
(
$service
,
$method
)
=
explode
(
':'
,
$controller
,
2
);
if
(
!
isset
(
$this
->
app
[
$service
]))
{
throw
new
\InvalidArgumentException
(
sprintf
(
'Service "%s" does not exist.'
,
$service
));
}
return
array
(
$this
->
app
[
$service
],
$method
);
}
/**
* {@inheritdoc}
*/
public
function
getArguments
(
Request
$request
,
$controller
)
{
return
$this
->
resolver
->
getArguments
(
$request
,
$controller
);
}
}
tests/Silex/Tests/ServiceControllerResolverRouterTest.php
0 → 100644
View file @
9ce84e86
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace
Silex\Tests
;
use
Silex\Application
;
use
Silex\Provider\ServiceControllerServiceProvider
;
use
Symfony\Component\HttpFoundation\Request
;
/**
* Router test cases, using the ServiceControllerResolver
*/
class
ServiceControllerResolverRouterTest
extends
RouterTest
{
public
function
testServiceNameControllerSyntax
()
{
$app
=
new
Application
();
$app
[
'service_name'
]
=
function
()
{
return
new
MyController
;
};
$app
->
get
(
'/bar'
,
'service_name:getBar'
);
$this
->
checkRouteResponse
(
$app
,
'/bar'
,
'bar'
);
}
protected
function
checkRouteResponse
(
$app
,
$path
,
$expectedContent
,
$method
=
'get'
,
$message
=
null
)
{
$app
->
register
(
new
ServiceControllerServiceProvider
());
$request
=
Request
::
create
(
$path
,
$method
);
$response
=
$app
->
handle
(
$request
);
$this
->
assertEquals
(
$expectedContent
,
$response
->
getContent
(),
$message
);
}
}
tests/Silex/Tests/ServiceControllerResolverTest.php
0 → 100644
View file @
9ce84e86
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex\Tests
;
use
Silex\ServiceControllerResolver
;
use
Silex\Application
;
use
Symfony\Component\HttpFoundation\Request
;
/**
* Unit tests for ServiceControllerResolver, see ServiceControllerResolverRouterTest for some
* integration tests
*/
class
ServiceControllerResolverTest
extends
\PHPUnit_Framework_Testcase
{
public
function
setup
()
{
$this
->
mockResolver
=
$this
->
getMockBuilder
(
'Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
app
=
new
Application
();
$this
->
resolver
=
new
ServiceControllerResolver
(
$this
->
mockResolver
,
$this
->
app
);
}
public
function
testShouldResolveServiceController
()
{
$this
->
app
[
'some_service'
]
=
function
()
{
return
new
\stdClass
();
};
$req
=
Request
::
create
(
'/'
);
$req
->
attributes
->
set
(
'_controller'
,
'some_service:methodName'
);
$this
->
assertEquals
(
array
(
$this
->
app
[
'some_service'
],
'methodName'
),
$this
->
resolver
->
getController
(
$req
)
);
}
public
function
testShouldDelegateNonStrings
()
{
$req
=
Request
::
create
(
'/'
);
$req
->
attributes
->
set
(
'_controller'
,
function
()
{});
$this
->
mockResolver
->
expects
(
$this
->
once
())
->
method
(
'getController'
)
->
with
(
$req
)
->
will
(
$this
->
returnValue
(
123
));
$this
->
assertEquals
(
123
,
$this
->
resolver
->
getController
(
$req
));
}
/**
* Note: This doesn't test the regex extensively, just a common use case
*/
public
function
testShouldDelegateNonMatchingSyntax
()
{
$req
=
Request
::
create
(
'/'
);
$req
->
attributes
->
set
(
'_controller'
,
'some_class::methodName'
);
$this
->
mockResolver
->
expects
(
$this
->
once
())
->
method
(
'getController'
)
->
with
(
$req
)
->
will
(
$this
->
returnValue
(
123
));
$this
->
assertEquals
(
123
,
$this
->
resolver
->
getController
(
$req
));
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Service "some_service" does not exist.
*/
public
function
testShouldThrowIfServiceIsMissing
()
{
$req
=
Request
::
create
(
'/'
);
$req
->
attributes
->
set
(
'_controller'
,
'some_service:methodName'
);
$this
->
resolver
->
getController
(
$req
);
}
public
function
testShouldDelegateGetArguments
()
{
$req
=
Request
::
create
(
'/'
);
$this
->
mockResolver
->
expects
(
$this
->
once
())
->
method
(
'getArguments'
)
->
with
(
$req
)
->
will
(
$this
->
returnValue
(
123
));
$this
->
assertEquals
(
123
,
$this
->
resolver
->
getArguments
(
$req
,
function
()
{}));
}
}
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