Guest User

Untitled

a guest
Jul 16th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Annotation actions.
  5. *
  6. * @author Katsuhiro Ogawa <katsuhiro@asial.co.jp>
  7. * @licence MIT Licence
  8. */
  9. class sfAnnotationActions extends sfActions
  10. {
  11. public function initialize($context, $moduleName, $actionName)
  12. {
  13. sfComponent::initialize($context, $moduleName, $actionName);
  14.  
  15. $this->setSecurityConfiguration($this->parseSecurityAnnotations());
  16. }
  17.  
  18. protected function parseSecurityAnnotations()
  19. {
  20. if (method_exists($this->context->getConfiguration(), 'getAnnotationCache')) {
  21. $cache = $this->context->getConfiguration()->getAnnotationCache();
  22. if ($cache->has('actions.'.get_class($this))) {
  23. $cachedAnnotations = $cache->get('actions.'.get_class($this));
  24. if ($cachedAnnotations) {
  25. return $cachedAnnotations;
  26. }
  27. }
  28. }
  29.  
  30. $securityConfiguration = array();
  31.  
  32. $class = new ReflectionObject($this);
  33. $securityConfiguration['all'] = $this->extractSecurityConfiguration($class->getDocComment());
  34.  
  35. $methods = $class->getMethods();
  36. foreach ($methods as $method) {
  37. if (0 === strpos($method->getName(), 'execute')) {
  38. $actionName = strtolower(substr($method->getName(), 7));
  39. $securityConfiguration[$actionName] = $this->extractSecurityConfiguration($method->getDocComment());
  40. }
  41. }
  42.  
  43. if (isset($cache)) {
  44. $cache->set('actions.'.get_class($this), $securityConfiguration);
  45. }
  46.  
  47. return $securityConfiguration;
  48. }
  49.  
  50. protected function extractSecurityConfiguration($comment)
  51. {
  52. $config = array();
  53.  
  54. // @login_required([c1, c2, [c3-1, c3-2]])
  55. // => is_secure: true, credentials: array('c1', 'c2', array('c3', 'c4'))
  56. if (preg_match('/@login_required(?:\(([^\)]*)\))?/', $comment, $matches)) {
  57. $config['is_secure'] = true;
  58.  
  59. if (!empty($matches[1])) {
  60. $config['credentials'] = self::parseCredentialsText(trim($matches[1], '[]'));
  61. }
  62. } elseif (preg_match('/@login_unrequired/', $comment)) {
  63. $config['is_secure'] = false;
  64. }
  65.  
  66. return $config;
  67. }
  68.  
  69.  
  70. static public function parseCredentialsText($text, $main = true)
  71. {
  72. static $cursor;
  73.  
  74. if ($main) {
  75. $cursor = 0;
  76. }
  77.  
  78. $credentials = array();
  79.  
  80. while ($cursor < strlen($text)) {
  81. if (preg_match('/[\s,]+/A', $text, $match, null, $cursor)) {
  82. $cursor += strlen($match[0]);
  83. } elseif (preg_match('/[\w-]+/A', $text, $match, null, $cursor)) {
  84. $credentials[] = $match[0];
  85. $cursor += strlen($match[0]);
  86. } elseif (preg_match('/\[/A', $text, $match, null, $cursor)) {
  87. $cursor += strlen($match[0]);
  88. $credentials[] = self::parseCredentialsText($text, false);
  89. } elseif (true !== $main && preg_match('/\]/A', $text, $match, null, $cursor)) {
  90. $cursor += strlen($match[0]);
  91.  
  92. return $credentials;
  93. } else {
  94. throw new InvalidArgumentException(sprintf('Unable to parse credentials "%s" near "...%s..."', $text, substr($text, $cursor, 10)));
  95. }
  96. }
  97.  
  98. return $credentials;
  99. }
  100. }
Add Comment
Please, Sign In to add comment