1

I am trying to create an install script to overwrite some bluefoot configurations. I am trying to figure out what is in the $buttonItemBlock variable so that I can pass that to another class. However I can not get anything to print during upgrade in the console.

<?php

namespace vender\module\Setup;

use Gene\BlueFoot\Model\Installer\File as InstallerFile;
use Magento\Framework\Filesystem\Io\File as IoFile;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Gene\BlueFoot\Model\Attribute\ContentBlockRepository;

/**
 * Class to install data for list builder BlueFoot block
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var Reader
     */
    protected $moduleReader;

    /**
     * @var IoFile
     */
    protected $ioFile;

    /**
     * @var InstallerFile
     */
    protected $fileInstaller;

    /**
     * @var Bluefoot content block repository manager
     */
    protected $contentBlockRepository;

    /**
     * InstallData constructor.
     *
     * @param Reader $moduleReader
     * @param IoFile $ioFile
     * @param InstallerFile $fileInstaller
     */
    public function __construct(
        Reader $moduleReader,
        IoFile $ioFile,
        InstallerFile $fileInstaller,
        ContentBlockRepository $contentBlockRepository
    ) {
        $this->moduleReader = $moduleReader;
        $this->ioFile = $ioFile;
        $this->fileInstaller = $fileInstaller;
        $this->contentBlockRepository = $contentBlockRepository;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $buttonItemBlock = $this->contentBlockRepository->getByIdentifier('button_item');
        var_dump($buttonItemBlock);exit;

        //Install the blocks and attributes
        //$this->installData($setup);

        $setup->endSetup();
    }
}
  • Have you tried echo 'test'; ? – Macas Feb 14 '18 at 19:21
  • Which type of output do you need? To console? To log file? – Stepan Furman Feb 14 '18 at 20:10
  • @Macas Yes I tried that – Metropolis Feb 14 '18 at 20:16
  • @Furman Anyway I can get it. – Metropolis Feb 14 '18 at 20:16
  • Just set psr logger interface to constructor and add any log message you need. Do you need additional guide how to inject logger to installer? – Stepan Furman Feb 14 '18 at 20:17
2

Shorter Logging to the file without logger object, in case you would need to log something fast:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/debug.log');  
$logger = new \Zend\Log\Logger();  
$logger->addWriter($writer);  
$logger->info('MESSAGE');

You can try this one, maybe it will suit you needs.

  • Can I output var_dump this way? – Metropolis Feb 14 '18 at 21:14
  • You can print_r($variable, true). I'm not sure about var_dump() – Macas Feb 15 '18 at 19:44
1

You can just log to system.log using LoggerInterface Furman/ConsoleOutput/Setup/InstallData.php

<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 14.02.18
 * Time: 22:26
 */

namespace Furman\ConsoleOutput\Setup;

use Psr\Log\LoggerInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
/**
 * @var LoggerInterface
 */
protected $logger;

/**
 * InstallData constructor.
 * @param LoggerInterface $logger
 */
public function __construct(
    LoggerInterface $logger
)
{
    $this->logger = $logger;
}

/**
 *
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function 
install(\Magento\Framework\Setup\ModuleDataSetupInterface $setup, 
\Magento\Framework\Setup\ModuleContextInterface $context)
{
    $setup->startSetup();

    $this->logger->info('test log string');

    $setup->endSetup();
}

}

run at the root of your project

php bin/magento setup:upgrade

And see at the log file:

var/log/system.log

P.S. Don't forget to enable "Log to File" system/config settings at the admin panel Stores->Settings->Configuration->Advanced->Developer->Debug->Log to file

P.P.S. Output using echo also works fine: enter image description here

  • I am starting to wonder if my install script is being called. I should be able to just disable the module and re-enable it -> run magento upgrade and it should work right? – Metropolis Feb 14 '18 at 20:49
  • No, you just need to kill your module row in setup_module table at the DB – Stepan Furman Feb 14 '18 at 20:56
  • Yup just did that :). Looks like its working now. Thanks! – Metropolis Feb 14 '18 at 20:56
  • You are welcome. – Stepan Furman Feb 14 '18 at 20:57
  • One more thing, what is the $context param on that function? – Metropolis Feb 14 '18 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.