cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

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

Open pantaovay opened this issue 11 years ago • 9 comments

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

pantaovay avatar Apr 23 '15 17:04 pantaovay

Please submit the source code, let test?

dreamsxin avatar Apr 24 '15 01:04 dreamsxin

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 avatar Apr 24 '15 03:04 pantaovay

@andresgutierrez

pantaovay avatar Apr 24 '15 04:04 pantaovay

Change to getActiveController to getControllerName

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

dreamsxin avatar Apr 24 '15 09:04 dreamsxin

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 avatar Apr 25 '15 01:04 pantaovay

Fixed but can be imporved!

pantaovay avatar Apr 25 '15 01:04 pantaovay

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

andresgutierrez avatar Apr 25 '15 01:04 andresgutierrez

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

dreamsxin avatar Apr 25 '15 02:04 dreamsxin

No response from OP

andresgutierrez avatar Jun 25 '15 01:06 andresgutierrez