@@ -55,7 +55,38 @@ public function getComposerCommand()
5555 */
5656 public function validatePath ($ path )
5757 {
58- // TODO: Implement validatePath() method.
58+ $ absPath = $ this ->getAbsolutePath ($ path );
59+ $ absPathParts = explode (DIRECTORY_SEPARATOR , $ absPath );
60+ $ nSteps = count ($ absPathParts );
61+
62+ $ tmpPath = $ absPathParts [0 ];
63+ $ prevReadable = false ;
64+ $ prevWritable = false ;
65+
66+ for ($ i = 1 ; $ i < $ nSteps ; $ i ++) {
67+ $ tmpPath .= DIRECTORY_SEPARATOR . $ absPathParts [$ i ];
68+
69+ if (file_exists ($ tmpPath )) {
70+ if (!is_dir ($ tmpPath )) {
71+ if (is_link ($ tmpPath )) {
72+ $ linkPath = readlink ($ tmpPath );
73+ if (false === $ linkPath || !is_dir ($ linkPath )) {
74+ return false ;
75+ }
76+ $ tmpPath = $ linkPath ;
77+ } else {
78+ return false ;
79+ }
80+ }
81+
82+ $ prevReadable = is_readable ($ tmpPath );
83+ $ prevWritable = is_writable ($ tmpPath );
84+ } else {
85+ return ($ prevReadable && $ prevWritable );
86+ }
87+ }
88+
89+ return true ;
5990 }
6091
6192 /**
@@ -64,7 +95,13 @@ public function validatePath($path)
6495 */
6596 public function ensurePath ($ path )
6697 {
67- // TODO: Implement ensurePath() method.
98+ $ absPath = $ this ->getAbsolutePath ($ path );
99+
100+ if (!file_exists ($ absPath ) && false === mkdir ($ absPath , 0755 , true )) {
101+ throw new \RuntimeException ('Unable to create the specified directory ( ' . $ absPath . '). ' );
102+ }
103+
104+ return $ absPath ;
68105 }
69106
70107 /**
@@ -73,7 +110,7 @@ public function ensurePath($path)
73110 */
74111 protected function isAbsolutePath ($ path )
75112 {
76- // TODO: Implement isAbsolutePath() method.
113+ return preg_match ( ' /^[a-z]\:/i ' , $ path ) === 1 ;
77114 }
78115
79116 /**
@@ -82,6 +119,11 @@ protected function isAbsolutePath($path)
82119 */
83120 protected function getAbsolutePath ($ path )
84121 {
85- // TODO: Implement getAbsolutePath() method.
122+ $ path = $ this ->isAbsolutePath ($ path ) ? $ path : (getcwd () . DIRECTORY_SEPARATOR . $ path );
123+
124+ // Normalise directory separators
125+ $ path = preg_replace ('/[\/ \\\\]/u ' , DIRECTORY_SEPARATOR , $ path );
126+
127+ return $ path ;
86128 }
87129}
0 commit comments