src/Admin/Controllers/EntityManagerController.php
public function do_generate_daos($entitiesNamespace, $proxyNamespace, $daoNamespace, $instanceName, $selfedit, $installMode = null)
    {
        $this->instanceName = $instanceName;
        $this->selfedit = $selfedit;
        $this->installMode = $installMode;

        if ($selfedit == 'true') {
            $this->moufManager = MoufManager::getMoufManager();
        } else {
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
        }

        $dbalConnection = $this->moufManager->getInstanceDescriptor('dbalConnection');
        $eventManager = $dbalConnection->getProperty('eventManager')->getValue();

        if (!$this->moufManager->instanceExists($instanceName)) {
            $em = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\MoufResetableEntityManager");
            $em->setName($instanceName);

            $quoteStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\EscapingQuoteStrategy");
            $quoteStrategy->setName('escapingQuoteStrategy');

            $namingStrategy = $this->moufManager->createInstance("Doctrine\\ORM\\Mapping\\UnderscoreNamingStrategy");
            $namingStrategy->setName('underscoreNamingStrategy');

            $doctrineApc = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');

            $config = $this->moufManager->createInstance("Doctrine\\ORM\\Configuration");
            $config->setName('doctrineConfiguration');

            $configQuoteProperty = $config->getSetterProperty('setQuoteStrategy');
            $configQuoteProperty->setValue($quoteStrategy);

            $configNamingProperty = $config->getSetterProperty('setNamingStrategy');
            $configNamingProperty->setValue($namingStrategy);

            $configQueryCacheProperty = $config->getSetterProperty('setQueryCacheImpl');
            $configQueryCacheProperty->setValue($doctrineApc);

            $configMetadataCacheProperty = $config->getSetterProperty('setMetadataCacheImpl');
            $configMetadataCacheProperty->setValue($doctrineApc);

            $configResultCacheProperty = $config->getSetterProperty('setResultCacheImpl');
            $configResultCacheProperty->setValue($doctrineApc);
        } else {
            $em = $this->moufManager->getInstanceDescriptor($instanceName);
            $config = $em->getProperty('config')->getValue();

            $configQuoteProperty = $config->getSetterProperty('setQuoteStrategy');
            if (!$configQuoteProperty->isValueSet()) {
                $quoteStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\EscapingQuoteStrategy");
                $quoteStrategy->setName('escapingQuoteStrategy');
                $configQuoteProperty->setValue($quoteStrategy);
            }

            $configNamingProperty = $config->getSetterProperty('setNamingStrategy');
            if(!$configNamingProperty) {
                $namingStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\UnderscoreNamingStrategy");
                $namingStrategy->setName('underscoreNamingStrategy');
                $configNamingProperty->setValue($namingStrategy);
            }

            $configMetadataCacheProperty = $config->getSetterProperty('setMetadataCacheImpl');
            $configQueryCacheProperty = $config->getSetterProperty('setQueryCacheImpl');
            $configResultCacheProperty = $config->getSetterProperty('setResultCacheImpl');
            if(!$configMetadataCacheProperty || !$configQueryCacheProperty || !$configResultCacheProperty){
                $doctrineApc = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');

                if(!$configQueryCacheProperty){
                    $configQueryCacheProperty->setValue($doctrineApc);
                }

                if(!$configMetadataCacheProperty){
                    $configMetadataCacheProperty->setValue($doctrineApc);
                }

                if(!$configResultCacheProperty){
                    $configResultCacheProperty->setValue($doctrineApc);
                }
            }
        }

        $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../composer.json');

        $entitiesNamespace = rtrim($entitiesNamespace, '\\');
        $proxyNamespace = rtrim($proxyNamespace, '\\');
        $daoNamespace = rtrim($daoNamespace, '\\');

        // Let's locate the path by locating a fake class in the namespace.
        $entitiesPath = substr($classNameMapper->getPossibleFileNames($entitiesNamespace.'\\ZZZ')[0], 0, -7);
        $proxyPath = substr($classNameMapper->getPossibleFileNames($proxyNamespace.'\\ZZZ')[0], 0, -7);
        $daoPath = substr($classNameMapper->getPossibleFileNames($daoNamespace.'\\ZZZ')[0], 0, -7);

        $fileSystem = new Filesystem();
        $oldMask = umask(0);
        // Note: for some reason, the mode of mkdir is not accounted for. We need to call chmod on it
        // Not perfect: only the last dir takes the mode, not the intermediate directories.
        $fileSystem->mkdir(array(ROOT_PATH.'../../../'.$entitiesPath, ROOT_PATH.'../../../'.$proxyPath, ROOT_PATH.'../../../'.$daoPath), 0775);
        try {
            $fileSystem->chmod(array(ROOT_PATH.'../../../'.$entitiesPath, ROOT_PATH.'../../../'.$proxyPath, ROOT_PATH.'../../../'.$daoPath), 0775);
        } catch (IOException $e) {
            // Do nothing because the change mode can send an error if the folder is associated to another user (in the same group)
        }
        umask($oldMask);

        $defaultMappingDriver = InstallUtils::getOrCreateInstance('defaultMappingDriver', null, $this->moufManager);
        $defaultMappingDriver->setCode('return new Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver($container->get(\'annotationReader\'), [ROOT_PATH . "'.$entitiesPath.'"]);');

        if (!$this->moufManager->has('mappingDriverChain')) {
            $mappingDriverChain = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\Driver\\MappingDriverChain");
            $mappingDriverChain->setName('mappingDriverChain');
            $mappingDriverChain->getProperty('defaultDriver')->setValue($defaultMappingDriver);
        } else {
            $mappingDriverChain = $this->moufManager->getInstanceDescriptor('mappingDriverChain');
        }

        $config->getProperty('metadataDriverImpl')->setValue($mappingDriverChain);
        $config->getProperty('proxyDir')->setOrigin('php')->setValue('return ROOT_PATH."'.addslashes($proxyPath).'";');
        $config->getProperty('proxyNamespace')->setValue($proxyNamespace);
        // Proxy classes are generated in development mode only.
        $config->getProperty('autoGenerateProxyClasses')->setOrigin('config')->setValue('DEBUG');
        // Ignore table "patches" because it is managed by our patch system.
        $config->getProperty('filterSchemaAssetsExpression')->setValue('/^(?!patches$).*/');

        // On the dbalConnection, we register a mapping type "enum"=>"string"
        $em->getProperty('entityManagerClassName')->setValue("Mouf\\Doctrine\\ORM\\EntityManager");
        $em->getProperty('conn')->setOrigin('php')->setValue('$dbalConnection = $container->get("dbalConnection");
$dbalConnection->getDatabasePlatform()->registerDoctrineTypeMapping("enum", "string");
return $dbalConnection;');
        $em->getProperty('config')->setValue($config);
        $em->getProperty('eventManager')->setValue($eventManager);

        $em->getSetterProperty('setEntitiesNamespace')->setValue($entitiesNamespace);
        $em->getSetterProperty('setProxyNamespace')->setValue($proxyNamespace);
        $em->getSetterProperty('setDaoNamespace')->setValue($daoNamespace);

        //Update connection to get the same configuration instance
        $dbalConnection->getProperty('config')->setValue($config);

        // Now, let's write the commands
        $consoleUtils = new ConsoleUtils($this->moufManager);

        // Let's configure the console
        if (!$this->moufManager->instanceExists("ormConnectionHelper")){
            $ormConnectionHelper = InstallUtils::getOrCreateInstance('ormConnectionHelper', 'Doctrine\\ORM\\Tools\\Console\\Helper\\EntityManagerHelper', $this->moufManager);
            $ormConnectionHelper->getConstructorArgumentProperty("em")->setValue($em);
            $consoleUtils->registerHelper($ormConnectionHelper, 'em');
        }

        $commands = [
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\MetadataCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\ResultCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\QueryCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\CreateCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\UpdateCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\DropCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\EnsureProductionSettingsCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ConvertDoctrine1SchemaCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateRepositoriesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateEntitiesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateProxiesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ConvertMappingCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\RunDqlCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ValidateSchemaCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\InfoCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\MappingDescribeCommand',
        ];

        foreach ($commands as $command) {
            $consoleUtils->registerCommand($this->moufManager->createInstance($command));
        }

        $this->moufManager->rewriteMouf();

        //Todo: If we generate Dao's we might create all the instances
        $proxy = new InstanceProxy($instanceName);
        $daoData = $proxy->generateDAOs();

        header('Location: generate_schema?name='.urlencode($instanceName).'&selfedit='.urlencode($selfedit).'&installMode='.urlencode($installMode));
    }
public function do_generate_daos($entitiesNamespace, $proxyNamespace, $daoNamespace, $instanceName, $selfedit, $installMode = null)
    {
        $this->instanceName = $instanceName;
        $this->selfedit = $selfedit;
        $this->installMode = $installMode;

        if ($selfedit == 'true') {
            $this->moufManager = MoufManager::getMoufManager();
        } else {
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
        }

        $dbalConnection = $this->moufManager->getInstanceDescriptor('dbalConnection');
        $eventManager = $dbalConnection->getProperty('eventManager')->getValue();

        if (!$this->moufManager->instanceExists($instanceName)) {
            $em = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\MoufResetableEntityManager");
            $em->setName($instanceName);

            $quoteStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\EscapingQuoteStrategy");
            $quoteStrategy->setName('escapingQuoteStrategy');

            $namingStrategy = $this->moufManager->createInstance("Doctrine\\ORM\\Mapping\\UnderscoreNamingStrategy");
            $namingStrategy->setName('underscoreNamingStrategy');

            $doctrineApc = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');

            $config = $this->moufManager->createInstance("Doctrine\\ORM\\Configuration");
            $config->setName('doctrineConfiguration');

            $configQuoteProperty = $config->getSetterProperty('setQuoteStrategy');
            $configQuoteProperty->setValue($quoteStrategy);

            $configNamingProperty = $config->getSetterProperty('setNamingStrategy');
            $configNamingProperty->setValue($namingStrategy);

            $configQueryCacheProperty = $config->getSetterProperty('setQueryCacheImpl');
            $configQueryCacheProperty->setValue($doctrineApc);

            $configMetadataCacheProperty = $config->getSetterProperty('setMetadataCacheImpl');
            $configMetadataCacheProperty->setValue($doctrineApc);

            $configResultCacheProperty = $config->getSetterProperty('setResultCacheImpl');
            $configResultCacheProperty->setValue($doctrineApc);
        } else {
            $em = $this->moufManager->getInstanceDescriptor($instanceName);
            $config = $em->getProperty('config')->getValue();

            $configQuoteProperty = $config->getSetterProperty('setQuoteStrategy');
            if (!$configQuoteProperty->isValueSet()) {
                $quoteStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\EscapingQuoteStrategy");
                $quoteStrategy->setName('escapingQuoteStrategy');
                $configQuoteProperty->setValue($quoteStrategy);
            }

            $configNamingProperty = $config->getSetterProperty('setNamingStrategy');
            if(!$configNamingProperty) {
                $namingStrategy = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\UnderscoreNamingStrategy");
                $namingStrategy->setName('underscoreNamingStrategy');
                $configNamingProperty->setValue($namingStrategy);
            }

            $configMetadataCacheProperty = $config->getSetterProperty('setMetadataCacheImpl');
            $configQueryCacheProperty = $config->getSetterProperty('setQueryCacheImpl');
            $configResultCacheProperty = $config->getSetterProperty('setResultCacheImpl');
            if(!$configMetadataCacheProperty || !$configQueryCacheProperty || !$configResultCacheProperty){
                $doctrineApc = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');

                if(!$configQueryCacheProperty){
                    $configQueryCacheProperty->setValue($doctrineApc);
                }

                if(!$configMetadataCacheProperty){
                    $configMetadataCacheProperty->setValue($doctrineApc);
                }

                if(!$configResultCacheProperty){
                    $configResultCacheProperty->setValue($doctrineApc);
                }
            }
        }

        $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../composer.json');

        $entitiesNamespace = rtrim($entitiesNamespace, '\\');
        $proxyNamespace = rtrim($proxyNamespace, '\\');
        $daoNamespace = rtrim($daoNamespace, '\\');

        // Let's locate the path by locating a fake class in the namespace.
        $entitiesPath = substr($classNameMapper->getPossibleFileNames($entitiesNamespace.'\\ZZZ')[0], 0, -7);
        $proxyPath = substr($classNameMapper->getPossibleFileNames($proxyNamespace.'\\ZZZ')[0], 0, -7);
        $daoPath = substr($classNameMapper->getPossibleFileNames($daoNamespace.'\\ZZZ')[0], 0, -7);

        $fileSystem = new Filesystem();
        $oldMask = umask(0);
        // Note: for some reason, the mode of mkdir is not accounted for. We need to call chmod on it
        // Not perfect: only the last dir takes the mode, not the intermediate directories.
        $fileSystem->mkdir(array(ROOT_PATH.'../../../'.$entitiesPath, ROOT_PATH.'../../../'.$proxyPath, ROOT_PATH.'../../../'.$daoPath), 0775);
        try {
            $fileSystem->chmod(array(ROOT_PATH.'../../../'.$entitiesPath, ROOT_PATH.'../../../'.$proxyPath, ROOT_PATH.'../../../'.$daoPath), 0775);
        } catch (IOException $e) {
            // Do nothing because the change mode can send an error if the folder is associated to another user (in the same group)
        }
        umask($oldMask);

        $defaultMappingDriver = InstallUtils::getOrCreateInstance('defaultMappingDriver', null, $this->moufManager);
        $defaultMappingDriver->setCode('return new Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver($container->get(\'annotationReader\'), [ROOT_PATH . "'.$entitiesPath.'"]);');

        if (!$this->moufManager->has('mappingDriverChain')) {
            $mappingDriverChain = $this->moufManager->createInstance("Mouf\\Doctrine\\ORM\\Mapping\\Driver\\MappingDriverChain");
            $mappingDriverChain->setName('mappingDriverChain');
            $mappingDriverChain->getProperty('defaultDriver')->setValue($defaultMappingDriver);
        } else {
            $mappingDriverChain = $this->moufManager->getInstanceDescriptor('mappingDriverChain');
        }

        $config->getProperty('metadataDriverImpl')->setValue($mappingDriverChain);
        $config->getProperty('proxyDir')->setOrigin('php')->setValue('return ROOT_PATH."'.addslashes($proxyPath).'";');
        $config->getProperty('proxyNamespace')->setValue($proxyNamespace);
        // Proxy classes are generated in development mode only.
        $config->getProperty('autoGenerateProxyClasses')->setOrigin('config')->setValue('DEBUG');
        // Ignore table "patches" because it is managed by our patch system.
        $config->getProperty('filterSchemaAssetsExpression')->setValue('/^(?!patches$).*/');

        // On the dbalConnection, we register a mapping type "enum"=>"string"
        $em->getProperty('entityManagerClassName')->setValue("Mouf\\Doctrine\\ORM\\EntityManager");
        $em->getProperty('conn')->setOrigin('php')->setValue('$dbalConnection = $container->get("dbalConnection");
$dbalConnection->getDatabasePlatform()->registerDoctrineTypeMapping("enum", "string");
return $dbalConnection;');
        $em->getProperty('config')->setValue($config);
        $em->getProperty('eventManager')->setValue($eventManager);

        $em->getSetterProperty('setEntitiesNamespace')->setValue($entitiesNamespace);
        $em->getSetterProperty('setProxyNamespace')->setValue($proxyNamespace);
        $em->getSetterProperty('setDaoNamespace')->setValue($daoNamespace);

        //Update connection to get the same configuration instance
        $dbalConnection->getProperty('config')->setValue($config);

        // Now, let's write the commands
        $consoleUtils = new ConsoleUtils($this->moufManager);

        // Let's configure the console
        if (!$this->moufManager->instanceExists("ormConnectionHelper")){
            $ormConnectionHelper = InstallUtils::getOrCreateInstance('ormConnectionHelper', 'Doctrine\\ORM\\Tools\\Console\\Helper\\EntityManagerHelper', $this->moufManager);
            $ormConnectionHelper->getConstructorArgumentProperty("em")->setValue($em);
            $consoleUtils->registerHelper($ormConnectionHelper, 'em');
        }

        $commands = [
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\MetadataCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\ResultCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\QueryCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\CreateCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\UpdateCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\DropCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\EnsureProductionSettingsCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ConvertDoctrine1SchemaCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateRepositoriesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateEntitiesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\GenerateProxiesCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ConvertMappingCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\RunDqlCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\ValidateSchemaCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\InfoCommand',
            '\\Doctrine\\ORM\\Tools\\Console\\Command\\MappingDescribeCommand',
        ];

        foreach ($commands as $command) {
            $consoleUtils->registerCommand($this->moufManager->createInstance($command));
        }

        $this->moufManager->rewriteMouf();

        //Todo: If we generate Dao's we might create all the instances
        $proxy = new InstanceProxy($instanceName);
        $daoData = $proxy->generateDAOs();

        header('Location: generate_schema?name='.urlencode($instanceName).'&selfedit='.urlencode($selfedit).'&installMode='.urlencode($installMode));
    }
Displays the "schema generation screen".

@Action
@Logged

@param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)