view.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Fuel
  4. *
  5. * Fuel is a fast, lightweight, community driven PHP5 framework.
  6. *
  7. * @package Fuel
  8. * @version 1.5
  9. * @author Fuel Development Team
  10. * @license MIT License
  11. * @copyright 2010 - 2013 Fuel Development Team
  12. * @link http://fuelphp.com
  13. */
  14. namespace Parser;
  15. class View extends \Fuel\Core\View
  16. {
  17. /**
  18. * @var array Holds the list of loaded files.
  19. */
  20. protected static $loaded_files = array();
  21. public static function _init()
  22. {
  23. \Config::load('parser', true);
  24. // Get class name
  25. $class = \Inflector::denamespace(get_called_class());
  26. if ($class !== __CLASS__)
  27. {
  28. // Include necessary files
  29. foreach ((array) \Config::get('parser.'.$class.'.include', array()) as $include)
  30. {
  31. if ( ! array_key_exists($include, static::$loaded_files))
  32. {
  33. require $include;
  34. static::$loaded_files[$include] = true;
  35. }
  36. }
  37. }
  38. }
  39. /**
  40. * Forges a new View object based on the extension
  41. *
  42. * @param string $file view filename
  43. * @param array $data view data
  44. * @param bool $auto_encode auto encode boolean, null for default
  45. * @return object a new view instance
  46. */
  47. public static function forge($file = null, $data = null, $auto_encode = null)
  48. {
  49. $class = null;
  50. if ($file !== null)
  51. {
  52. $extension = pathinfo($file, PATHINFO_EXTENSION);
  53. $class = \Config::get('parser.extensions.'.$extension, null);
  54. }
  55. if ($class === null)
  56. {
  57. $class = get_called_class();
  58. }
  59. // Only get rid of the extension if it is not an absolute file path
  60. if ($file !== null and $file[0] !== '/' and $file[1] !== ':')
  61. {
  62. $file = $extension ? preg_replace('/\.'.preg_quote($extension).'$/i', '', $file) : $file;
  63. }
  64. // Class can be an array config
  65. if (is_array($class))
  66. {
  67. $class['extension'] and $extension = $class['extension'];
  68. $class = $class['class'];
  69. }
  70. // Include necessary files
  71. foreach ((array) \Config::get('parser.'.$class.'.include', array()) as $include)
  72. {
  73. if ( ! array_key_exists($include, static::$loaded_files))
  74. {
  75. require $include;
  76. static::$loaded_files[$include] = true;
  77. }
  78. }
  79. // Instantiate the Parser class without auto-loading the view file
  80. if ($auto_encode === null)
  81. {
  82. $auto_encode = \Config::get('parser.'.$class.'.auto_encode', null);
  83. }
  84. $view = new $class(null, $data, $auto_encode);
  85. if ($file !== null)
  86. {
  87. // Set extension when given
  88. $extension and $view->extension = $extension;
  89. // Load the view file
  90. $view->set_filename($file);
  91. }
  92. return $view;
  93. }
  94. }