Template.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @deprecated
  4. */
  5. class PHPParser_Template
  6. {
  7. protected $parser;
  8. protected $template;
  9. /**
  10. * Creates a new code template from a template string.
  11. *
  12. * @param PHPParser_Parser $parser A parser instance
  13. * @param string $template The template string
  14. */
  15. public function __construct(PHPParser_Parser $parser, $template) {
  16. $this->parser = $parser;
  17. $this->template = $template;
  18. }
  19. /**
  20. * Get the statements of the template with the passed in placeholders
  21. * replaced.
  22. *
  23. * @param array $placeholders Placeholders
  24. *
  25. * @return PHPParser_Node[] Statements
  26. */
  27. public function getStmts(array $placeholders) {
  28. return $this->parser->parse(
  29. $this->getTemplateWithPlaceholdersReplaced($placeholders)
  30. );
  31. }
  32. protected function getTemplateWithPlaceholdersReplaced(array $placeholders) {
  33. if (empty($placeholders)) {
  34. return $this->template;
  35. }
  36. return strtr($this->template, $this->preparePlaceholders($placeholders));
  37. }
  38. /*
  39. * Prepare the placeholders for replacement. This means that
  40. * a) all placeholders will be surrounded with __.
  41. * b) ucfirst/lcfirst variations of the placeholders are generated.
  42. *
  43. * E.g. for an input array of ['foo' => 'bar'] the result will be
  44. * ['__foo__' => 'bar', '__Foo__' => 'Bar'].
  45. */
  46. protected function preparePlaceholders(array $placeholders) {
  47. $preparedPlaceholders = array();
  48. foreach ($placeholders as $name => $value) {
  49. $preparedPlaceholders['__' . $name . '__'] = $value;
  50. if (ctype_lower($name[0])) {
  51. $ucfirstName = ucfirst($name);
  52. if (!isset($placeholders[$ucfirstName])) {
  53. $preparedPlaceholders['__' . $ucfirstName . '__'] = ucfirst($value);
  54. }
  55. }
  56. if (ctype_upper($name[0])) {
  57. $lcfirstName = lcfirst($name);
  58. if (!isset($placeholders[$lcfirstName])) {
  59. $preparedPlaceholders['__' . $lcfirstName . '__'] = lcfirst($value);
  60. }
  61. }
  62. }
  63. return $preparedPlaceholders;
  64. }
  65. }