1
|
|
|
<?php
|
2
|
|
|
/*
|
3
|
|
|
This project is Licenced under The MIT License (MIT)
|
4
|
|
|
|
5
|
|
|
Copyright (c) 2014 Christopher Seufert
|
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
9
|
|
|
in the Software without restriction, including without limitation the rights
|
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
12
|
|
|
furnished to do so, subject to the following conditions:
|
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in
|
15
|
|
|
all copies or substantial portions of the Software.
|
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
|
|
THE SOFTWARE.
|
24
|
|
|
|
25
|
|
|
*/
|
26
|
|
|
namespace Seufert\Hamle;
|
27
|
|
|
|
28
|
|
|
use Seufert\Hamle\Exception\RunTime;
|
29
|
|
|
use Seufert\Hamle\Model;
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* HAMLE Runtime
|
33
|
|
|
*
|
34
|
|
|
* @author Chris Seufert <[email protected]>
|
35
|
|
|
*/
|
36
|
|
|
class Run {
|
37
|
|
|
/**
|
38
|
|
|
* @var Hamle Current HAMLE instance
|
39
|
|
|
*/
|
40
|
|
|
static protected $hamle;
|
41
|
|
|
static protected $hamleList = array();
|
42
|
|
|
|
43
|
|
|
/**
|
44
|
|
|
* Add a hamle instance to the stack
|
45
|
|
|
* @param Hamle $hamle Hamle Instance
|
46
|
|
|
*/
|
47
|
|
|
static function addInstance(Hamle $hamle) {
|
|
|
|
|
48
|
|
|
self::$hamleList[] = $hamle;
|
49
|
|
|
self::$hamle = $hamle;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Remove hamle Instance from the stack
|
54
|
|
|
*/
|
55
|
|
|
static function popInstance() {
|
|
|
|
|
56
|
|
|
array_pop(self::$hamleList);
|
57
|
|
|
if (self::$hamleList)
|
|
|
|
|
58
|
|
|
self::$hamle = self::$hamleList[count(self::$hamleList) - 1];
|
59
|
|
|
else
|
60
|
|
|
self::$hamle = NULL;
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* Execute a hamle String Filter
|
65
|
|
|
* @param string $name Name of Filter
|
66
|
|
|
* @param string $data Data to pass to filter
|
67
|
|
|
* @return string Fitlered data
|
68
|
|
|
*/
|
69
|
|
|
static function filter($name, $data) {
|
|
|
|
|
70
|
|
|
return strrev($data);
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Helper for hamle |include command
|
75
|
|
|
* @param string $path Path to file to include
|
76
|
|
|
* @return string HTML Code
|
77
|
|
|
*/
|
78
|
|
|
static function includeFile($path) {
|
|
|
|
|
79
|
|
|
return self::$hamle->load($path)->output();
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @param $fragment Name of fragment
|
84
|
|
|
* @internal Only for use in template system
|
85
|
|
|
* @return string String to output where |include #fragment was called
|
86
|
|
|
*/
|
87
|
|
|
static function includeFragment($fragment) {
|
|
|
|
|
88
|
|
|
return self::$hamle->setup->getFragment(self::$hamle, substr($fragment, 1));
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Called from template by $() to find a specific model
|
93
|
|
|
* @param array[] $typeTags array of tags with types as key eg ['page'=>[]] or ['product'=>['featured]]
|
94
|
|
|
* @param array $sort
|
95
|
|
|
* @param int $limit Results Limit
|
96
|
|
|
* @param int $offset Offset Results by
|
97
|
|
|
* @internal param string $sortBy Field name to sort by
|
98
|
|
|
* @return Model
|
99
|
|
|
*/
|
100
|
|
|
static function modelTypeTags($typeTags, $sort = [], $limit = 0, $offset = 0) {
|
|
|
|
|
101
|
|
|
return self::$hamle->setup->getModelTypeTags($typeTags,
|
102
|
|
|
$sort, $limit, $offset);
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* Called from template by $() to find a specific model
|
107
|
|
|
* @param string $id id to search for
|
108
|
|
|
* @param array $sort
|
109
|
|
|
* @param int $limit Limit of results
|
110
|
|
|
* @param int $offset Results Offset
|
111
|
|
|
* @throws RunTime
|
112
|
|
|
* @return Model
|
113
|
|
|
*/
|
114
|
|
|
static function modelId($id, $sort = [], $limit = 0, $offset = 0) {
|
|
|
|
|
115
|
|
|
$o = self::$hamle->setup->getModelDefault($id, $sort, $limit, $offset);
|
116
|
|
|
if (!$o instanceOf Model) throw new RunTime("Application must return instance of hamleModel");
|
117
|
|
|
return $o;
|
118
|
|
|
}
|
119
|
|
|
|
120
|
|
|
/**
|
121
|
|
|
* Called from template by $() to find a specific model
|
122
|
|
|
* @param array[] $typeId Array of types mapped to ids [type1=>[1],type2=>[2]]
|
123
|
|
|
* @param int $sortDir Sort Direction defined by hamle::SORT_*
|
|
|
|
|
124
|
|
|
* @param string $sortField Field name to sort by
|
|
|
|
|
125
|
|
|
* @param int $limit Results Limit
|
126
|
|
|
* @param int $offset Results Offset
|
127
|
|
|
* @internal param string $type type to filter by
|
128
|
|
|
* @internal param string $id id to search for
|
129
|
|
|
* @return Model
|
130
|
|
|
*/
|
131
|
|
|
static function modelTypeId($typeId, $sort = [], $limit = 0, $offset = 0) {
|
|
|
|
|
132
|
|
|
return self::$hamle->setup->getModelTypeId($typeId, $sort, $limit, $offset);
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
}
|
137
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.