src/Common/Console/Command/Make/Event/Listener.php
private function createEventListener(): self
    {
        $aFields  = $this->getArguments();
        $aCreated = [];

        try {

            $aToCreate  = [];
            $aListeners = array_filter(
                array_map(function ($sListener) {
                    return implode('/', array_map('ucfirst', explode('/', ucfirst(trim($sListener)))));
                }, explode(',', $aFields['NAME']))
            );

            sort($aListeners);

            foreach ($aListeners as $sListener) {

                $aListenerBits = explode('/', $sListener);
                $aListenerBits = array_map('ucfirst', $aListenerBits);

                $sNamespace     = $this->generateNamespace($aListenerBits);
                $sClassName     = $this->generateClassName($aListenerBits);
                $sClassNameFull = $sNamespace . '\\' . $sClassName;
                $sFilePath      = $this->generateFilePath($aListenerBits);

                //  Test it does not already exist
                if (file_exists($sFilePath)) {
                    throw new ConsoleException(
                        'An event listener at "' . $sFilePath . '" already exists'
                    );
                }

                $aToCreate[] = [
                    'NAMESPACE' => $sNamespace,
                    'CLASS_NAME' => $sClassName,
                    'CLASS_NAME_FULL' => $sClassNameFull,
                    'FILE_PATH' => $sFilePath,
                    'DIRECTORY' => dirname($sFilePath) . DIRECTORY_SEPARATOR,
                ];
            }

            $this->oOutput->writeln('The following event listeners(s) will be created:');
            foreach ($aToCreate as $aConfig) {
                $this->oOutput->writeln('');
                $this->oOutput->writeln('Class: <info>' . $aConfig['CLASS_NAME_FULL'] . '</info>');
                $this->oOutput->writeln('Path:  <info>' . $aConfig['FILE_PATH'] . '</info>');
            }
            $this->oOutput->writeln('');

            if ($this->confirm('Continue?', true)) {
                foreach ($aToCreate as $aConfig) {
                    $this->oOutput->writeln('');
                    $this->oOutput->write('Creating event listener <comment>' . $aConfig['CLASS_NAME_FULL'] . '</comment>... ');
                    $this->createPath($aConfig['DIRECTORY']);
                    $this->createFile(
                        $aConfig['FILE_PATH'],
                        $this->getResource('template/event_listener.php', $aConfig)
                    );
                    $aCreated[] = $aConfig['FILE_PATH'];
                    $this->oOutput->writeln('<info>done!</info>');

                    //  @todo (Pablo - 2019-04-30) - Add to src/Events.php if properly configured
                }
            }

        } catch (ConsoleException $e) {
            $this->oOutput->writeln('<error>failed!</error>');
            //  Clean up created services
            if (!empty($aCreated)) {
                $this->oOutput->writeln('<error>Cleaning up - removing newly created files</error>');
                foreach ($aCreated as $sPath) {
                    @unlink($sPath);
                }
            }
            throw new ConsoleException($e->getMessage());
        }

        return $this;
    }
private function createEventListener(): self
    {
        $aFields  = $this->getArguments();
        $aCreated = [];

        try {

            $aToCreate  = [];
            $aListeners = array_filter(
                array_map(function ($sListener) {
                    return implode('/', array_map('ucfirst', explode('/', ucfirst(trim($sListener)))));
                }, explode(',', $aFields['NAME']))
            );

            sort($aListeners);

            foreach ($aListeners as $sListener) {

                $aListenerBits = explode('/', $sListener);
                $aListenerBits = array_map('ucfirst', $aListenerBits);

                $sNamespace     = $this->generateNamespace($aListenerBits);
                $sClassName     = $this->generateClassName($aListenerBits);
                $sClassNameFull = $sNamespace . '\\' . $sClassName;
                $sFilePath      = $this->generateFilePath($aListenerBits);

                //  Test it does not already exist
                if (file_exists($sFilePath)) {
                    throw new ConsoleException(
                        'An event listener at "' . $sFilePath . '" already exists'
                    );
                }

                $aToCreate[] = [
                    'NAMESPACE' => $sNamespace,
                    'CLASS_NAME' => $sClassName,
                    'CLASS_NAME_FULL' => $sClassNameFull,
                    'FILE_PATH' => $sFilePath,
                    'DIRECTORY' => dirname($sFilePath) . DIRECTORY_SEPARATOR,
                ];
            }

            $this->oOutput->writeln('The following event listeners(s) will be created:');
            foreach ($aToCreate as $aConfig) {
                $this->oOutput->writeln('');
                $this->oOutput->writeln('Class: <info>' . $aConfig['CLASS_NAME_FULL'] . '</info>');
                $this->oOutput->writeln('Path:  <info>' . $aConfig['FILE_PATH'] . '</info>');
            }
            $this->oOutput->writeln('');

            if ($this->confirm('Continue?', true)) {
                foreach ($aToCreate as $aConfig) {
                    $this->oOutput->writeln('');
                    $this->oOutput->write('Creating event listener <comment>' . $aConfig['CLASS_NAME_FULL'] . '</comment>... ');
                    $this->createPath($aConfig['DIRECTORY']);
                    $this->createFile(
                        $aConfig['FILE_PATH'],
                        $this->getResource('template/event_listener.php', $aConfig)
                    );
                    $aCreated[] = $aConfig['FILE_PATH'];
                    $this->oOutput->writeln('<info>done!</info>');

                    //  @todo (Pablo - 2019-04-30) - Add to src/Events.php if properly configured
                }
            }

        } catch (ConsoleException $e) {
            $this->oOutput->writeln('<error>failed!</error>');
            //  Clean up created services
            if (!empty($aCreated)) {
                $this->oOutput->writeln('<error>Cleaning up - removing newly created files</error>');
                foreach ($aCreated as $sPath) {
                    @unlink($sPath);
                }
            }
            throw new ConsoleException($e->getMessage());
        }

        return $this;
    }
private function createEventListener ( ) : self { $ aFields = $ this -> getArguments ( ) ; $ aCreated = [ ] ; try { $ aToCreate = [ ] ; $ aListeners = array_filter ( array_map ( function ( $ sListener ) { return implode ( '/' , array_map ( 'ucfirst' , explode ( '/' , ucfirst ( trim ( $ sListener ) ) ) ) ) ; } , explode ( ',' , $ aFields [ 'NAME' ] ) ) ) ; sort ( $ aListeners ) ; foreach ( $ aListeners as $ sListener ) { $ aListenerBits = explode ( '/' , $ sListener ) ; $ aListenerBits = array_map ( 'ucfirst' , $ aListenerBits ) ; $ sNamespace = $ this -> generateNamespace ( $ aListenerBits ) ; $ sClassName = $ this -> generateClassName ( $ aListenerBits ) ; $ sClassNameFull = $ sNamespace . '\\' . $ sClassName ; $ sFilePath = $ this -> generateFilePath ( $ aListenerBits ) ; //  Test it does not already exist if ( file_exists ( $ sFilePath ) ) { throw new ConsoleException ( 'An event listener at "' . $ sFilePath . '" already exists' ) ; } $ aToCreate [ ] = [ 'NAMESPACE' => $ sNamespace , 'CLASS_NAME' => $ sClassName , 'CLASS_NAME_FULL' => $ sClassNameFull , 'FILE_PATH' => $ sFilePath , 'DIRECTORY' => dirname ( $ sFilePath ) . DIRECTORY_SEPARATOR , ] ; } $ this -> oOutput -> writeln ( 'The following event listeners(s) will be created:' ) ; foreach ( $ aToCreate as $ aConfig ) { $ this -> oOutput -> writeln ( '' ) ; $ this -> oOutput -> writeln ( 'Class: <info>' . $ aConfig [ 'CLASS_NAME_FULL' ] . '</info>' ) ; $ this -> oOutput -> writeln ( 'Path:  <info>' . $ aConfig [ 'FILE_PATH' ] . '</info>' ) ; } $ this -> oOutput -> writeln ( '' ) ; if ( $ this -> confirm ( 'Continue?' , true ) ) { foreach ( $ aToCreate as $ aConfig ) { $ this -> oOutput -> writeln ( '' ) ; $ this -> oOutput -> write ( 'Creating event listener <comment>' . $ aConfig [ 'CLASS_NAME_FULL' ] . '</comment>... ' ) ; $ this -> createPath ( $ aConfig [ 'DIRECTORY' ] ) ; $ this -> createFile ( $ aConfig [ 'FILE_PATH' ] , $ this -> getResource ( 'template/event_listener.php' , $ aConfig ) ) ; $ aCreated [ ] = $ aConfig [ 'FILE_PATH' ] ; $ this -> oOutput -> writeln ( '<info>done!</info>' ) ; //  @todo (Pablo - 2019-04-30) - Add to src/Events.php if properly configured } } } catch ( ConsoleException $ e ) { $ this -> oOutput -> writeln ( '<error>failed!</error>' ) ; //  Clean up created services if ( ! empty ( $ aCreated ) ) { $ this -> oOutput -> writeln ( '<error>Cleaning up - removing newly created files</error>' ) ; foreach ( $ aCreated as $ sPath ) { @ unlink ( $ sPath ) ; } } throw new ConsoleException ( $ e -> getMessage ( ) ) ; } return $ this ; }
Create the Listener

@return$this
@throws ConsoleException
Create the Listener