1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: class RequestCycle
39: {
40: private $request;
41: private $response;
42: private $resolver;
43: private $targetStack;
44: private $maxStackSize = 10;
45:
46: public function __construct()
47: {
48: $GLOBALS['requestCycle'] = $this;
49: $this->targetStack = new \ArrayObject();
50: $this->resolver = new RequestResolverCollection();
51: $this->request = new WebRequest();
52: $this->response = new WebResponse();
53: }
54:
55: public function process()
56: {
57: $target = $this->resolver->resolve($this->request);
58:
59: if($target!=null)
60: {
61: $this->addTarget($target);
62: }
63:
64: if(count($this->targetStack)==0)
65: {
66: $this->addTarget(new PageNotFoundRequestTarget());
67: }
68:
69:
70: $iterator = $this->targetStack->getIterator();
71:
72: while($iterator->valid())
73: {
74: try
75: {
76: if(PiconApplication::get()->getProfile()->isCleanBeforeOutput())
77: {
78: ob_clean();
79: $this->response->clean();
80: }
81: $iterator->current()->respond($this->response);
82: }
83: catch(RestartRequestOnPageException $restartEx)
84: {
85: $iterator->rewind();
86: $this->targetStack->exchangeArray(array());
87: $this->addTarget(new PageRequestTarget($restartEx->getPageIdentifier()));
88: }
89: catch(\Exception $ex)
90: {
91: $iterator->rewind();
92: $this->targetStack->exchangeArray(array());
93: $this->addTarget(new ExceptionPageRequestTarget($ex));
94: }
95: $iterator->next();
96: }
97:
98: }
99:
100: public function addTarget($target)
101: {
102: if($target==null)
103: {
104: return;
105: }
106: if(!($target instanceof RequestTarget))
107: {
108: throw new \InvalidArgumentException("addTarget() expects a paramater that is an instance of RequestTarget");
109: }
110: if($this->targetStack->count()==$this->maxStackSize)
111: {
112: throw new \OverflowException(sprintf("The request target stack cannot contain more than %d elements per request", $this->maxStackSize));
113: }
114: $this->targetStack->append($target);
115: }
116:
117: public function generateUrl(RequestTarget $target)
118: {
119: return $this->resolver->generateUrl($target);
120: }
121:
122: public function getResponse()
123: {
124: return $this->response;
125: }
126:
127: public function getRequest()
128: {
129: return $this->request;
130: }
131:
132: public static function get()
133: {
134: return $GLOBALS['requestCycle'];
135: }
136:
137: public function containsTarget(Identifier $contains)
138: {
139: foreach($this->targetStack as $target)
140: {
141: $identifier = Identifier::forObject($target);
142: if($identifier->of($contains))
143: {
144: return true;
145: }
146: }
147: return false;
148: }
149: }
150:
151: ?>
152: