Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
UNL_GraduateBulletin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Digital Experience Group
UNL_GraduateBulletin
Commits
30360f11
Commit
30360f11
authored
10 years ago
by
Kevin Abel
Browse files
Options
Downloads
Patches
Plain Diff
Update Savvy lib from Undergrad bulletin
parent
b05bbff1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/php/Savvy.php
+274
-215
274 additions, 215 deletions
lib/php/Savvy.php
with
274 additions
and
215 deletions
lib/php/Savvy.php
+
274
−
215
View file @
30360f11
...
...
@@ -10,7 +10,7 @@
* @copyright 2010 Brett Bieber
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version SVN: $Id$
* @link http://
svn.php.net/repository/pear2/S
avvy
* @link http
s
://
github.com/saltybeagle/s
avvy
*/
/**
...
...
@@ -21,7 +21,7 @@
* @author Brett Bieber <saltybeagle@php.net>
* @copyright 2010 Brett Bieber
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://
svn.php.net/repository/pear2/S
avvy
* @link http
s
://
github.com/saltybeagle/s
avvy
*/
class
Savvy
{
...
...
@@ -39,6 +39,7 @@ class Savvy
'compiler'
=>
null
,
'filters'
=>
array
(),
'escape'
=>
null
,
'iterate_traversable'
=>
false
,
);
/**
...
...
@@ -106,7 +107,6 @@ class Savvy
//
// -----------------------------------------------------------------
/**
*
* Constructor.
...
...
@@ -141,6 +141,11 @@ class Savvy
if
(
isset
(
$config
[
'filters'
]))
{
$this
->
addFilters
(
$config
[
'filters'
]);
}
// set whether to iterate over Traversable objects
if
(
isset
(
$config
[
'iterate_traversable'
]))
{
$this
->
setIterateTraversable
(
$config
[
'iterate_traversable'
]);
}
}
/**
...
...
@@ -161,6 +166,7 @@ class Savvy
unset
(
$__name
,
$__value
);
ob_start
();
include
$file
;
return
ob_get_clean
();
}
...
...
@@ -182,6 +188,7 @@ class Savvy
unset
(
$__name
,
$__value
);
ob_start
();
include
$file
;
return
$savvy
->
applyFilters
(
ob_get_clean
());
}
...
...
@@ -203,6 +210,7 @@ class Savvy
unset
(
$__name
,
$__value
);
ob_start
();
include
$savvy
->
template
(
$file
);
return
ob_get_clean
();
}
...
...
@@ -224,6 +232,7 @@ class Savvy
unset
(
$__name
,
$__value
);
ob_start
();
include
$savvy
->
template
(
$file
);
return
$savvy
->
applyFilters
(
ob_get_clean
());
}
...
...
@@ -260,13 +269,24 @@ class Savvy
/**
* Add a global variable which will be available inside every template
*
* Inside templates, reference the global using the name passed
* <code>
* $savvy->addGlobal('formHelper', new FormHelper());
* </code>
*
* Sample template, Form.tpl.php
* <code>
* echo $formHelper->renderInput('name');
* </code>
*
* @param string $var The global variable name
* @param mixed $value The value
* @param mixed $value The value
or variable to expose globally
*
* @return void
*/
function
addGlobal
(
$name
,
$value
)
public
function
addGlobal
(
$name
,
$value
)
{
// disallow specific variable names, these are reserved variables
switch
(
$name
)
{
case
'context'
:
case
'parent'
:
...
...
@@ -276,25 +296,45 @@ class Savvy
throw
new
Savvy_BadMethodCallException
(
'Invalid global variable name'
);
}
// if output is currently escaped, make sure the global is escaped
if
(
$this
->
__config
[
'escape'
])
{
switch
(
gettype
(
$value
))
{
$value
=
$this
->
filterVar
(
$value
);
}
$this
->
globals
[
$name
]
=
$value
;
}
/**
* Filter a variable of unknown type
*
* @param mixed $var The variable to filter
*
* @return string|Savvy_ObjectProxy
*/
public
function
filterVar
(
$var
)
{
switch
(
gettype
(
$var
))
{
case
'object'
:
if
(
!
$va
lue
instanceof
Savvy_ObjectProxy
)
{
$value
=
Savvy_ObjectProxy
::
fac
tor
y
(
$va
lue
,
$this
);
if
(
$va
r
instanceof
ArrayIterator
)
{
return
new
Savvy_ObjectProxy
_ArrayItera
tor
(
$va
r
,
$this
);
}
break
;
if
(
$var
instanceof
ArrayAccess
)
{
return
new
Savvy_ObjectProxy_ArrayAccess
(
$var
,
$this
);
}
return
Savvy_ObjectProxy
::
factory
(
$var
,
$this
);
case
'string'
:
case
'int'
:
case
'int
eger
'
:
case
'double'
:
$value
=
$this
->
escape
(
$value
);
break
;
return
$this
->
escape
(
$var
);
case
'array'
:
$value
=
new
Savvy_ObjectProxy_ArrayIterator
(
$value
,
$this
);
break
;
}
return
new
Savvy_ObjectProxy_ArrayObject
(
new
\ArrayObject
(
$var
),
$this
);
}
$this
->
globals
[
$name
]
=
$va
lue
;
return
$va
r
;
}
/**
...
...
@@ -302,7 +342,7 @@ class Savvy
*
* @return array
*/
function
getGlobals
()
public
function
getGlobals
()
{
return
$this
->
globals
;
}
...
...
@@ -312,19 +352,17 @@ class Savvy
*
* @return string
*/
function
getTemplate
()
public
function
getTemplate
()
{
return
$this
->
template
;
}
// -----------------------------------------------------------------
//
// Public configuration management (getters and setters).
//
// -----------------------------------------------------------------
/**
*
* Returns a copy of the Savvy configuration parameters.
...
...
@@ -352,7 +390,6 @@ class Savvy
}
}
/**
*
* Sets a custom compiler/pre-processor callback for template sources.
...
...
@@ -384,6 +421,7 @@ class Savvy
$this
->
selected_controller
=
'filterfastcompiled'
;
break
;
}
return
;
}
if
(
!
strpos
(
$this
->
selected_controller
,
'compiled'
))
{
...
...
@@ -400,9 +438,10 @@ class Savvy
*
* @return Main
*/
function
setClassToTemplateMapper
(
Savvy_MapperInterface
$mapper
)
public
function
setClassToTemplateMapper
(
Savvy_MapperInterface
$mapper
)
{
$this
->
class_to_template
=
$mapper
;
return
$this
;
}
...
...
@@ -411,14 +450,26 @@ class Savvy
*
* @return MapperInterface
*/
function
getClassToTemplateMapper
()
public
function
getClassToTemplateMapper
()
{
if
(
!
isset
(
$this
->
class_to_template
))
{
$this
->
setClassToTemplateMapper
(
new
Savvy_ClassToTemplateMapper
());
}
return
$this
->
class_to_template
;
}
public
function
setIterateTraversable
(
$iterate
)
{
$this
->
__config
[
'iterate_traversable'
]
=
(
bool
)
$iterate
;
return
$this
;
}
public
function
getIterateTraversable
()
{
return
$this
->
__config
[
'iterate_traversable'
];
}
// -----------------------------------------------------------------
//
...
...
@@ -426,7 +477,6 @@ class Savvy
//
// -----------------------------------------------------------------
/**
*
* Clears then sets the callbacks to use when calling $this->escape().
...
...
@@ -452,10 +502,10 @@ class Savvy
public
function
setEscape
()
{
$this
->
__config
[
'escape'
]
=
@
func_get_args
();
return
$this
;
}
/**
*
* Gets the array of output-escaping callbacks.
...
...
@@ -471,7 +521,6 @@ class Savvy
return
$this
->
__config
[
'escape'
];
}
/**
* Escapes a value for output in a view script.
*
...
...
@@ -495,10 +544,10 @@ class Savvy
$var
=
call_user_func
(
$escape
,
$var
);
}
}
return
$var
;
}
// -----------------------------------------------------------------
//
// File management
...
...
@@ -510,7 +559,7 @@ class Savvy
*
* @return array
*/
function
getTemplatePath
()
public
function
getTemplatePath
()
{
return
$this
->
template_path
;
}
...
...
@@ -535,10 +584,10 @@ class Savvy
// actually add the user-specified directories
$this
->
addTemplatePath
(
$path
);
return
$this
;
}
/**
*
* Adds to the search path for templates and resources.
...
...
@@ -671,9 +720,10 @@ class Savvy
*
* @return string The template output
*/
function
render
(
$mixed
=
null
,
$template
=
null
)
public
function
render
(
$mixed
=
null
,
$template
=
null
)
{
$method
=
'render'
.
gettype
(
$mixed
);
return
$this
->
$method
(
$mixed
,
$template
);
}
...
...
@@ -728,6 +778,7 @@ class Savvy
if
(
!
$this
->
__config
[
'filters'
])
{
return
$string
;
}
return
$this
->
applyFilters
(
$string
);
}
...
...
@@ -745,6 +796,7 @@ class Savvy
foreach
(
$array
as
$mixed
)
{
$output
.
=
$this
->
render
(
$mixed
,
$template
);
}
return
$output
;
}
...
...
@@ -766,15 +818,17 @@ class Savvy
foreach
(
$array
as
$key
=>
$element
)
{
$ret
.
=
$template
(
$key
,
$element
,
$selected
);
}
return
$ret
;
}
protected
function
render
ArrayAccess
(
ArrayAccess
$array
,
$template
=
null
)
protected
function
render
Traversable
(
Traversable
$array
,
$template
=
null
)
{
$ret
=
''
;
foreach
(
$array
as
$key
=>
$element
)
{
$ret
.
=
$this
->
render
(
$element
,
$template
);
}
return
$ret
;
}
...
...
@@ -814,10 +868,13 @@ class Savvy
$object
=
Savvy_ObjectProxy
::
factory
(
$object
,
$this
);
}
if
(
$object
instanceof
Savvy_ObjectProxy_ArrayIterator
)
{
return
$this
->
renderArrayAccess
(
$object
);
if
(
$object
instanceof
Traversable
&&
$this
->
__config
[
'iterate_traversable'
]
)
{
return
$this
->
renderTraversable
(
$object
->
getRawObject
(),
$template
);
}
}
return
$this
->
fetch
(
$object
,
$template
);
}
...
...
@@ -836,7 +893,7 @@ class Savvy
}
}
p
ublic
function
fetch
(
$mixed
,
$template
=
null
)
p
rotected
function
fetch
(
$mixed
,
$template
=
null
)
{
if
(
$template
)
{
$this
->
template
=
$template
;
...
...
@@ -858,6 +915,7 @@ class Savvy
$this
->
templateStack
[]
=
$current
;
$ret
=
call_user_func
(
array
(
$this
,
$this
->
selected_controller
.
'OutputController'
),
$current
->
context
,
$current
->
parent
,
$current
->
file
,
$this
);
array_pop
(
$this
->
templateStack
);
return
$ret
;
}
...
...
@@ -980,6 +1038,7 @@ class Savvy
foreach
(
$this
->
__config
[
'filters'
]
as
$callback
)
{
$buffer
=
call_user_func
(
$callback
,
$buffer
);
}
return
$buffer
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment