How to replace digitalwrite by another method

Everyone knows, that some microboards have a very low number of input/output pins. And we are using some port expanders. On another side, we are using a lot of external libraries which are used a direct digitalread/write. Is it possible to replace it by own digitalread function and redirect this to the port expander? Otherwise, you should fork a library, review its code and replace all calls with another one.