Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Phalcon\Annotations\Exception' with message 'Scanning error before 'ExitExpression) .. #10155

Closed
pantaovay opened this issue Apr 23, 2015 · 9 comments

Comments

@pantaovay
Copy link

I have tried all kinds of ways but it just doesn't work.

I want to use annotations but no matter what I put in the doc block, it gives the error: Scanning error before 'ExitExpression

And I have tried php5.5 and php5.6, phalcon 1.3.3 and phalcon 1.3.4. It just doesn't work.

Actually, I used php5.5.12 and phalcon1.3.3 and annotations a couple months ago. But it just doesn't work with all the environment the same.

The following is the backstrace:

Fatal error: Uncaught exception 'Phalcon\Annotations\Exception' with message 'Scanning error before 'ExitExpression) ...' in /vagrant/data/code/phalcon/app/controllers/ErrorController.php on line 7' in /vagrant/data/code/phalcon/app/plugins/VerifyParamsPlugin.php:11 Stack trace: #0 [internal function]: Phalcon\Annotations\Reader->parse('Live\Controller...') #1 [internal function]: Phalcon\Annotations\Adapter->get(Object(Live\Controllers\ErrorController)) #2 /vagrant/data/code/phalcon/app/plugins/VerifyParamsPlugin.php(11): Phalcon\Annotations\Adapter->getMethod(Object(Live\Controllers\ErrorController), 'notFoundAction') #3 [internal function]: Live\Plugins\VerifyParamsPlugin->beforeExecuteRoute(Object(Phalcon\Events\Event), Object(Phalcon\Mvc\Dispatcher), NULL) #4 [internal function]: Phalcon\Events\Manager->fireQueue(Array, Object(Phalcon\Events\Event)) #5 [internal function]: Phalcon\Events\Manager->fire('dispatch:before...', Object(Phalcon\Mvc\Dispatcher)) #6 [internal function]: Phalcon\Dispatcher->dispatch() #7 /vag in /vagrant/data/code/phalcon/app/plugins/VerifyParamsPlugin.php on line 11

@dreamsxin
Copy link
Contributor

Please submit the source code, let test?

@pantaovay
Copy link
Author

ErrorController.php

<?php
namespace Live\Controllers;

use Live\Library\Exception;

class ErrorController extends AbstractController
{
    public function notFoundAction()
    {
        throw new Exception('10003');
    }
}

Dispatcher

$diContainer->set('dispatcher', function () {
        $eventsManager = new \Phalcon\Events\Manager();
        $eventsManager->attach('dispatch', new \Live\Plugins\VerifyParamsPlugin());

        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setEventsManager($eventsManager);
        $dispatcher->setDefaultNamespace('Live\Controllers');

        return $dispatcher;
    });

The verify plugin:

<?php
namespace Live\Plugins;

use Live\Library\Exception;
use Live\Library\Validation;

class VerifyParamsPlugin extends \Phalcon\Mvc\User\Plugin
{
    public function beforeExecuteRoute($event, $dispatcher)
    {
        $annotations = $this->annotations->getMethod($dispatcher->getActiveController(), $dispatcher->getActiveMethod());
        if (!$annotations->has('ParamsRules')) {
            return;
        }

        $annotation = $annotations->get('ParamsRules');
        $needValidationParamRules = [];

        foreach ($annotation->getArguments() as $key => $item) {
            if (isset($item['required']) && $item['required'] === true) {
                if (!$this->request->has($key) && !isset($_FILES[$key])) {
                    throw new Exception('10002', $key);
                }
            }

            if ($this->request->has($key)) {
                if ($this->request->get($key) === '') {
                    throw new Exception('10002');
                }
            }

            if ($this->request->has($key) || isset($_FILES[$key])) {
                $needValidationParamRules[$key] = $item;
            }
        }

        if (!empty($needValidationParamRules)) {
            $validation = new Validation($needValidationParamRules);
            $messages = $validation->validate(array_merge($_REQUEST, $_FILES));
            if (count($messages) != 0) {
                throw new Exception('10002');
            }
        }
    }

    public function beforeException($event, $dispatcher, $exception)
    {
        throw $exception;
    }
}

@pantaovay
Copy link
Author

@andresgutierrez

@dreamsxin
Copy link
Contributor

Change to getActiveController to getControllerName

$annotations = $this->annotations->getMethod($dispatcher->getControllerName(), $dispatcher->getActiveMethod());

@pantaovay
Copy link
Author

No. Finally I found the error. There is an error in AbstractController, but the error message tells it is the ErrorController.

    /**
     * @SuppressWarnings(PHPMD.ExitExpression) 
     */
    protected function renderJson(array $data, $statusCode = 200)
    {
        $this->response->setContentType('application/json', 'UTF-8');
        $this->response->setStatusCode($statusCode, self::$httpStatusCodeAndMessages[$statusCode]);
        $this->response->setJsonContent($data, JSON_UNESCAPED_SLASHES);
        $this->response->send();
        exit;
    }

The annotation I put is to avoid PHPMD warning, but conflicts with phalcon's annotation.

So can this be improved?

Are you using opcache or similar?

@pantaovay
Copy link
Author

Fixed but can be imporved!

@andresgutierrez
Copy link
Contributor

Does work if you use: @SuppressWarnings("PHPMD.ExitExpression") ?

@dreamsxin
Copy link
Contributor

@andresgutierrez It's right, Add double quotation can fix this question.

@andresgutierrez
Copy link
Contributor

No response from OP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants