PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/symphony/lib/class.textformatter.php

https://github.com/sevenray/symphony-3
PHP | 124 lines | 81 code | 32 blank | 11 comment | 11 complexity | b76e3d6e1a69d6be5784a4286e3f780c MD5 | raw file
  1. <?php
  2. Class TextFormatterException extends Exception {}
  3. Class TextFormatterFilterIterator extends FilterIterator{
  4. public function __construct($path){
  5. parent::__construct(new DirectoryIterator($path));
  6. }
  7. public function accept(){
  8. if($this->isDir() == false && preg_match('/^.+\.php$/i', $this->getFilename())){
  9. return true;
  10. }
  11. return false;
  12. }
  13. }
  14. Class TextFormatterIterator implements Iterator{
  15. private $position;
  16. private $formatters;
  17. public function __construct(){
  18. $this->formatters = array();
  19. $this->formatters = array_merge(
  20. glob(TEXTFORMATTERS . "/*.php"),
  21. glob(EXTENSIONS . "/*/text-formatters/*.php")
  22. );
  23. /*
  24. foreach(new DirectoryIterator(EXTENSIONS) as $dir){
  25. if(!$dir->isDir() || $dir->isDot() || !is_dir($dir->getPathname() . '/text-formatters')) continue;
  26. foreach(new TextFormatterFilterIterator($dir->getPathname() . '/text-formatters') as $file){
  27. $this->formatters[] = $file->getPathname();
  28. }
  29. }
  30. */
  31. }
  32. public function length(){
  33. return count($this->formatters);
  34. }
  35. public function rewind(){
  36. $this->position = 0;
  37. }
  38. public function current(){
  39. return $this->formatters[$this->position];
  40. }
  41. public function key(){
  42. return $this->position;
  43. }
  44. public function next(){
  45. ++$this->position;
  46. }
  47. public function valid(){
  48. return isset($this->formatters[$this->position]);
  49. }
  50. }
  51. Abstract Class TextFormatter{
  52. const NONE = 'none';
  53. private static $iterator;
  54. private static $formatters;
  55. abstract public function run($string);
  56. public static function getHandleFromFilename($filename){
  57. return preg_replace('/.php$/i', NULL, $filename);
  58. }
  59. public static function load($path){
  60. if(!is_array(self::$formatters)){
  61. self::$formatters = array();
  62. }
  63. if(!file_exists($path)){
  64. throw new TextFormatterException("No such Formatter '{$path}'");
  65. }
  66. if(!isset(self::$formatters[$path])){
  67. self::$formatters[$path] = require_once($path);
  68. }
  69. return new self::$formatters[$path];
  70. }
  71. public static function loadFromHandle($handle){
  72. if(!is_array(self::$formatters)){
  73. self::$formatters = array();
  74. }
  75. if(!(self::$iterator instanceof TextFormatterIterator)){
  76. self::$iterator = new TextFormatterIterator;
  77. }
  78. self::$iterator->rewind();
  79. if(in_array($handle, array_values(self::$formatters))){
  80. $tmp = array_flip(self::$formatters);
  81. return new $tmp[$handle];
  82. }
  83. foreach(self::$iterator as $tf){
  84. if(basename($tf) == "{$handle}.php"){
  85. return self::load($tf);
  86. }
  87. }
  88. throw new TextFormatterException("No such Formatter '{$handle}'");
  89. }
  90. }