title: Symfony 4.2+: Replacement for ContainerAwareCommand's getContainer->get()
body: <h2>My goal</h2>
  
  <p>In Plesk i want to run a PHP script frequently using PHP 7.2. It has to be as a PHP script and not console command (<em>see "my environment" for more details</em>). My current Symfony 4.2 based implementation works fine, but it is marked <em>deprecated</em>. </p>
  
  <p>As stated <a href="https://symfony.com/blog/new-in-symfony-4-2-important-deprecations#deprecated-containerawarecommand" rel="nofollow noreferrer">here</a>, the <code>ContainerAwareCommand</code> is marked <code>deprecated</code> in Symfony 4.2. Unfortunately, the referenced <a href="https://symfony.com/doc/current/console.html" rel="nofollow noreferrer">article</a> about how to solve this issue in the future doesn't contain information about it.</p>
  
  <h2>My environment</h2>
  
  <p>My shared webhosting (Plesk) runs with PHP 7.0 but allows scripts to run with PHP 7.2. Later is only possible, if it directly runs the PHP script and <em>not</em> as a console command. I require PHP 7.2.</p>
  
  <p>I know the <a href="https://symfony.com/doc/current/service_container/injection_types.html" rel="nofollow noreferrer">injection types</a> in Symfony. Based on my current knowledge, this issue only can be solved by using the <code>getContainer</code> approach or providing all services by hand, for instance via constructor, which would result in a code mess.</p>
  
  <h2>Current solution</h2>
  
  <h3>File: cron1.php</h3>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  // namespaces, Dotenv and gathering $env and $debug
  // ... 
  
  $kernel = new Kernel($env, $debug);
  
  $app = new Application($kernel);
  $app-&gt;add(new FillCronjobQueueCommand());
  $app-&gt;setDefaultCommand('fill_cronjob_queue');
  $app-&gt;run();
  </code></pre>
  
  <h3>File: FillCronjobQueueCommand.php</h3>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php 
  
  // ...
  
  use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Output\OutputInterface;
  
  class FillCronjobQueueCommand extends ContainerAwareCommand
  {
      protected function configure()
      {
          $this-&gt;setName('fill_cronjob_queue');
      }
  
      protected function execute(InputInterface $input, OutputInterface $output)
      {
          // using "$this-&gt;getContainer()" is deprecated since Symfony 4.2 
          $manager = $this-&gt;getContainer()-&gt;get('doctrine')-&gt;getManager();
  
          $cron_queue_repo = $manager-&gt;getRepository(CronjobQueue::class);
  
          $cronjobs = $manager-&gt;getRepository(Cronjob::class)-&gt;findAll();
  
          $logger = $this-&gt;getContainer()-&gt;get('logger');
  
          // ...
      }
  }
  
  </code></pre>