rebased to https://github.com/zendframework/zf2/pull/6242