Symfony 4 Component Process pass arguments for command - command-line

Symfony Component Process
/**
* #param array $command The command to run and its arguments listed as separate entries
* #param string|null $cwd The working directory or null to use the working dir of the current PHP process
* #param array|null $env The environment variables or null to use the same environment as the current PHP process
* #param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input
* #param int|float|null $timeout The timeout in seconds or null to disable
*
* #throws LogicException When proc_open is not installed
*/
public function __construct($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)
{ .. }
I have created a command and I use the component Process in my controller.
When I try to run Process I get a ProcessFailException
The command "app:load-file foo bar foobar" failed
Exit Code: 1(General error)
.. ErrorOutput: The filename, directory name, or volume label syntax is incorrect
use Symfony\Component\Process\Process;
..
public function loadFile(KernelInterface $kernel)
{
$argument1 = 'foo';
$argument2 = 'bar';
$argument3 = 'foobar';
$rootDir = $kernel->getProjectDir();
$process = new Process(array(
$rootDir . '/bin/console app:load-file',
$argument1,
$argument2,
$argument3
));
$process->mustRun();
}
What is the correct syntax to run the command from the controller ?
/* *#param array $command The command to run and its arguments listed as separate entries
public function __construct($command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)
{ .. }

$cwd is the second parameter of the constructor.
$argument1 = 'foo';
$argument2 = 'bar';
$argument3 = 'foobar';
$rootDir = $kernel->getProjectDir();
$process = new Process(
"bin/console app:load-file $argument1 $argument2 $argument3",
$rootDir
);
try {
$process->mustRun();
} catch(ProcessFailedException $e) {
echo $e->getMessage();
}

$argument1 = 'foo';
$argument2 = 'bar';
$argument3 = 'foobar';
$rootDir = $kernel->getProjectDir();
$process = new Process(
[
$_SERVER['_'],
'bin/console'
'app:load-file',
$argument1,
$argument2,
$argument3,
],
$rootDir
);
$process->mustRun();
$_SERVER['_'] contains path to PHP interpreter executable. Use it if you have several php versions on the server

Related

Typo3 Scheduler - Command Controller: I can't execute a command that uses class repositories of my extension

Yesterday i finally got my Typo3 Scheduler working the way i want. Mostly it was the implementation of the CommandController into my extension that was a little bit "problematic".
Now i have another question regarding the Scheduler and the CommandController specifically. I have the following code. It's an Action i have implemented in the controller of a class of my extension:
public function simpleCommand()
{
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
$hosts = $apprep->findAll();
$objectManager2 = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$apprep2 = $objectManager2->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
$services = $apprep2->findAll();
foreach($hosts as $host)
{
$name = $host->getUid();
$address = $host->getIpv4();
$file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
$code_a = 'object Host "';
$code_b = '" {
import "generic-host"
address = "';
$code_c = '"
vars.notification["mail"] = {
groups = [ "icingaadmins" ]
}
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
fclose($fp);
mkdir('/etc/icinga2/conf.d/hosts/' . $name);
foreach($services as $service)
{
if($service->getHost() == $name)
{
$name = $host->getUid();
$chkcmd = 'http';
$file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
$code_a = 'object Service "';
$code_b = '" {
import "generic-service"
host_name = "';
$code_c = '"
check_command = "http"
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
fclose($fp);
}
}
exec('sudo /etc/init.d/icinga2 restart');
}
}
This is the way i implemented the code in the CommandController, but in a similar way it is also implementd in my Action in the Class Controller. Now, what this function does is simply generating a specific file i need to use in another application. The function gets the repsitory of the class "Host" and then finds all objects of it. Then it just uses the properties of each object to generate the beforementioned files. It does the same with the class "services".
In the frontend through the Action the code works perfectly and generates the files, but in the CommandController, executed automatically through the Scheduler it simply doesn't work.
Is there a missunderstanding on my side? Can't i access each class repository via a command or rather: "Are the repositories only accessable via an Action?"
Or is there another error?
I guess the reason here, is the difference between frontend and backend context.This answer here, from a different context, sums it up very nice and is worth a read
Basically, in the frontend context, you have the typoscript configuration, telling the repository where to store and find records. That is not present in the backend context. That is explained in the answer above with this code
module.tx_yourext.persistence < plugin.tx_yourext.persistence
As it didn't work with the CommandController for my specific case and i had no access to the persistence layer with with it, not even with dependency injection, i decided to not use CommandContoller tasks at all, but rather the normal Scheduler task for it. The biggest problem i encountered was to actually use the findAll() function for the repositories (it worked with findByUid(). This was because the repository expects a storage page by default. As i don’t have a module nor a plugin, my typoscript settings were ignored in that case.
So i had to set the repository to disrespect the storage page in my task. Here the code:
<?php
namespace Cjk\Icingaconfgen\Tasks;
class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask
{
public function execute() {
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
/** #var CustomerRepository $apprep */
$apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
/** #var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep->setDefaultQuerySettings($querySettings);
$hosts = $apprep->findAll();
/** #var CustomerRepository $apprep2 */
$apprep2 = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
/** #var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep2->setDefaultQuerySettings($querySettings);
$services = $apprep2->findAll();
foreach($hosts as $host)
{
$name = $host->getUid();
$address = $host->getIpv4();
$file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
$code_a = 'object Host "';
$code_b = '" {
import "generic-host"
address = "';
$code_c = '"
vars.notification["mail"] = {
groups = [ "icingaadmins" ]
}
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
fclose($fp);
mkdir('/etc/icinga2/conf.d/hosts/' . $name);
foreach($services as $service)
{
if($service->getHost() == $name)
{
$name = $host->getUid();
$chkcmd = 'http';
$file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
$code_a = 'object Service "';
$code_b = '" {
import "generic-service"
host_name = "';
$code_c = '"
check_command = "http"
}';
$fp = fopen("{$file}", 'wb');
fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
fclose($fp);
}
}
exec('sudo /etc/init.d/icinga2 restart');
}
return TRUE;
}
}

Magento 2 - change url key programmatically

Is there a way to generate URL Keys for all products and save them using a script?
I deleted all URL keys for products from database, but now I want to generate them again using a script.
// Edit: I need to do this in Magento 2. Forgot to specify.
I got this until now:
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$deploymentConfig = $obj->get('Magento\Framework\App\DeploymentConfig');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$repo = $objectManager->get('Magento\Catalog\Model\ProductRepository');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();
foreach ($collection as $product){
$name = $product->getName();
$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
$url = strtolower($url);
echo $url;
$pr = $repo->getById($product->getId());
$pr->setUrlKey($url);
$repo->save($pr);
break;
}
But I get this error:
Fatal error: Call to undefined function Magento\Catalog\Model\Config\Source\Product\Options__() in /home2/magazi70/public_html/vendor/magento/module-catalog/Model/Config/Source/Product/Options/Price.php on line 23
<?php
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$deploymentConfig = $obj->get('Magento\Framework\App\DeploymentConfig');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('\Magento\Catalog\Model\Product');
$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();
foreach ($collection as $product){
$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($product->getId());
$name = $product->getName();
$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
$url = strtolower($url);
$product ->setUrlKey($url);
$product->save($pr);
}
The magento script may take longer time.
1. You can try exporting the products (the csv file will not have url keys)
2. Remove all the attributes and keep only SKU and Name and add a new attribute column url_key
3. Use some Excel Functions to generate url keys using Name
4. Remove the Name column
5. Import the csv
to loading a collection and save the new object of product is slow way to done the job
here is best way to
composer require elgentos/regenerate-catalog-urls
php bin/magento module:enable Iazel_RegenProductUrl
php bin/magento setup:upgrade
more information are available on
https://github.com/elgentos/regenerate-catalog-urls
This code shows how to generate an url key, in a helper class, the same way Magento 2 generates url keys when creating products.
In the example I use dependency injection in order to use Magento\Catalog\Model\Product\Url class in my helper.
namespace Myprojects\Mymodule\Helper;
use Magento\Catalog\Model\Product\Url;
use Magento\Framework\App\Helper\Context as HelperContext;
class Data extends AbstractHelper
{
/**
* #param Url $url
*/
public function __construct(
HelperContext $context,
Url $url
)
{
parent::__construct($context);
$this->url = $url;
}
public function generateUrlKey($string)
{
return $this->url->formatUrlKey($string);
}
}

Overwrite Zend_Config and access parents nodes

I want to overwrite Zend_Config method __set($name, $value), but I have same problem.
$name - return current key of overwrite config value, eg:
$this->config->something->other->more = 'crazy variable'; // $name in __set() will return 'more'
Because every node in config is new Zend_Config() class.
So - how from overwritten __set() metod get access to the parents nodes names?
My application:
I must overwrite same config value in controller, but to have controll about the overwritting, and do not allow to overwrite other config variables, I want to specify in same other config variable, an tree-array of overwriting allowed config keys.
Zend_Config is read only unless you have set $allowModifications to true during construction.
From the Zend_Config_Ini::__constructor() docblock:-
/** The $options parameter may be provided as either a boolean or an array.
* If provided as a boolean, this sets the $allowModifications option of
* Zend_Config. If provided as an array, there are three configuration
* directives that may be set. For example:
*
* $options = array(
* 'allowModifications' => false,
* 'nestSeparator' => ':',
* 'skipExtends' => false,
* );
*/
public function __construct($filename, $section = null, $options = false)
This means that you would need to do something like this:-
$inifile = APPLICATION_PATH . '/configs/application.ini';
$section = 'production';
$allowModifications = true;
$config = new Zend_Config_ini($inifile, $section, $allowModifications);
$config->resources->db->params->username = 'test';
var_dump($config->resources->db->params->username);
Result
string 'test' (length=4)
In response to comment
In that case you can simply extend Zend_Config_Ini and override the __construct() and __set() methods like this:-
class Application_Model_Config extends Zend_Config_Ini
{
private $allowed = array();
public function __construct($filename, $section = null, $options = false) {
$this->allowed = array(
'list',
'of',
'allowed',
'variables'
);
parent::__construct($filename, $section, $options);
}
public function __set($name, $value) {
if(in_array($name, $this->allowed)){
$this->_allowModifications = true;
parent::__set($name, $value);
$this->setReadOnly();
} else { parent::__set($name, $value);} //will raise exception as expected.
}
}
Always there is another way :)
$arrSettings = $oConfig->toArray();
$arrSettings['params']['dbname'] = 'new_value';
$oConfig= new Zend_Config($arrSettings);

How do I bind DBI parameters at runtime in Perl?

I have the following code :
sub run_query {
my $name = shift || undef;
my $sql = (defined $name ) ? "select * from table where name = ?" :
"select * from table";
my $sth = $dbh->prepare("$sql");
$sth->execute($name);
}
The above subroutine need to work as follows: if $name is provided, then run the first query, else fetch all the data from the table. How can I bind the name field? I'd like it bound dynamically if it is provided.
From the DBI documentation on cpan:
A common issue is to have a code fragment handle a value that could be
either defined or undef (non-NULL or NULL) at runtime. A simple
technique is to prepare the appropriate statement as needed, and
substitute the placeholder for non-NULL cases:
$sql_clause = defined $age ? "age = ?" : "age IS NULL";
$sth = $dbh->prepare(qq{
SELECT fullname FROM people WHERE $sql_clause
});
$sth->execute(defined $age ? $age : ());
It does not exactly apply to your question, which I assume is that your execute fails if you add an argument where one is not expected. So, the last line here would apply:
$sth->execute(defined $name ? $name : ());
You should probably have two different subs, but you could use
sub run_query {
my $sql = #_
? "select * from table where name = ?"
: "select * from table";
my $sth = $dbh->prepare($sql);
$sth->execute(#_);
}
You can conditionally omit parameters if $name is not defined:
$sth->execute(defined $name ? $name : ());

Zend DB Framework examine query for an update

So you can use something like this:
$query = $db->select();
$query->from('pages', array('url'));
echo $query->__toString();
to examine the sql that the Zend Db Framework is going to use for that SELECT query. Is there an equivilent way to view the SQL for an update?
$data = array(
'content' => stripslashes(htmlspecialchars_decode($content))
);
$n = $db->update('pages', $data, "url = '".$content."'");
??
Use Zend_Db_Profiler to capture and report SQL statements:
$db->getProfiler()->setEnabled(true);
$db->update( ... );
print $db->getProfiler()->getLastQueryProfile()->getQuery();
print_r($db->getProfiler()->getLastQueryProfile()->getQueryParams());
$db->getProfiler()->setEnabled(false);
Remember to turn the profiler off if you don't need it! I talked to one fellow who thought he had a memory leak, but it was the profiler instantiating a few PHP objects for each of the millions of SQL queries he was running.
PS: You should use quoteInto() in that query:
$n = $db->update('pages', $data, $db->quoteInto("url = ?", $content));
No, not directly, since Zend Framework builds and executes the SQL inside the adapter method Zend_Db_Adapter_Abstract::update:
/**
* Updates table rows with specified data based on a WHERE clause.
*
* #param mixed $table The table to update.
* #param array $bind Column-value pairs.
* #param mixed $where UPDATE WHERE clause(s).
* #return int The number of affected rows.
*/
public function update($table, array $bind, $where = '')
{
/**
* Build "col = ?" pairs for the statement,
* except for Zend_Db_Expr which is treated literally.
*/
$set = array();
foreach ($bind as $col => $val) {
if ($val instanceof Zend_Db_Expr) {
$val = $val->__toString();
unset($bind[$col]);
} else {
$val = '?';
}
$set[] = $this->quoteIdentifier($col, true) . ' = ' . $val;
}
$where = $this->_whereExpr($where);
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->quoteIdentifier($table, true)
. ' SET ' . implode(', ', $set)
. (($where) ? " WHERE $where" : '');
/**
* Execute the statement and return the number of affected rows
*/
$stmt = $this->query($sql, array_values($bind));
$result = $stmt->rowCount();
return $result;
}
You can, temporarily, insert a var_dump and exit inside this method to inspect the sql to ensure that it is correct:
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->quoteIdentifier($table, true)
. ' SET ' . implode(', ', $set)
. (($where) ? " WHERE $where" : '');
var_dump($sql); exit;
I quess another way is to log the actual SQL query, rather than changing the ZF library code, by combining the profiler data.
$db->getProfiler()->setEnabled(true);
$db->update( ... );
$query = $db->getProfiler()->getLastQueryProfile()->getQuery();
$queryParams = $db->getProfiler()->getLastQueryProfile()->getQueryParams();
$logger->log('SQL: ' . $db->quoteInto($query, $queryParams), Zend_Log::DEBUG);
$db->getProfiler()->setEnabled(false);
Recently came across this looking for a way to debug a zend_db_statement. If anyone else comes across this with the same search, you can use the following function.
Just replace "self::getDefaultAdapter()" with your method of getting a DB connection or adapter.
/**
* replace any named parameters with placeholders
* #param string $sql sql string with placeholders, e.g. :theKey
* #param array $bind array keyed on placeholders, e.g. array('theKey', 'THEVALUE')
*
* #return String sql statement with the placeholders replaced
*/
public static function debugNamedParamsSql($sql, array $bind) {
$sqlDebug = $sql;
foreach($bind as $needle => $replace) {
$sqlDebug = str_replace(
':' . $needle,
self::getDefaultAdapter()->quote($replace),
$sqlDebug
);
}
return $sqlDebug;
}