2010-04-08 00:47:42 -03:00
< ? php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
2012-05-26 14:37:00 +02:00
* and is licensed under the MIT license . For more information , see
2010-04-08 00:47:42 -03:00
* < http :// www . doctrine - project . org >.
*/
namespace Doctrine\ORM\Tools\Console\Command ;
2017-11-23 11:18:08 +01:00
use Doctrine\ORM\EntityManagerInterface ;
use Doctrine\ORM\Tools\Console\MetadataFilter ;
use Symfony\Component\Console\Command\Command ;
2012-12-14 12:49:05 +00:00
use Symfony\Component\Console\Input\InputArgument ;
2017-11-23 11:18:08 +01:00
use Symfony\Component\Console\Input\InputInterface ;
2012-12-14 12:49:05 +00:00
use Symfony\Component\Console\Input\InputOption ;
2012-10-22 01:58:31 +02:00
use Symfony\Component\Console\Output\OutputInterface ;
2010-04-08 00:47:42 -03:00
/**
* Command to ( re ) generate the proxy classes used by doctrine .
*
* @ link www . doctrine - project . org
* @ since 2.0
* @ author Benjamin Eberlei < kontakt @ beberlei . de >
* @ author Guilherme Blanco < guilhermeblanco @ hotmail . com >
* @ author Jonathan Wage < jonwage @ gmail . com >
* @ author Roman Borschel < roman @ code - factory . org >
*/
2012-10-22 01:58:31 +02:00
class GenerateProxiesCommand extends Command
2010-04-08 00:47:42 -03:00
{
2012-12-14 12:49:05 +00:00
/**
* { @ inheritdoc }
*/
2010-04-08 00:47:42 -03:00
protected function configure ()
{
2017-11-23 11:18:08 +01:00
$this -> setName ( 'orm:generate-proxies' )
-> setAliases ([ 'orm:generate:proxies' ])
-> setDescription ( 'Generates proxy classes for entity classes' )
-> addArgument ( 'dest-path' , InputArgument :: OPTIONAL , 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' )
-> addOption ( 'filter' , null , InputOption :: VALUE_REQUIRED | InputOption :: VALUE_IS_ARRAY , 'A string pattern used to match entities that should be processed.' )
-> setHelp ( 'Generates proxy classes for entity classes.' );
2010-04-08 00:47:42 -03:00
}
2012-12-14 12:49:05 +00:00
/**
* { @ inheritdoc }
*/
2012-10-22 01:58:31 +02:00
protected function execute ( InputInterface $input , OutputInterface $output )
2010-04-08 00:47:42 -03:00
{
2017-11-23 11:18:08 +01:00
/** @var EntityManagerInterface $em */
2010-04-08 00:47:42 -03:00
$em = $this -> getHelper ( 'em' ) -> getEntityManager ();
2011-12-19 22:56:19 +01:00
2010-04-11 09:30:01 +02:00
$metadatas = $em -> getMetadataFactory () -> getAllMetadata ();
$metadatas = MetadataFilter :: filter ( $metadatas , $input -> getOption ( 'filter' ));
2010-04-08 00:47:42 -03:00
// Process destination directory
if (( $destPath = $input -> getArgument ( 'dest-path' )) === null ) {
$destPath = $em -> getConfiguration () -> getProxyDir ();
}
2010-04-13 14:12:44 -04:00
if ( ! is_dir ( $destPath )) {
2015-08-31 13:57:29 +02:00
mkdir ( $destPath , 0775 , true );
2010-04-13 14:12:44 -04:00
}
2010-04-08 00:47:42 -03:00
$destPath = realpath ( $destPath );
if ( ! file_exists ( $destPath )) {
throw new \InvalidArgumentException (
2011-08-27 13:36:18 +02:00
sprintf ( " Proxies destination directory '<info>%s</info>' does not exist. " , $em -> getConfiguration () -> getProxyDir ())
2010-04-08 00:47:42 -03:00
);
2012-10-22 01:58:31 +02:00
}
if ( ! is_writable ( $destPath )) {
2010-04-08 00:47:42 -03:00
throw new \InvalidArgumentException (
sprintf ( " Proxies destination directory '<info>%s</info>' does not have write permissions. " , $destPath )
);
}
2010-04-11 09:30:01 +02:00
if ( count ( $metadatas )) {
2010-04-08 00:47:42 -03:00
foreach ( $metadatas as $metadata ) {
2012-10-22 01:58:31 +02:00
$output -> writeln (
sprintf ( 'Processing entity "<info>%s</info>"' , $metadata -> name )
2010-04-08 00:47:42 -03:00
);
}
// Generating Proxies
$em -> getProxyFactory () -> generateProxyClasses ( $metadatas , $destPath );
// Outputting information message
2012-10-22 01:58:31 +02:00
$output -> writeln ( PHP_EOL . sprintf ( 'Proxy classes generated to "<info>%s</INFO>"' , $destPath ));
2010-04-08 00:47:42 -03:00
} else {
2012-10-22 01:58:31 +02:00
$output -> writeln ( 'No Metadata Classes to process.' );
2010-04-08 00:47:42 -03:00
}
}
2010-08-23 08:21:41 +02:00
}