public function materialize($resource, $overwrite = false)
    {
        if ($resource instanceof MaterializedResource) {
            return $resource;
        }

        if ($overwrite || !$this->exists($resource)) {
            try {
                $this->getReplicator()->replicate($resource);
            }
            catch (ReplicatorException $e) {
                throw new MaterializeException(sprintf('Cannot materialize resource: %s', $e->getMessage()), 0, $e);
            }
            $materialized = true;
        }

        $materializedResource = new MaterializedResource($resource, $this->getDirectory());
        if (!empty($materialized)) {
            $this->dispatcher->dispatch(ResourceEvents::MATERIALIZE, new ResourceMaterializeEvent($materializedResource));
        }

        return $materializedResource;
    }