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

"$this->eventsManager" return null #14269

Closed
zuozhehao opened this issue Jul 31, 2019 · 13 comments
Closed

"$this->eventsManager" return null #14269

zuozhehao opened this issue Jul 31, 2019 · 13 comments
Assignees
Labels
bug A bug report status: medium Medium

Comments

@zuozhehao
Copy link

zuozhehao commented Jul 31, 2019

phalcon 4

I found that "protected event manager" would affect the magic method _get to get "eventsManager" when I did not use "setEventsManager"

* Events Manager
*
* @var \Phalcon\Events\ManagerInterface
*/
protected eventsManager;
/**
* Magic method __get
*/
public function __get(string! propertyName) -> var | null
{
var container, service;
let container = <DiInterface> this->container;
if typeof container != "object" {
let container = Di::getDefault();
if unlikely typeof container != "object" {
throw new Exception(
Exception::containerServiceNotFound("internal services")
);
}

@CameronHall
Copy link
Contributor

In which case we will need to add the __isset magic method to catch that. @niden

@niden
Copy link
Member

niden commented Jul 31, 2019

Sorry I do not understand the issue. The code is identical to v3 meaning eventsManager is protected

What are you trying to do?

@niden niden added 4.0 labels Jul 31, 2019
@niden
Copy link
Member

niden commented Jul 31, 2019

Never mind @CameronHall explained it

@CameronHall
Copy link
Contributor

CameronHall commented Aug 7, 2019

It's worth considering removing the eventsManager property from the DI altogether alongside the setEventsManager method altogether. Considering no one has noticed (until now) that it hasn't been an issue, there should be minimal impact (if any).

@CameronHall
Copy link
Contributor

@zuozhehao can you please provide a code sample?

@zuozhehao
Copy link
Author

class BaseController extends \Phalcon\Mvc\Controller
{

    public function afterExecuteRoute($dispatcher)
    {

        $this->eventsManager->fire('someEvent' ,$this);

    }

}

@CameronHall

@CameronHall
Copy link
Contributor

@zuozhehao how do you populate your Di? With FactoryDefault?

@zuozhehao
Copy link
Author

@CameronHall yes, Phalcon\Di\FactoryDefault

@CameronHall
Copy link
Contributor

Thanks @zuozhehao I'll take a look.

@CameronHall
Copy link
Contributor

CameronHall commented Aug 29, 2019

@zuozhehao I've tried reproducing this but haven't be able too. After rereading through all the code, there's no reason why this shouldn't work, the only reason it wouldn't is if the the eventsManager isn't in your DI.

One thing I have noticed specifically is that the __get method of the Di can overwrite the eventsManger of the DI itself.

if container->has(propertyName) {
let service = container->getShared(propertyName);
let this->{propertyName} = service;
return service;
}

@zuozhehao
Copy link
Author

@CameronHall
When initializing the controller, the setEventsManager was not executed,
When I used this->eventsManager, it did not go into the magic method__get

<?php

class BaseController {


    /**
     * Events Manager
     *
     * @var \Phalcon\Events\ManagerInterface
     */
    protected $eventsManager;


    /**
     * Magic method __get
     */
    public function __get($propertyName) {

        var_dump($propertyName);

    }

    /**
     * Sets the event manager
     */
    public function setEventsManager($eventsManager)
    {

        $this->eventsManager = $eventsManager;

    }

}

class IndexController extends BaseController {


    public function indexAction() {

        var_dump($this->eventsManager);

    }

}


(new IndexController)->indexAction();

@ruudboon
Copy link
Member

ruudboon commented Sep 19, 2019

@sergeyklay If you could check why this pull is failing and working in nanobox it would be great.

@niden
Copy link
Member

niden commented Oct 3, 2019

This has been addressed in #14445

@niden niden closed this as completed Oct 3, 2019
@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

5 participants