Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Annotation actions.
- *
- * @author Katsuhiro Ogawa <katsuhiro@asial.co.jp>
- * @licence MIT Licence
- */
- class sfAnnotationActions extends sfActions
- {
- public function initialize($context, $moduleName, $actionName)
- {
- sfComponent::initialize($context, $moduleName, $actionName);
- $this->setSecurityConfiguration($this->parseSecurityAnnotations());
- }
- protected function parseSecurityAnnotations()
- {
- if (method_exists($this->context->getConfiguration(), 'getAnnotationCache')) {
- $cache = $this->context->getConfiguration()->getAnnotationCache();
- if ($cache->has('actions.'.get_class($this))) {
- $cachedAnnotations = $cache->get('actions.'.get_class($this));
- if ($cachedAnnotations) {
- return $cachedAnnotations;
- }
- }
- }
- $securityConfiguration = array();
- $class = new ReflectionObject($this);
- $securityConfiguration['all'] = $this->extractSecurityConfiguration($class->getDocComment());
- $methods = $class->getMethods();
- foreach ($methods as $method) {
- if (0 === strpos($method->getName(), 'execute')) {
- $actionName = strtolower(substr($method->getName(), 7));
- $securityConfiguration[$actionName] = $this->extractSecurityConfiguration($method->getDocComment());
- }
- }
- if (isset($cache)) {
- $cache->set('actions.'.get_class($this), $securityConfiguration);
- }
- return $securityConfiguration;
- }
- protected function extractSecurityConfiguration($comment)
- {
- $config = array();
- // @login_required([c1, c2, [c3-1, c3-2]])
- // => is_secure: true, credentials: array('c1', 'c2', array('c3', 'c4'))
- if (preg_match('/@login_required(?:\(([^\)]*)\))?/', $comment, $matches)) {
- $config['is_secure'] = true;
- if (!empty($matches[1])) {
- $config['credentials'] = self::parseCredentialsText(trim($matches[1], '[]'));
- }
- } elseif (preg_match('/@login_unrequired/', $comment)) {
- $config['is_secure'] = false;
- }
- return $config;
- }
- static public function parseCredentialsText($text, $main = true)
- {
- static $cursor;
- if ($main) {
- $cursor = 0;
- }
- $credentials = array();
- while ($cursor < strlen($text)) {
- if (preg_match('/[\s,]+/A', $text, $match, null, $cursor)) {
- $cursor += strlen($match[0]);
- } elseif (preg_match('/[\w-]+/A', $text, $match, null, $cursor)) {
- $credentials[] = $match[0];
- $cursor += strlen($match[0]);
- } elseif (preg_match('/\[/A', $text, $match, null, $cursor)) {
- $cursor += strlen($match[0]);
- $credentials[] = self::parseCredentialsText($text, false);
- } elseif (true !== $main && preg_match('/\]/A', $text, $match, null, $cursor)) {
- $cursor += strlen($match[0]);
- return $credentials;
- } else {
- throw new InvalidArgumentException(sprintf('Unable to parse credentials "%s" near "...%s..."', $text, substr($text, $cursor, 10)));
- }
- }
- return $credentials;
- }
- }
Add Comment
Please, Sign In to add comment