public function generate($namespace, $bundle, $dir, $format)
    {
        $dir .= '/' . strtr($namespace, '\\', '/');

        // make sure we have no trailing \ in namespace
        if (substr($namespace, -1) == '\\') {
            $namespace = substr($namespace, 0, -1);
        }

        $basename = $this->getBundleBaseName($bundle);
        $parameters = array(
            'namespace' => $namespace,
            'bundle' => $bundle,
            'format' => $format,
            'bundle_basename' => $basename,
            'extension_alias' => Container::underscore($basename),
        );

        $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle . '.php', $parameters);
        $this->renderFile(
            'bundle/Extension.php.twig',
            $dir . '/DependencyInjection/' . $basename . 'Extension.php',
            $parameters
        );

        $this->renderFile('bundle/config.xml.twig', $dir . '/Resources/config/config.xml', $parameters);

        if ('xml' === $format || 'annotation' === $format) {
            $this->renderFile('bundle/services.xml.twig', $dir . '/Resources/config/services.xml', $parameters);
        } else {
            $this->renderFile(
                'bundle/services.' . $format . '.twig',
                $dir . '/Resources/config/services.' . $format,
                $parameters
            );
        }

        if ('annotation' != $format) {
            $this->renderFile(
                'bundle/routing.' . $format . '.twig',
                $dir . '/Resources/config/routing.' . $format,
                $parameters
            );
        }
    }