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
a3a784b2
Commit
a3a784b2
authored
Apr 18, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[mount] refactor mounting to not expose an __invoke method
parent
79d0dc45
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
27 deletions
+20
-27
src/Silex/Application.php
src/Silex/Application.php
+14
-17
src/Silex/LazyApplication.php
src/Silex/LazyApplication.php
+3
-6
tests/Silex/Tests/FunctionalTest.php
tests/Silex/Tests/FunctionalTest.php
+3
-4
No files found.
src/Silex/Application.php
View file @
a3a784b2
...
@@ -215,7 +215,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -215,7 +215,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{
{
$this
[
'dispatcher'
]
->
addListener
(
Events
::
onSilexError
,
function
(
GetResponseForErrorEvent
$event
)
use
(
$callback
)
{
$this
[
'dispatcher'
]
->
addListener
(
Events
::
onSilexError
,
function
(
GetResponseForErrorEvent
$event
)
use
(
$callback
)
{
$exception
=
$event
->
getException
();
$exception
=
$event
->
getException
();
$result
=
$callback
->
__invoke
(
$exception
);
$result
=
$callback
(
$exception
);
if
(
null
!==
$result
)
{
if
(
null
!==
$result
)
{
$event
->
setStringResponse
(
$result
);
$event
->
setStringResponse
(
$result
);
...
@@ -263,10 +263,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -263,10 +263,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
*/
public
function
mount
(
$prefix
,
$app
)
public
function
mount
(
$prefix
,
$app
)
{
{
$mountHandler
=
function
(
Request
$request
,
$prefix
)
use
(
$app
)
{
if
(
is_callable
(
$app
))
{
$app
=
$app
();
}
foreach
(
$app
[
'controllers'
]
->
all
()
as
$controller
)
{
$controller
->
getRoute
()
->
setPattern
(
rtrim
(
$prefix
,
'/'
)
.
$controller
->
getRoute
()
->
getPattern
());
}
return
$app
->
handle
(
$request
);
};
$prefix
=
rtrim
(
$prefix
,
'/'
);
$prefix
=
rtrim
(
$prefix
,
'/'
);
$this
$this
->
match
(
$prefix
.
'/{path}'
,
$
app
)
->
match
(
$prefix
.
'/{path}'
,
$
mountHandler
)
->
assert
(
'path'
,
'.*'
)
->
assert
(
'path'
,
'.*'
)
->
value
(
'prefix'
,
$prefix
);
->
value
(
'prefix'
,
$prefix
);
}
}
...
@@ -290,21 +302,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
...
@@ -290,21 +302,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return
$this
[
'kernel'
]
->
handle
(
$request
,
$type
,
$catch
);
return
$this
[
'kernel'
]
->
handle
(
$request
,
$type
,
$catch
);
}
}
/**
* Handles a Request when this application has been mounted under a prefix.
*
* @param Request $request A Request instance
* @param string $path The path info (without the prefix)
*/
public
function
__invoke
(
Request
$request
,
$prefix
)
{
foreach
(
$this
[
'controllers'
]
->
all
()
as
$controller
)
{
$controller
->
getRoute
()
->
setPattern
(
rtrim
(
$prefix
,
'/'
)
.
$controller
->
getRoute
()
->
getPattern
());
}
return
$this
->
handle
(
$request
);
}
/**
/**
* Handles onCoreRequest events.
* Handles onCoreRequest events.
*/
*/
...
...
src/Silex/LazyApplication.php
View file @
a3a784b2
...
@@ -37,17 +37,14 @@ class LazyApplication
...
@@ -37,17 +37,14 @@ class LazyApplication
}
}
/**
/**
* Handles a Request when this application has been mounted under a prefix.
* Returns the application.
*
* @param Request $request A Request instance
* @param string $path The path info (without the prefix)
*/
*/
public
function
__invoke
(
Request
$request
,
$prefix
)
public
function
__invoke
()
{
{
if
(
!
$this
->
app
)
{
if
(
!
$this
->
app
)
{
$this
->
app
=
require
$this
->
appPath
;
$this
->
app
=
require
$this
->
appPath
;
}
}
return
$this
->
app
->
__invoke
(
$request
,
$prefix
)
;
return
$this
->
app
;
}
}
}
}
tests/Silex/Tests/FunctionalTest.php
View file @
a3a784b2
...
@@ -57,15 +57,14 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
...
@@ -57,15 +57,14 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
public
function
testLazyMount
()
public
function
testLazyMount
()
{
{
$mounted
=
function
(
Request
$request
,
$prefix
)
{
$mounted
Factory
=
function
(
)
{
$mounted
=
new
Application
();
$mounted
=
new
Application
();
$mounted
->
get
(
'/{name}'
,
function
(
$name
)
{
return
new
Response
(
$name
);
});
$mounted
->
get
(
'/{name}'
,
function
(
$name
)
{
return
new
Response
(
$name
);
});
return
$mounted
;
return
$mounted
(
$request
,
$prefix
);
};
};
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
mount
(
'/hello'
,
$mounted
);
$app
->
mount
(
'/hello'
,
$mounted
Factory
);
$response
=
$app
->
handle
(
Request
::
create
(
'/hello/Silex'
));
$response
=
$app
->
handle
(
Request
::
create
(
'/hello/Silex'
));
$this
->
assertEquals
(
'Silex'
,
$response
->
getContent
());
$this
->
assertEquals
(
'Silex'
,
$response
->
getContent
());
...
...
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