public function generateProxyClass($entity, $config)
    {
        if (!is_array($config) && !$config instanceof \ArrayAccess) {
            throw new Exception\InvalidArgumentException(
                'Invalid proxy class configuration; array or instance of ArrayAccess expected');
        }
        
        if (!is_object($entity)) {
            if ((is_string($entity) && (!class_exists($entity))) // non-existent class
                    || (!is_string($entity)) // not an object or string
            ) {
                throw new Exception\InvalidArgumentException(sprintf(
                        '%s expects an object or valid class name; received "%s"',
                        __METHOD__,
                        var_export($entity, 1)
                ));
            }
        }
        
        $this->serviceConfig = $config;
        
        $reflection  = new ReflectionClass($entity);
        $className = $reflection->getName();
        
        $class = ClassGenerator::fromArray([
            'name' => $this->getProxyClassName($className),
            'namespace_name' => $this->getProxyNamespace($className),
            'extended_class' => '\\' . $className,
            'implemented_interfaces' => array('\Zend\EventManager\EventManagerAwareInterface'),
            'methods' => $this->generateMethods($reflection),
            'properties' => [
                new PropertyGenerator('__wrappedObject', null, PropertyGenerator::FLAG_PUBLIC),
                new PropertyGenerator('__eventManager', null, PropertyGenerator::FLAG_PRIVATE),
                new PropertyGenerator('__commandStack', null, PropertyGenerator::FLAG_PRIVATE)
            ]
        ]);
        
        $source = "<?php\n" . $class->generate();
        
        $fileName        = $this->getProxyFileName($className);
        $parentDirectory = $this->proxyDirectory;
        
        if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) {
            throw new Exception\RuntimeException('Proxy directory '.$parentDirectory.' not found');
        }
        
        if ( ! is_writable($parentDirectory)) {
            throw new Exception\RuntimeException('Proxy directory '.$parentDirectory.' is not writable');
        }
        
        $tmpFileName = $fileName . '.' . uniqid('', true);
        file_put_contents($tmpFileName, $source);
        rename($tmpFileName, $fileName);
    }