Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
27f5c49b
Commit
27f5c49b
authored
Mar 25, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fragment cache WIP
parent
2f3cc8f1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
4 deletions
+48
-4
View.php
framework/base/View.php
+19
-0
FragmentCache.php
framework/widgets/FragmentCache.php
+29
-4
No files found.
framework/base/View.php
View file @
27f5c49b
...
...
@@ -156,6 +156,25 @@ class View extends Component
return
ob_get_clean
();
}
public
function
renderDynamic
(
$statements
)
{
if
(
!
empty
(
$this
->
cachingStack
))
{
$n
=
count
(
$this
->
_dynamicOutput
);
$placeholder
=
"<![CDATA[YDP-
$n
]]>"
;
foreach
(
$this
->
cachingStack
as
$cache
)
{
$cache
->
dynamicPlaceholders
[
$placeholder
]
=
$statements
;
}
return
$placeholder
;
}
else
{
return
$this
->
evaluateDynamicContent
(
$statements
);
}
}
public
function
evaluateDynamicContent
(
$statements
)
{
return
eval
(
$statements
);
}
/**
* Finds the view file based on the given view name.
*
...
...
framework/widgets/FragmentCache.php
View file @
27f5c49b
...
...
@@ -61,6 +61,16 @@ class FragmentCache extends Widget
* the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).
*/
public
$enabled
=
true
;
/**
* @var \yii\base\View the view object within which this widget is sued. If not set,
* the view registered with the application will be used. This is mainly used by dynamic content feature.
*/
public
$view
;
/**
* @var array
*/
public
$dynamicPlaceholders
;
/**
* Marks the start of content to be cached.
...
...
@@ -70,7 +80,11 @@ class FragmentCache extends Widget
*/
public
function
init
()
{
if
(
$this
->
view
===
null
)
{
$this
->
view
=
Yii
::
$app
->
getView
();
}
if
(
$this
->
getCachedContent
()
===
false
&&
$this
->
getCache
()
!==
null
)
{
array_push
(
$this
->
view
->
cachingStack
,
$this
);
ob_start
();
ob_implicit_flush
(
false
);
}
...
...
@@ -88,10 +102,12 @@ class FragmentCache extends Widget
echo
$content
;
}
elseif
((
$cache
=
$this
->
getCache
())
!==
false
)
{
$content
=
ob_get_clean
();
array_pop
(
$this
->
view
->
cachingStack
);
if
(
is_array
(
$this
->
dependency
))
{
$this
->
dependency
=
Yii
::
createObject
(
$this
->
dependency
);
}
$cache
->
set
(
$this
->
calculateKey
(),
$content
,
$this
->
duration
,
$this
->
dependency
);
$data
=
array
(
$content
,
$this
->
dynamicPlaceholders
);
$cache
->
set
(
$this
->
calculateKey
(),
$data
,
$this
->
duration
,
$this
->
dependency
);
echo
$content
;
}
}
...
...
@@ -108,11 +124,20 @@ class FragmentCache extends Widget
public
function
getCachedContent
()
{
if
(
$this
->
_content
===
null
)
{
$this
->
_content
=
false
;
if
((
$cache
=
$this
->
getCache
())
!==
null
)
{
$key
=
$this
->
calculateKey
();
$this
->
_content
=
$cache
->
get
(
$key
);
}
else
{
$this
->
_content
=
false
;
$data
=
$cache
->
get
(
$key
);
if
(
is_array
(
$data
)
&&
count
(
$data
)
===
2
)
{
list
(
$content
,
$placeholders
)
=
$data
;
if
(
is_array
(
$placeholders
)
&&
count
(
$placeholders
)
>
0
)
{
foreach
(
$placeholders
as
$name
=>
$statements
)
{
$placeholders
[
$name
]
=
$this
->
view
->
evaluateDynamicContent
(
$statements
);
}
$content
=
strtr
(
$content
,
$placeholders
);
}
$this
->
_content
=
$content
;
}
}
}
return
$this
->
_content
;
...
...
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