8000 FOSRestController is now a trait and not longer a class extending from the core Controller class by lsmith77 · Pull Request #1207 · FriendsOfSymfony/FOSRestBundle · GitHub
[go: up one dir, main page]

Skip to content

FOSRestController is now a trait and not longer a class extending from the core Controller class #1207

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

Merged
merged 1 commit into from
Dec 8, 2015

Conversation

lsmith77
Copy link
Member

No description provided.

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch from 41e0181 to 4aafa19 Compare November 13, 2015 14:08
@lsmith77 lsmith77 added this to the 2.0 milestone Nov 13, 2015
@GromNaN
Copy link
GromNaN commented Nov 13, 2015

👍 As PHP 5.4 is required.

@GuilhemN
Copy link
Member

I'm wondering if it wouldn't be better to suffix its name with Trait and to remove FOS: RestControllerTrait.

@lsmith77
Copy link
Member Author

yeah .. was pondering this as well

@GuilhemN
Copy link
Member

And IMO we should create a last release for the 1.* branch with bc layers to help to migrate to FOSRestBundle 2.0.

@lsmith77
Copy link
Member Author

not sure if its worth it .. I mean we have tons of open bugs/enhancement tickets .. at this point moving to 2.0 should not take all too long .. what does make sense is to maybe invest some more time into a migration guide

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch 2 times, most recently from c9a3762 to 7825c35 Compare November 13, 2015 15:10
@@ -16,6 +16,7 @@ This document will be updated to list important BC breaks and behavioral changes
* Removed ``callback_filter`` configuration option for the jsonp_handler
* ``exception_wrapper_handler`` is now the name of a service and not the name of a class
* removed all ``.class`` parameters, instead overwriting services via explicit Bundle configuration is preferred
* ControllerTrait is now a trait and not longer a class extending from the core Controller class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear, imo include the new and the old name would be clearer.

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch from 7825c35 to cd331d8 Compare November 13, 2015 15:30
*/
abstract class FOSRestController extends Controller
trait ControllerTrait
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this trait must extend the ContainerAwareTrait as it can't work without it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution would be to add an abstract method get.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well the trait falls back to using the magic get() method .. but it can work without it ..
I am not quite sure how to best approach this .. maybe provide two traits .. ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops look too fast...
If there are two traits, the problem will be the same (moreover get belongs to Controller) ...
You can maybe check if the get method exists on $this or if $this is an instance of Controller.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed it by using $this->container->get()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, I'm wondering if two traits it's not too complicated to use...
Can't we just check if $this is an instance of ContainerAwareInterface or if $this->viewHandler is defined and otherwise throw an exception?

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch 3 times, most recently from 675cdb6 to f27465f Compare November 13, 2015 17:41
/**
* Get the ViewHandler.
*
* @@param View $view
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a typo here that I need to fix

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch from f27465f to 2be0336 Compare November 14, 2015 11:54
@lsmith77
Copy link
Member Author

To fix the tests one needs to do the following, which sucks big time as it basically means the trait isn't a logical unit anymore but requires other things that I cannot express with code :-/

diff --git a/Controller/ContainerAwareControllerTrait.php b/Controller/ContainerAwareControllerTrait.php
index b03ecd2..d677c1d 100644
--- a/Controller/ContainerAwareControllerTrait.php
+++ b/Controller/ContainerAwareControllerTrait.php
@@ -12,7 +12,6 @@
 namespace FOS\RestBundle\Controller;

 use FOS\RestBundle\View\ViewHandlerInterface;
-use Symfony\Component\DependencyInjection\ContainerAwareTrait;

 /**
  * Trait for Controllers using the View functionality of FOSRestBundle.
@@ -22,7 +21,6 @@ use Symfony\Component\DependencyInjection\ContainerAwareTrait;
 trait ContainerAwareControllerTrait
 {
     use ControllerTrait;
-    use ContainerAwareTrait;

     /**
      * Get the ViewHandler.

@lsmith77
Copy link
Member Author

Note even if we would change the core ContainerAware class to use the ContainerAwareTrait we would still run into the same problem as can be seen here https://3v4l.org/l1qkE

@lsmith77
Copy link
Member Author

ok changing to use getContainer() and defining such an abstract method within ContrainerAwareControllerTrait along with the following change fixes the issue:

diff --git a/ContainerAware.php b/ContainerAware.php
index af977fe..080f8cb 100644
--- a/ContainerAware.php
+++ b/ContainerAware.php
@@ -18,18 +18,5 @@ namespace Symfony\Component\DependencyInjection;
  */
 abstract class ContainerAware implements ContainerAwareInterface
 {
-    /**
-     * @var ContainerInterface
-     */
-    protected $container;
-
-    /**
-     * Sets the Container associated with this Controller.
-     *
-     * @param ContainerInterface $container A ContainerInterface instance
-     */
-    public function setContainer(ContainerInterface $container = null)
-    {
-        $this->container = $container;
-    }
+    use ContainerAwareTrait;
 }
diff --git a/ContainerAwareTrait.php b/ContainerAwareTrait.php
index 57280aa..caf222a 100644
--- a/ContainerAwareTrait.php
+++ b/ContainerAwareTrait.php
@@ -32,4 +32,14 @@ trait ContainerAwareTrait
     {
         $this->container = $container;
     }
+
+    /**
+     * Gets the Container associated with this Controller.
+     *
+     * @return ContainerInterface
+     */
+    public function getContainer()
+    {
+        return $this->container;
+    }
 }

Note technically it is sufficient to add the getContainer() method to ContainerAware but while we are at it, we should also add such a method to ContainerAwareTrait. And we can of course choose to not use the ContrainerAwareTrait inside the ContainerAware class in all versions of Symfony that are compatible with PHP 5.3.

tl;dr: trait properties should always come with a getter method

@GuilhemN
Copy link
Member

@lsmith77
Copy link
Member Author

@Ener-Getick at the end of the day it still relies on a property that is no where defined in the trait or the contract defined by the trait. will see what happens with the ticket I opened on symfony core.

*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
trait ContainerAwareControllerTrait
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this to be a base class, ie. keep the existing name FOSRestController

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch from 76a649b to f5bbe57 Compare December 5, 2015 12:12
@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch 3 times, most recently from aee479f to 2393d31 Compare December 5, 2015 12:15
@lsmith77
Copy link
Member Author
lsmith77 commented Dec 5, 2015

ok .. keeping FOSRestController and just introducing ControllerTrait .. ready for final review.

2D7E
*
* @param ViewHandlerInterface $viewhandler
*/
protected function setViewHandler(ViewHandlerInterface $viewhandler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should be public to allow setter injection

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

@GuilhemN
Copy link
Member
GuilhemN commented Dec 7, 2015

Except my last comment, sounds good to me 👍.

@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch 2 times, most recently from 8ee4666 to 26c5bc2 Compare December 8, 2015 07:16
@lsmith77
Copy link
Member Author
lsmith77 commented Dec 8, 2015

@Ener-Getick fixed

…r controllers instead of extending ``FOSRestController``
@lsmith77 lsmith77 force-pushed the make_controller_a_trait branch from 26c5bc2 to 7970b0c Compare December 8, 2015 08:02
lsmith77 added a commit that referenced this pull request Dec 8, 2015
FOSRestController is now a trait and not longer a class extending from the core Controller class
@lsmith77 lsmith77 merged commit 78698c1 into master Dec 8, 2015
@lsmith77 lsmith77 removed the in review label Dec 8, 2015
@lsmith77 lsmith77 deleted the make_controller_a_trait branch December 8, 2015 08:13
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

Successfully merging this pull request may close these issues.

3 participants
0