fishy-framework /libraries/Controller.php

Language PHP Lines 779
MD5 Hash 7f37ef2ec75fe07d78a96f313b6e7600 Estimated Cost $12,556 (why?)
Repository https://github.com/wilkerlucio/fishy-framework.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php

/*
 * Copyright 2008 Wilker Lucio <wilkerlucio@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

abstract class Fishy_Controller
{
	protected $_current_route;
	protected $_data;
	protected $_render;
	protected $_render_layout;
	protected $_layout;
	protected $_page_cache;
	protected $_cycle;
	protected $_cycle_it;
	protected $_params;
	protected $_block_cache_stack;
	
	public final function __construct($route = null)
	{
		$this->_data = array();
		$this->_render = true;
		$this->_render_layout = true;
		$this->_page_cache = false;
		$this->_layout = null;
		$this->_cycle = array();
		$this->_cycle_it = 0;
		$this->_params = array_merge($_GET, $_POST);
		$this->_block_cache_stack = array();
		$this->_current_route = $route;
		
		//load helpers
		foreach (glob(FISHY_HELPERS_PATH . "/*.php") as $helper) {
			require_once $helper;
		}
		
		foreach (glob(FISHY_SLICES_PATH . "/*/app/helpers/*.php") as $helper) {
			require_once $helper;
		}
		
		$this->initialize();
	}
	
	protected function initialize() {}
	
	public static function build_view_path($controller, $action)
	{
		$sufix = '/' . str_replace('_', '/', strtolower($controller)) . '/' . $action . '.php';
		
		$files = glob(FISHY_SLICES_PATH . '/*/app/views' . $sufix);
		$files[] = FISHY_VIEWS_PATH . $sufix;
		
		return $files[0];
	}
	
	protected function classname($lowercase = true)
	{
		$name = substr(get_class($this), 0, -strlen('Controller'));
		
		if ($lowercase) {
			$name = strtolower($name);
		}
		
		return $name;
	}
	
	protected function check_for_haml($path)
	{
		$haml_path = $path . ".haml";
		
		if (!file_exists($haml_path)) return $path;
		
		$new_path = FISHY_CACHE_PATH . "/haml" . substr($path, strlen(FISHY_VIEWS_PATH));
		
		if (file_exists($new_path)) {
			$current = filemtime($haml_path);
			$old = filemtime($new_path);
			
			if ($current <= $old) return $new_path;
		}
		
		require_once FISHY_VENDOR_PATH . "/haml/lib/haml.php";
		
		$haml = new Haml();
		$generated = $haml->parse(file_get_contents($haml_path));
		
		Fishy_DirectoryHelper::mkdir($new_path, true);
		
		file_put_contents($new_path, $generated);
		
		return $new_path;
	}
	
	protected function view_path($action, $controller = null)
	{
		if ($controller === null) {
			$controller = $this->classname();
		}
		
		$path = self::build_view_path($controller, $action);
		$path = $this->check_for_haml($path);
		
		return $path;
	}
	
	public function get_current_route()
	{
		return $this->_current_route;
	}
	
	/**
	 * Execute main routine of an action
	 *
	 * @param $method The action to be executed
	 * @param $args The arguments to pass to action
	 * @return void
	 */
	public function execute($method, $args = array())
	{
		$view_path = $this->view_path($method);
		
		if (!method_exists($this, $method) && !file_exists($view_path)) {
			dispatch_error(new Exception("Not found $method in " . $this->classname()), 404);
		}
		
		if (method_exists($this, $method)) {
			call_user_func_array(array($this, $method), $args);
		}
		
		if ($this->_render) {
			$this->render($method);
		}
		
		if ($this->_page_cache) {
			$data = ob_get_flush();
			
			$file = Fishy_Cache::cache(Fishy_Uri::get_querystring(), $data);
		}
	}
	
	/**
	 * Do an execution based on a route
	 *
	 * @param $route The route to be executed
	 * @return void
	 */
	public static function run($route) {
		global $CONTROLLER;
		
		$controller_name = Fishy_StringHelper::camelize($route['controller']) . 'Controller';
		$method = strtolower($route['action']);
		
		if (!class_exists($controller_name)) {
			dispatch_error(new Exception("Controller {$controller_name} doesn't exists"), 404);
		}
		
		$controller = new $controller_name($route);
		$controller->_current_route = $route;
		$controller->_params = array_merge($controller->_params, $route['params']);
		
		$CONTROLLER = $controller;
		
		$controller->execute($method);
	}
	
	protected function render_options()
	{
		return array(
			'return' => false,
			'controller' => $this->classname(),
			'as' => null,
			'layout' => $this->layout_file(),
			'locals' => array()
		);
	}
	
	protected function layout_file()
	{
		if ($this->_layout) {
			return $this->_layout;
		}
		
		$layout = $this->classname();
		$current_class = $this;
		
		while (!$this->layout_path($layout)) {
			$class = new ReflectionClass($current_class);
			$class = $class->getParentClass();
			$class = $class->getName();
			
			if ($class == 'Fishy_Controller') {
				break;
			}
			
			$current_class = new $class;
			$layout = $current_class->classname();
		}
		
		return $layout;
	}
	
	protected function layout_path($layout)
	{
		$layouts = glob(FISHY_SLICES_PATH . "/*/app/views/layouts/" . $layout . '.php');
		$layouts[] = FISHY_VIEWS_PATH . '/layouts/' . $layout . '.php';
		
		$layout = $layouts[0];
		$layout = $this->check_for_haml($layout);
		
		return file_exists($layout) ? $layout : null;
	}
	
	/**
	 * Render the view of an action
	 *
	 * @action The action to be rendered
	 */
	protected function render($action, $options = array())
	{
		$options = array_merge($this->render_options(), $options);
		
		ob_start();
		
		$view_path = $this->view_path($action, $options['controller']);
		
		if (file_exists($view_path)) {
			include $view_path;
		}
		
		$output = ob_get_clean();
		
		$layout = $this->layout_path($options['layout']);
		
		if ($layout && $this->_render_layout) {
			$content = $output;
			ob_start();
			include $layout;
			$output = ob_get_clean();
		}
		
		$this->_render = false;
		
		if ($options['return']) {
			return $output;
		} else {
			echo $output;
		}
	}
	
	/**
	 * Renderize a partial template
	 *
	 * @param $partial The name o partial template
	 * @param $data The data to send to partial template
	 * @param $return If you pass true to this parameter, the output will be returned instead of printed
	 * @return mixed
	 */
	protected function render_partial($partial, $data = null, $options = array())
	{
		$options = array_merge($this->render_options(), $options);
		
		if (preg_match("/^(\w+)\/(\w+)$/", $partial, $matches)) {
			$partial = $matches[2];
			$options["controller"] = $matches[1];
		}
		
		if ($options['as'] === null) {
			$options['as'] = $partial;
		}
		
		ob_start();
		
		$view_path = $this->view_path("_$partial", $options['controller']);
		
		if (file_exists($view_path)) {
			//instance locals
			foreach ($options['locals'] as $key => $value) {
				$$key = $value;
			}
			
			$$options['as'] = $data;
			
			include $view_path;
		}
		
		$output = ob_get_clean();
		
		if ($options['return']) {
			return $output;
		} else {
			echo $output;
		}
	}
	
	/**
	 * Renderize a collection into a partial template
	 *
	 * @param $partial The name o partial template
	 * @param $collection The collection to send to partial template
	 * @param $return If you pass true to this parameter, the output will be returned instead of printed
	 * @return mixed
	 */
	protected function render_collection($partial, $collection, $options = array())
	{
		$output = array();
		$options = array_merge($this->render_options(), $options);
		$return = $options['return'];
		$options['return'] = true;
		
		foreach ($collection as $data) {
			$output[] = $this->render_partial($partial, $data, $options);
		}
		
		$output = implode('', $output);
		
		if ($return) {
			return $output;
		} else {
			echo $output;
		}
	}
	
	/**
	 * Redirect current flow
	 *
	 * @param $path The path to be redirected, this path works like if you are using site_url() method
	 * @return void
	 */
	public function redirect_to($path)
	{
		header('Location: ' . $this->url_to($path));
		exit;
	}
	
	/**
	 * This function get the base URL of site
	 *
	 * @param $sulfix Sulfix to be append at end of file
	 * @return string The final path
	 */
	public final function base_url($sulfix = '')
	{
		if (preg_match("/^([a-z]+):\/\//", $sulfix)) {
			return $sulfix;
		}
		
		return FISHY_BASE_URL . $sulfix;
	}
	
	/**
	 * This function get one internal url of site
	 *
	 * @param $sulfix Sulfix to be append at end of file
	 * @return string The final path
	 */
	public function site_url($sulfix = '')
	{
		return $this->base_url(FISHY_INDEX_PAGE . $sulfix);
	}
	
	/**
	 * This function get one public url of site
	 *
	 * @param $sulfix Sulfix to be append at end of file
	 * @return string The final path
	 */
	public function public_url($sulfix = '')
	{
		return $this->base_url($sulfix);
	}
	
	/**
	 * Schedule cache when page load is finished
	 *
	 * @return void
	 */
	protected function cache_output()
	{
		$this->_page_cache = true;
		
		ob_start();
	}
	
	/**
	 * 
	 */
	public function cache_block($identifier)
	{
		$this->_block_cache_stack[] = $identifier;
		
		ob_start();
	}
	
	public function cache_block_end()
	{
		$block_content = ob_get_clean();
		$identifier = array_pop($this->_block_cache_stack);
		
		$cache_path = FISHY_CACHE_PATH . "/{$identifier}.block";
		
		//check for cache
		if (file_exists($cache_path)) {
			echo file_get_contents($cache_path);
			return;
		}
		
		//generate cache
		$parsed_block = preg_replace("/<#(.*?)#>/ms", "<?\$1?>", $block_content);
		
		$tmp_path = tempnam(FISHY_TMP_PATH, "BC");
		file_put_contents($tmp_path, $parsed_block);
		
		ob_start();
		include($tmp_path);
		$cache = ob_get_clean();
		
		unlink($tmp_path);
		
		file_put_contents($cache_path, $cache);
		
		echo $cache;
	}
	
	/**
	 * Find a element at $_GET, or return a default value
	 *
	 * @param string $propertie The name of propertie
	 * @param mixed $default The default value
	 * @return mixed
	 */
	public function gp($propertie, $default = null)
	{
		return isset($_GET[$propertie]) ? $_GET[$propertie] : $default;
	}
	
	/**
	 * Find a element at $_POST, or return a default value
	 *
	 * @param string $propertie The name of propertie
	 * @param mixed $default The default value
	 * @return mixed
	 */
	public function pp($propertie, $default = null)
	{
		return isset($_POST[$propertie]) ? $_POST[$propertie] : $default;
	}
	
	/**
	 * Find a element at $_SESSION, or return a default value
	 *
	 * @param string $propertie The name of propertie
	 * @param mixed $default The default value
	 * @return mixed
	 */
	public function sp($propertie, $default = null)
	{
		return isset($_SESSION[$propertie]) ? $_SESSION[$propertie] : $default;
	}
	
	public function cycle()
	{
		$args = func_get_args();
		
		if ($args !== $this->_cycle) {
			$this->_cycle = $args;
			$this->_cycle_it = 0;
		} else {
			$this->_cycle_it++;
			
			if ($this->_cycle_it >= count($this->_cycle)) {
				$this->_cycle_it = 0;
			}
		}
		
		return $this->_cycle[$this->_cycle_it];
	}
	
	public function image_cache($model, $field, $id = null, $configuration = array(), $user_vars = array())
	{
		$configuration = array_merge(array(
			'width' => 100,
			'height' => 100,
			'mode' => 1,
			'format' => 'jpg',
			'path_format' => '#cache/#model/#field/#id.#width.#height.#mode.#format',
			'default' => ''
		), $configuration);
		
		$vars = array_merge(array(
			'cache' => FISHY_CACHE_PATH,
			'model' => $model,
			'field' => $field,
			'id' => $id,
			'width' => $configuration['width'],
			'height' => $configuration['height'],
			'mode' => $configuration['mode'],
			'format' => $configuration['format']
		), $user_vars);
		
		$path = Fishy_StringHelper::simple_template($configuration['path_format'], $vars);
		
		$object = is_a($model, 'ActiveRecord') ? $model : ActiveRecord::model($model)->find($id);
		
		if ($object->$field) {
			if (file_exists($path) && file_exists($object->$field) && filemtime($object->$field) > filemtime($path)) {
				unlink($path);
			}
			
			if (!file_exists($path)) {
				if ($object->$field) {
					Fishy_DirectoryHelper::mkdir($path, true);
					
					$image = new Fishy_Image($object->$field);
					$image->resize($configuration['width'], $configuration['height'], $configuration['mode']);
					$image->save($path);
				}
			}
		} else {
			$this->redirect_to('public/' . $configuration['default']);
		}
		
		return file_get_contents($path);
	}
	
	public function url_to($params)
	{
		global $ROUTER;
		
		$route = "";
		
		if (is_string($params) && preg_match("/^@([a-z][a-z0-9_]*)(?:\/([a-z][a-z0-9_]*))?/i", $params, $matches)) {
			$p = array();
			
			if (isset($matches[2])) {
				$p['controller'] = $matches[1];
				$p['action'] = $matches[2];
			} else {
				$p['action'] = $matches[1];
			}
			
			$params = $p;
		}
		
		if (is_array($params)) {
			$params = array_merge(array("controller" => $this->classname()), $params);
			
			if (!isset($params['action'])) {
				$params['action'] = $ROUTER->default_action();
			}
			
			$controller = $params['controller'];
			$action = $params['action'];
			
			unset($params['controller']);
			unset($params['action']);
			
			$route = $ROUTER->discovery_route($controller, $action, $params);
		} else {
			$route = $params;
		}
		
		if (!preg_match("/^[a-z]+:\/\//", $route)) {
			$route = FISHY_BASE_URL . FISHY_INDEX_PAGE . $route;
		}
		
		return $route;
	}
	
	/**
	 * Get a request param
	 *
	 * @param string $name The name of parameter
	 * @param mixed $default The default value
	 * @return mixed
	 */
	public function param($name, $default = null)
	{
		return isset($this->_params[$name]) ? $this->_params[$name] : $default;
	}
	
	/**
	 * Display a Javascript alert and then redirect user to another page
	 * Note: this method only works with client javascript enabled,
	 *       don't use it if you are not sure that your client will be
	 *       with javascript enabled
	 *
	 * @param string $message Message to display for client
	 * @param mixed $redirect_to String or Array with path to use in redirect
	 */
	public function show_message_and_redirect_to($message, $redirect_to)
	{
		$message = preg_replace('/(\r\n|\r|\n)/', '\\n', $message);
		
		$this->_render = false;
		$url = $this->url_to($redirect_to);
		
		$template_file = dirname(__FILE__) . '/../misc/show_message_and_redirect_to_template.html';
		$template_data = array("message" => $message, "url" => $url);
		
		$template = file_get_contents($template_file);
		
		echo Fishy_StringHelper::simple_template($template, $template_data);
	}
	
	/**
	 * Test if the request method is a POST request
	 *
	 * @return boolean true if is a POST request, false otherwise
	 */
	public function post_request()
	{
		return $_SERVER['REQUEST_METHOD'] == "POST";
	}
	
	/**
	 * Get a value at data store
	 *
	 * @param $propertie The name o propertie
	 * @param $default The default value (if propertie doesn't exists)
	 * @return mixed
	 */
	public function get($propertie, $default = null)
	{
		return isset($this->_data[$propertie]) ? $this->_data[$propertie] : $default;
	}
	
	public function __get($propertie)
	{
		return $this->get($propertie);
	}
	
	public function __set($propertie, $value)
	{
		$this->_data[$propertie] = $value;
	}
	
	public function __call($method, $args)
	{
		global $ROUTER;
		
		if (Fishy_StringHelper::ends_with($method, '_url')) {
			$route_name = substr($method, 0, -4);
			
			return FISHY_BASE_URL . FISHY_INDEX_PAGE . $ROUTER->named_route($route_name, @$args[0]);
		}
		
		if (Fishy_StringHelper::starts_with($method, 'redirect_to_')) {
			$route_name = substr($method, strlen('redirect_to_'));
			
			return $this->redirect_to($ROUTER->named_route($route_name, @$args[0]));
		}
		
		throw new Exception("Method $method doesn't exists");
	}
	
	/* VIEW HELPERS */
	
	public function stylesheet_tag($css, $attr = array())
	{
		if (!preg_match('/\.css$/', $css)) {
			$css .= '.css';
		}
		
		$path_sufix = "stylesheets/{$css}";
		$real_path = FISHY_PUBLIC_PATH . '/' . $path_sufix;
		$mtime = @filemtime($real_path);
		
		$path = $this->public_url("stylesheets/{$css}?{$mtime}");
		$attr = array_merge(array(
			"href" => $path,
			"media" => "screen",
			"rel" => "stylesheet", 
			"type" => "text/css"
		), $attr);
		
		$attr = $this->build_tag_attributes($attr);
		
		return "<link{$attr} />";
	}
	
	public function ie_stylesheet_tag($file)
	{
		$css = $this->stylesheet_tag($file);
		
		return "<!--[if IE]>" . $css . "<![endif]-->";
	}
	
	public function javascript_tag()
	{
		$args = func_get_args();
		$last = $args[count($args) - 1];
		
		if (is_array($last)) {
			$attr = array_pop($args);
		} else {
			$attr = array();
		}
		
		$buffer = "";
		
		foreach ($args as $js) {
			if (!preg_match('/\.js$/', $js)) {
				$js .= '.js';
			}
		
			if (preg_match("/^[a-z]+:\/\//", $js)) {
				$path = $js;
			} else {
				$path_sufix = "javascripts/{$js}";
				$real_path = FISHY_PUBLIC_PATH . '/' . $path_sufix;
				$mtime = @filemtime($real_path);
				$path = $this->public_url("javascripts/{$js}?{$mtime}");
			}
			
			$cur_attr = array_merge(array(
				"src" => $path,
				"type" => "text/javascript"
			), $attr);
		
			$cur_attr = $this->build_tag_attributes($cur_attr);
			
			$buffer .= "<script{$cur_attr}></script>\r\n";
		}
		
		return $buffer;
	}
	
	public function image_tag($url, $params = array())
	{
		$url = preg_match("/^[a-z]+:\/\//", $url) ? $url : $this->public_url("images/" . $url);
		
		$params = array_merge(array(
				"src" => $url
		), $params);
		
		$attr = $this->build_tag_attributes($params);
		$img = "<img{$attr} />";
		
		return $img;
	}
	
	public function link_to($label, $url, $params = array())
	{
		$params = array_merge(array(
			"title" => $label,
			"href" => $this->url_to($url)
		), $params);
		
		$attr = $this->build_tag_attributes($params);
		$img = "<a{$attr}>{$label}</a>";
		
		return $img;
	}
	
	private function build_tag_attributes($attributes)
	{
		$attr = "";
		
		foreach ($attributes as $key => $value) {
			$attr .= " {$key}=\"{$value}\"";
		}
		
		return $attr;
	}
}
Back to Top