Created
November 11, 2020 18:45
-
-
Save beheist/df6efe759ce0601c9ef5d5cd50bafd7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use Pimcore\Console\Application; | |
use Pimcore\Controller\FrontendController; | |
use Pimcore\Model\DataObject\Collection; | |
use Pimcore\Model\DataObject\Folder; | |
use Pimcore\Model\DataObject\Product; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
abstract class AbstractCLIController extends FrontendController | |
{ | |
protected function checkAuth() | |
{ | |
if (! isset($_SERVER['PHP_AUTH_USER'])) { | |
header('WWW-Authenticate: Basic realm="Login"'); | |
} | |
$username = $this->container->getParameter('app.apiusername'); | |
$password = $this->container->getParameter('app.apipassword'); | |
if ( | |
$username === $_SERVER['PHP_AUTH_USER'] && | |
$password === $_SERVER['PHP_AUTH_PW'] | |
) { | |
return; | |
} | |
header('HTTP/1.0 401 Unauthorized'); | |
echo 'Unauthorized'; | |
exit; | |
} | |
protected function executeCommand($inputParams) | |
{ | |
$application = new Application(\Pimcore::getKernel()); | |
$application->setAutoExit(false); | |
$input = new ArrayInput($inputParams); | |
$errorCode = $application->run($input, new ConsoleOutput()); | |
if ($errorCode !== 0) { | |
return new Response('<h2>ERROR</h2><strong>An error occured during command execution</strong>. Check the application log for details.', 500); | |
} | |
return new Response('<h2>OK</h2><p>Command finished. Check the application log for details.</p>'); | |
} | |
} | |
-------------------------------------------- | |
<?php | |
namespace AppBundle\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
class PimcoreBridgeController extends AbstractCLIController | |
{ | |
public function maintenanceAction(Request $request) | |
{ | |
$this->checkAuth(); | |
return $this->executeCommand([ | |
'command' => 'pimcore:maintenance' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment