Symfony Dependency Injection for Input and Output classes












1















For a project that I'm working on, using Symfony which I'm quite new to, I am trying to create an object of a class that uses Dependency Injection but also needs some custom parameters.



Now let's say I have a Command:



<?php

class ServerCommand extends Command {
public function __construct(Server $server) {
$this->server = $server;
}

protected function execute(InputInterface $input, OutputInterface $output) {
...
}
}


And a Server class:



<?php
class Server {
public function __construct(MessageManager $messageManager, InputInterface $input, OutputInterface $output) {
...
}
}


Now, the Server class is injected into the Command class and the MessageManager class is injected into the Server class.



The problem that I'm having is getting the $input and $ouput variables in the Command class into the constructor of the Server class.



And to make it even more difficult, I also want the $input and $output variables accessible in the MessageManager class.



Is this possible, and if so, how do I achieve this?










share|improve this question





























    1















    For a project that I'm working on, using Symfony which I'm quite new to, I am trying to create an object of a class that uses Dependency Injection but also needs some custom parameters.



    Now let's say I have a Command:



    <?php

    class ServerCommand extends Command {
    public function __construct(Server $server) {
    $this->server = $server;
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
    ...
    }
    }


    And a Server class:



    <?php
    class Server {
    public function __construct(MessageManager $messageManager, InputInterface $input, OutputInterface $output) {
    ...
    }
    }


    Now, the Server class is injected into the Command class and the MessageManager class is injected into the Server class.



    The problem that I'm having is getting the $input and $ouput variables in the Command class into the constructor of the Server class.



    And to make it even more difficult, I also want the $input and $output variables accessible in the MessageManager class.



    Is this possible, and if so, how do I achieve this?










    share|improve this question



























      1












      1








      1








      For a project that I'm working on, using Symfony which I'm quite new to, I am trying to create an object of a class that uses Dependency Injection but also needs some custom parameters.



      Now let's say I have a Command:



      <?php

      class ServerCommand extends Command {
      public function __construct(Server $server) {
      $this->server = $server;
      }

      protected function execute(InputInterface $input, OutputInterface $output) {
      ...
      }
      }


      And a Server class:



      <?php
      class Server {
      public function __construct(MessageManager $messageManager, InputInterface $input, OutputInterface $output) {
      ...
      }
      }


      Now, the Server class is injected into the Command class and the MessageManager class is injected into the Server class.



      The problem that I'm having is getting the $input and $ouput variables in the Command class into the constructor of the Server class.



      And to make it even more difficult, I also want the $input and $output variables accessible in the MessageManager class.



      Is this possible, and if so, how do I achieve this?










      share|improve this question
















      For a project that I'm working on, using Symfony which I'm quite new to, I am trying to create an object of a class that uses Dependency Injection but also needs some custom parameters.



      Now let's say I have a Command:



      <?php

      class ServerCommand extends Command {
      public function __construct(Server $server) {
      $this->server = $server;
      }

      protected function execute(InputInterface $input, OutputInterface $output) {
      ...
      }
      }


      And a Server class:



      <?php
      class Server {
      public function __construct(MessageManager $messageManager, InputInterface $input, OutputInterface $output) {
      ...
      }
      }


      Now, the Server class is injected into the Command class and the MessageManager class is injected into the Server class.



      The problem that I'm having is getting the $input and $ouput variables in the Command class into the constructor of the Server class.



      And to make it even more difficult, I also want the $input and $output variables accessible in the MessageManager class.



      Is this possible, and if so, how do I achieve this?







      symfony






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 14:16









      Tomáš Votruba

      9,66953862




      9,66953862










      asked Nov 22 '18 at 21:59









      AndriesAndries

      162




      162
























          2 Answers
          2






          active

          oldest

          votes


















          1














          EDIT: SymfonyStyle actually only uses Input, but doesn't allow to access it. What exactly do you need Input for? You should use only variables it provides outside the Command.





          So, basically you need Input and Output as a service?



          The class that combines them is called SymfonyStyle and it was introduced in in Symfony 2.8 with nice blog post.



          There are many ways to get input/output to SymfonyStyle, but I'll show you the most straighforward one. I use it in Symplify packages and Rector for over 3 years and it is very reliable.



          <?php declare(strict_types=1);

          namespace AppConsole;

          use SymfonyComponentConsoleInputArgvInput;
          use SymfonyComponentConsoleOutputConsoleOutput;
          use SymfonyComponentConsoleStyleSymfonyStyle;

          final class SymfonyStyleFactory
          {
          public function create(): SymfonyStyle
          {
          $input = new ArgvInput();
          $output = new ConsoleOutput();

          return new SymfonyStyle($input, $output);
          }
          }


          Then register this factory as a sevice:



          # app/config/services.yaml
          services:
          AppConsoleSymfonyStyleFactory: ~

          SymfonyComponentConsoleStyleSymfonyStyle:
          factory: ['@AppConsoleSymfonyStyleFactory', 'create']


          Then just require SymfonyStyle in any service you need it in and use it:



          <?php declare(strict_types=1); 

          class MessageManager
          {
          /**
          * @var SymfonyStyle
          */
          private $symfonyStyle;

          public function __construct(SymfonyStyle $symfonyStyle)
          {
          $this->symfonStyle = $symfonyStyle;
          }

          public function run()
          {
          // some code
          $this->symfonyStyle->writeln('It works!');
          // some code
          }
          }





          share|improve this answer





















          • 1





            That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

            – Andries
            Nov 23 '18 at 15:57



















          0














          The data that is in the concrete instance of the InputInterface is only created when the command is run, the same for Output. These would therefore be parameters that are passed into a function, from the execute() method, or another method that is eventually called. Similarly, they are parameters that can be given to a Server class method (and likely from there, to a MessageManager method).



          They hold data, but they are not a service.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438417%2fsymfony-dependency-injection-for-input-and-output-classes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            EDIT: SymfonyStyle actually only uses Input, but doesn't allow to access it. What exactly do you need Input for? You should use only variables it provides outside the Command.





            So, basically you need Input and Output as a service?



            The class that combines them is called SymfonyStyle and it was introduced in in Symfony 2.8 with nice blog post.



            There are many ways to get input/output to SymfonyStyle, but I'll show you the most straighforward one. I use it in Symplify packages and Rector for over 3 years and it is very reliable.



            <?php declare(strict_types=1);

            namespace AppConsole;

            use SymfonyComponentConsoleInputArgvInput;
            use SymfonyComponentConsoleOutputConsoleOutput;
            use SymfonyComponentConsoleStyleSymfonyStyle;

            final class SymfonyStyleFactory
            {
            public function create(): SymfonyStyle
            {
            $input = new ArgvInput();
            $output = new ConsoleOutput();

            return new SymfonyStyle($input, $output);
            }
            }


            Then register this factory as a sevice:



            # app/config/services.yaml
            services:
            AppConsoleSymfonyStyleFactory: ~

            SymfonyComponentConsoleStyleSymfonyStyle:
            factory: ['@AppConsoleSymfonyStyleFactory', 'create']


            Then just require SymfonyStyle in any service you need it in and use it:



            <?php declare(strict_types=1); 

            class MessageManager
            {
            /**
            * @var SymfonyStyle
            */
            private $symfonyStyle;

            public function __construct(SymfonyStyle $symfonyStyle)
            {
            $this->symfonStyle = $symfonyStyle;
            }

            public function run()
            {
            // some code
            $this->symfonyStyle->writeln('It works!');
            // some code
            }
            }





            share|improve this answer





















            • 1





              That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

              – Andries
              Nov 23 '18 at 15:57
















            1














            EDIT: SymfonyStyle actually only uses Input, but doesn't allow to access it. What exactly do you need Input for? You should use only variables it provides outside the Command.





            So, basically you need Input and Output as a service?



            The class that combines them is called SymfonyStyle and it was introduced in in Symfony 2.8 with nice blog post.



            There are many ways to get input/output to SymfonyStyle, but I'll show you the most straighforward one. I use it in Symplify packages and Rector for over 3 years and it is very reliable.



            <?php declare(strict_types=1);

            namespace AppConsole;

            use SymfonyComponentConsoleInputArgvInput;
            use SymfonyComponentConsoleOutputConsoleOutput;
            use SymfonyComponentConsoleStyleSymfonyStyle;

            final class SymfonyStyleFactory
            {
            public function create(): SymfonyStyle
            {
            $input = new ArgvInput();
            $output = new ConsoleOutput();

            return new SymfonyStyle($input, $output);
            }
            }


            Then register this factory as a sevice:



            # app/config/services.yaml
            services:
            AppConsoleSymfonyStyleFactory: ~

            SymfonyComponentConsoleStyleSymfonyStyle:
            factory: ['@AppConsoleSymfonyStyleFactory', 'create']


            Then just require SymfonyStyle in any service you need it in and use it:



            <?php declare(strict_types=1); 

            class MessageManager
            {
            /**
            * @var SymfonyStyle
            */
            private $symfonyStyle;

            public function __construct(SymfonyStyle $symfonyStyle)
            {
            $this->symfonStyle = $symfonyStyle;
            }

            public function run()
            {
            // some code
            $this->symfonyStyle->writeln('It works!');
            // some code
            }
            }





            share|improve this answer





















            • 1





              That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

              – Andries
              Nov 23 '18 at 15:57














            1












            1








            1







            EDIT: SymfonyStyle actually only uses Input, but doesn't allow to access it. What exactly do you need Input for? You should use only variables it provides outside the Command.





            So, basically you need Input and Output as a service?



            The class that combines them is called SymfonyStyle and it was introduced in in Symfony 2.8 with nice blog post.



            There are many ways to get input/output to SymfonyStyle, but I'll show you the most straighforward one. I use it in Symplify packages and Rector for over 3 years and it is very reliable.



            <?php declare(strict_types=1);

            namespace AppConsole;

            use SymfonyComponentConsoleInputArgvInput;
            use SymfonyComponentConsoleOutputConsoleOutput;
            use SymfonyComponentConsoleStyleSymfonyStyle;

            final class SymfonyStyleFactory
            {
            public function create(): SymfonyStyle
            {
            $input = new ArgvInput();
            $output = new ConsoleOutput();

            return new SymfonyStyle($input, $output);
            }
            }


            Then register this factory as a sevice:



            # app/config/services.yaml
            services:
            AppConsoleSymfonyStyleFactory: ~

            SymfonyComponentConsoleStyleSymfonyStyle:
            factory: ['@AppConsoleSymfonyStyleFactory', 'create']


            Then just require SymfonyStyle in any service you need it in and use it:



            <?php declare(strict_types=1); 

            class MessageManager
            {
            /**
            * @var SymfonyStyle
            */
            private $symfonyStyle;

            public function __construct(SymfonyStyle $symfonyStyle)
            {
            $this->symfonStyle = $symfonyStyle;
            }

            public function run()
            {
            // some code
            $this->symfonyStyle->writeln('It works!');
            // some code
            }
            }





            share|improve this answer















            EDIT: SymfonyStyle actually only uses Input, but doesn't allow to access it. What exactly do you need Input for? You should use only variables it provides outside the Command.





            So, basically you need Input and Output as a service?



            The class that combines them is called SymfonyStyle and it was introduced in in Symfony 2.8 with nice blog post.



            There are many ways to get input/output to SymfonyStyle, but I'll show you the most straighforward one. I use it in Symplify packages and Rector for over 3 years and it is very reliable.



            <?php declare(strict_types=1);

            namespace AppConsole;

            use SymfonyComponentConsoleInputArgvInput;
            use SymfonyComponentConsoleOutputConsoleOutput;
            use SymfonyComponentConsoleStyleSymfonyStyle;

            final class SymfonyStyleFactory
            {
            public function create(): SymfonyStyle
            {
            $input = new ArgvInput();
            $output = new ConsoleOutput();

            return new SymfonyStyle($input, $output);
            }
            }


            Then register this factory as a sevice:



            # app/config/services.yaml
            services:
            AppConsoleSymfonyStyleFactory: ~

            SymfonyComponentConsoleStyleSymfonyStyle:
            factory: ['@AppConsoleSymfonyStyleFactory', 'create']


            Then just require SymfonyStyle in any service you need it in and use it:



            <?php declare(strict_types=1); 

            class MessageManager
            {
            /**
            * @var SymfonyStyle
            */
            private $symfonyStyle;

            public function __construct(SymfonyStyle $symfonyStyle)
            {
            $this->symfonStyle = $symfonyStyle;
            }

            public function run()
            {
            // some code
            $this->symfonyStyle->writeln('It works!');
            // some code
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 0:00

























            answered Nov 23 '18 at 13:49









            Tomáš VotrubaTomáš Votruba

            9,66953862




            9,66953862








            • 1





              That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

              – Andries
              Nov 23 '18 at 15:57














            • 1





              That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

              – Andries
              Nov 23 '18 at 15:57








            1




            1





            That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

            – Andries
            Nov 23 '18 at 15:57





            That seems like what I'm trying to achieve, I'll try this when I get home. Thanks!

            – Andries
            Nov 23 '18 at 15:57













            0














            The data that is in the concrete instance of the InputInterface is only created when the command is run, the same for Output. These would therefore be parameters that are passed into a function, from the execute() method, or another method that is eventually called. Similarly, they are parameters that can be given to a Server class method (and likely from there, to a MessageManager method).



            They hold data, but they are not a service.






            share|improve this answer




























              0














              The data that is in the concrete instance of the InputInterface is only created when the command is run, the same for Output. These would therefore be parameters that are passed into a function, from the execute() method, or another method that is eventually called. Similarly, they are parameters that can be given to a Server class method (and likely from there, to a MessageManager method).



              They hold data, but they are not a service.






              share|improve this answer


























                0












                0








                0







                The data that is in the concrete instance of the InputInterface is only created when the command is run, the same for Output. These would therefore be parameters that are passed into a function, from the execute() method, or another method that is eventually called. Similarly, they are parameters that can be given to a Server class method (and likely from there, to a MessageManager method).



                They hold data, but they are not a service.






                share|improve this answer













                The data that is in the concrete instance of the InputInterface is only created when the command is run, the same for Output. These would therefore be parameters that are passed into a function, from the execute() method, or another method that is eventually called. Similarly, they are parameters that can be given to a Server class method (and likely from there, to a MessageManager method).



                They hold data, but they are not a service.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 11:45









                Alister BulmanAlister Bulman

                26k656100




                26k656100






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438417%2fsymfony-dependency-injection-for-input-and-output-classes%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Paul Cézanne

                    UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                    Angular material date-picker (MatDatepicker) auto completes the date on focus out