A general method for constructing spherical galaxy models